From 43aef44d98db69acc935c691915078a26a995cd8 Mon Sep 17 00:00:00 2001 From: the Date: Sat, 22 Jun 2024 12:18:43 +0400 Subject: [PATCH] supply model --- BusinessLogic/BusinessLogic/ProductLogic.cs | 47 ++++++++ Contracts/BindingModels/SupplyBindingModel.cs | 2 +- Contracts/ViewModels/SupplyViewModel.cs | 4 +- DataModels/Models/ISupply.cs | 2 +- DatabaseImplement/Database.cs | 2 + DatabaseImplement/Models/Supply.cs | 108 ++++++++++++++++++ DatabaseImplement/Models/SupplyProduct.cs | 24 ++++ 7 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 BusinessLogic/BusinessLogic/ProductLogic.cs create mode 100644 DatabaseImplement/Models/Supply.cs create mode 100644 DatabaseImplement/Models/SupplyProduct.cs diff --git a/BusinessLogic/BusinessLogic/ProductLogic.cs b/BusinessLogic/BusinessLogic/ProductLogic.cs new file mode 100644 index 0000000..a177c19 --- /dev/null +++ b/BusinessLogic/BusinessLogic/ProductLogic.cs @@ -0,0 +1,47 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BusinessLogic.BusinessLogic +{ + public class ProductLogic : IProductLogic + { + private readonly IProductStorage _productStorage; + public ProductLogic(IProductStorage productStorage) + { + _productStorage = productStorage; + } + public bool Create(ProductBindingModel model) + { + throw new NotImplementedException(); + } + + public bool Delete(ProductBindingModel model) + { + throw new NotImplementedException(); + } + + public ProductViewModel? ReadElement(ProductSearchModel model) + { + throw new NotImplementedException(); + } + + public List? ReadList(ProductSearchModel? model) + { + throw new NotImplementedException(); + } + + public bool Update(ProductBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/Contracts/BindingModels/SupplyBindingModel.cs b/Contracts/BindingModels/SupplyBindingModel.cs index 9c2e2f1..32f3458 100644 --- a/Contracts/BindingModels/SupplyBindingModel.cs +++ b/Contracts/BindingModels/SupplyBindingModel.cs @@ -15,6 +15,6 @@ namespace Contracts.BindingModels public DateTime Date { get; set; } public double Price { get; set; } public SupplyStatus Status { get; set; } - public Dictionary SupplyProducts { get; set; } = new(); + public Dictionary SupplyProducts { get; set; } = new(); } } diff --git a/Contracts/ViewModels/SupplyViewModel.cs b/Contracts/ViewModels/SupplyViewModel.cs index 9dca657..fca9ae7 100644 --- a/Contracts/ViewModels/SupplyViewModel.cs +++ b/Contracts/ViewModels/SupplyViewModel.cs @@ -1,4 +1,5 @@ using DataModels.Enums; +using DataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -10,9 +11,10 @@ namespace Contracts.ViewModels public class SupplyViewModel { public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; public string SupplierName { get; set; } = string.Empty; public double Price { get; set; } - public Dictionary Products { get; set; } = new(); + public Dictionary Products { get; set; } = new(); public DateTime Date { get; set; } public SupplyStatus Status { get; set; } } diff --git a/DataModels/Models/ISupply.cs b/DataModels/Models/ISupply.cs index a728595..0fddfba 100644 --- a/DataModels/Models/ISupply.cs +++ b/DataModels/Models/ISupply.cs @@ -13,6 +13,6 @@ namespace DataModels.Models double Price { get; } DateTime Date { get; } SupplyStatus Status { get; } - Dictionary SupplyProducts { get; } + Dictionary SupplyProducts { get; } } } diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index 69e2127..b8320eb 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -24,6 +24,8 @@ namespace DatabaseImplement public virtual DbSet Sells { get; set; } = null!; public virtual DbSet Purchases { get; set; } = null!; public virtual DbSet Products { get; set; } = null!; + public virtual DbSet Supplies { get; set; } = null!; + public virtual DbSet SupplyProducts { get; set; } = null!; public virtual DbSet MediaFiles { get; set; } = null!; } } \ No newline at end of file diff --git a/DatabaseImplement/Models/Supply.cs b/DatabaseImplement/Models/Supply.cs new file mode 100644 index 0000000..b30e50a --- /dev/null +++ b/DatabaseImplement/Models/Supply.cs @@ -0,0 +1,108 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Enums; +using DataModels.Models; +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class Supply : ISupply + { + [Required] + public Guid Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public DateTime Date { get; set; } + [Required] + public SupplyStatus Status { get; set; } = SupplyStatus.Pending; + private Dictionary? _supplyProducts = null; + [NotMapped] + public Dictionary SupplyProducts + { + get + { + if (_supplyProducts == null) + { + _supplyProducts = Products + .ToDictionary(recPC => recPC.Id, recPC => + (recPC.Product as IProduct, recPC.Count)); + } + return _supplyProducts; + } + } + [ForeignKey("SupplyId")] + public virtual List Products { get; set; } = new(); + + public static Supply Create(Database context, SupplyBindingModel model) + { + return new Supply() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Products = model.SupplyProducts.Select(x => new + SupplyProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SupplyBindingModel model) + { + Name = model.Name; + Price = model.Price; + } + public SupplyViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Price = Price, + Products = SupplyProducts, + //supplierName сделать + Date = Date, + Status = Status + }; + public void UpdateComponents(Database context, SupplyBindingModel model) + { + var supplyProducts = context.SupplyProducts.Where(rec => + rec.Id == model.Id).ToList(); + if (supplyProducts != null && supplyProducts.Count > 0) + { // удалили те, которых нет в модели + context.SupplyProducts.RemoveRange(supplyProducts.Where(rec + => !model.SupplyProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateProduct in supplyProducts) + { + updateProduct.Count = model.SupplyProducts[updateProduct.ProductId].Item2; + model.SupplyProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + var supply = context.Supplies.First(x => x.Id == Id); + foreach (var pc in model.SupplyProducts) + { + context.SupplyProducts.Add(new SupplyProduct + { + Supply = supply, + Product = context.Products.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _supplyProducts = null; + } + } +} diff --git a/DatabaseImplement/Models/SupplyProduct.cs b/DatabaseImplement/Models/SupplyProduct.cs new file mode 100644 index 0000000..f55ed47 --- /dev/null +++ b/DatabaseImplement/Models/SupplyProduct.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class SupplyProduct + { + public Guid Id { get; set; } + [Required] + public Guid SupplyId { get; set; } + [Required] + public Guid ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Supply Supply { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +}