diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs index af08205..df458d5 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs @@ -8,7 +8,7 @@ namespace ComputerHardwareStoreContracts.BindingModels public int VendorId { get; set; } public double Cost { get; set; } public DateTime DateCreate { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public Dictionary PurchaseBuilds { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs index d0de94d..7e159b7 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs @@ -13,8 +13,8 @@ namespace ComputerHardwareStoreContracts.ViewModels public int VendorId { get; set; } [DisplayName("Сумма")] public double Sum { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); + public Dictionary PurchaseBuilds { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs index 9afa7b6..39dd9e9 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs @@ -5,7 +5,7 @@ int VendorId { get; } double Cost { get; } DateTime DateCreate { get; } - public Dictionary PurchaseBuild { get; } - public Dictionary PurchaseProduct { get; } + public Dictionary PurchaseBuilds { get; } + public Dictionary PurchaseProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 84fa66a..6d081d6 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -36,5 +36,8 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet OrderProducts { set; get; } public virtual DbSet BuildComponents { set; get; } public virtual DbSet Comments { set; get; } + public virtual DbSet Purchases { set; get; } + public virtual DbSet PurchaseBuilds { set; get; } + public virtual DbSet PurchaseProducts { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs new file mode 100644 index 0000000..3c5c9f0 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -0,0 +1,151 @@ + +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Purchase : IPurchaseModel + { + public int Id { get; private set; } + [Required] + public int VendorId { get; set; } + [Required] + public double Cost { get; set; } + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + private Dictionary? _purchaseBuilds = null; + [NotMapped] + public Dictionary PurchaseBuilds + { + get + { + if (_purchaseBuilds == null) + { + _purchaseBuilds = Builds + .ToDictionary(op => op.BuildId, op => (op.Build as IBuildModel, op.Count)); + } + return _purchaseBuilds; + } + } + + [ForeignKey("PurchaseId")] + public virtual List Builds { get; set; } = new(); + private Dictionary? _purchaseProducts = null; + [NotMapped] + public Dictionary PurchaseProducts + { + get + { + if (_purchaseProducts == null) + { + _purchaseProducts = Products + .ToDictionary(op => op.ProductId, op => (op.Product as IProductModel, op.Count)); + } + return _purchaseProducts; + } + } + [ForeignKey("PurchaseId")] + public virtual List Products { get; set; } = new(); + + public static Purchase Create(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + return new Purchase() + { + Id = model.Id, + Cost = model.Cost, + DateCreate = model.DateCreate, + Builds = model.PurchaseBuilds.Select(x => + new PurchaseBuild + { + Build = context.Builds.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + Products = model.PurchaseProducts.Select(x => + new PurchaseProduct + { + Product = context.Products.First(z => z.Id == z.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PurchaseBindingModel model) + { + Cost = model.Cost; + + } + public PurchaseViewModel GetViewModel => new() + { + Id = Id, + Cost = Cost, + DateCreate = DateCreate, + PurchaseBuilds = PurchaseBuilds, + PurchaseProducts = PurchaseProducts + }; + + public static void UpdateBuilds(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productBuilds = context.PurchaseBuilds + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productBuilds.Count != 0 && productBuilds.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseBuilds + .Where(pc => !model.PurchaseBuilds.ContainsKey(pc.BuildId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productBuilds + .ForEach(updateBuild => + { + updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2; + model.PurchaseBuilds.Remove(updateBuild.BuildId); + }); + } + // добавили новые + context.PurchaseBuilds + .AddRange(model.PurchaseBuilds.Values + .Select(val => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + + public static void UpdateProducts(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productProducts = context.PurchaseProducts + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productProducts.Count != 0 && productProducts.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseProducts + .Where(pc => !model.PurchaseProducts.ContainsKey(pc.ProductId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productProducts + .ForEach(updateProduct => + { + updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2; + model.PurchaseProducts.Remove(updateProduct.ProductId); + }); + } + // добавили новые + context.PurchaseProducts + .AddRange(model.PurchaseProducts.Values + .Select(val => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs new file mode 100644 index 0000000..2f5c1af --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseBuild + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int BuildId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Build Build { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs new file mode 100644 index 0000000..2bc9116 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseProduct + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +}