ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallationDatabaseImplement/Models/Package.cs

100 lines
3.6 KiB
C#
Raw Normal View History

2024-04-07 17:24:16 +04:00
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
2024-05-12 14:45:39 +04:00
using System.ComponentModel.DataAnnotations;
2024-04-07 17:24:16 +04:00
namespace SoftwareInstallationDatabaseImplement.Models
{
public class Package : IPackageModel
{
public int Id { get; set; }
[Required]
public string PackageName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
2024-05-12 14:45:39 +04:00
private Dictionary<int, (IComponentModel, int)>? _pizzaComponents = null;
2024-04-07 17:24:16 +04:00
[NotMapped]
public Dictionary<int, (IComponentModel, int)> PackageComponents
{
get
{
2024-05-12 14:45:39 +04:00
if (_pizzaComponents == null)
2024-04-07 17:24:16 +04:00
{
2024-05-12 14:45:39 +04:00
_pizzaComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
2024-04-07 17:24:16 +04:00
}
2024-05-12 14:45:39 +04:00
return _pizzaComponents;
2024-04-07 17:24:16 +04:00
}
}
[ForeignKey("PackageId")]
public virtual List<PackageComponent> Components { get; set; } = new();
[ForeignKey("PackageId")]
public virtual List<Order> Orders { get; set; } = new();
2024-05-12 14:45:39 +04:00
[ForeignKey("PackageId")]
public virtual List<ShopPackages> ShopPackages { get; set; } = new();
public static Package
Create(SoftwareInstallationDatabase context, PackageBindingModel model)
2024-04-07 17:24:16 +04:00
{
return new Package()
{
Id = model.Id,
PackageName = model.PackageName,
Price = model.Price,
2024-05-12 14:45:39 +04:00
Components = model.PackageComponents.Select(x => new PackageComponent
2024-04-07 17:24:16 +04:00
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
2024-05-12 14:45:39 +04:00
2024-04-07 17:24:16 +04:00
public void Update(PackageBindingModel model)
{
PackageName = model.PackageName;
Price = model.Price;
}
2024-05-12 14:45:39 +04:00
2024-04-07 17:24:16 +04:00
public PackageViewModel GetViewModel => new()
{
Id = Id,
PackageName = PackageName,
Price = Price,
PackageComponents = PackageComponents
};
2024-05-12 14:45:39 +04:00
public void UpdateComponents(SoftwareInstallationDatabase context, PackageBindingModel model)
2024-04-07 17:24:16 +04:00
{
2024-05-12 14:45:39 +04:00
var pizzaComponents = context.PackageComponents.Where(rec => rec.PackageId == model.Id).ToList();
2024-04-07 17:24:16 +04:00
2024-05-12 14:45:39 +04:00
if (pizzaComponents != null && pizzaComponents.Count > 0)
{
context.PackageComponents.RemoveRange(pizzaComponents.Where(rec => !model.PackageComponents.ContainsKey(rec.ComponentId)));
2024-04-07 17:24:16 +04:00
context.SaveChanges();
2024-05-12 14:45:39 +04:00
foreach (var updateComponent in pizzaComponents)
2024-04-07 17:24:16 +04:00
{
2024-05-12 14:45:39 +04:00
updateComponent.Count = model.PackageComponents[updateComponent.ComponentId].Item2;
2024-04-07 17:24:16 +04:00
model.PackageComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
2024-05-12 14:45:39 +04:00
var pizza = context.Packages.First(x => x.Id == Id);
2024-04-07 17:24:16 +04:00
foreach (var pc in model.PackageComponents)
{
context.PackageComponents.Add(new PackageComponent
{
2024-05-12 14:45:39 +04:00
Package = pizza,
2024-04-07 17:24:16 +04:00
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
2024-05-12 14:45:39 +04:00
_pizzaComponents = null;
2024-04-07 17:24:16 +04:00
}
}
}