From 5f76bf27509a75bff5c2618e64511ccc7d9662fc Mon Sep 17 00:00:00 2001 From: the Date: Sat, 22 Jun 2024 15:10:46 +0400 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D0=BA=D1=80?= =?UTF-8?q?=D1=83=D0=B4=D0=B0=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/SupplierBindingModel.cs | 2 +- Contracts/BindingModels/SupplyBindingModel.cs | 1 + Contracts/SearchModels/SupplierSearchModel.cs | 2 +- Contracts/SearchModels/SupplySearchModel.cs | 5 +- Contracts/ViewModels/SupplierViewModel.cs | 5 +- Contracts/ViewModels/SupplyViewModel.cs | 2 + DataModels/Models/ISupplier.cs | 2 +- DataModels/Models/ISupply.cs | 1 + DatabaseImplement/Database.cs | 5 +- .../Implements/SupplierStorage.cs | 121 ++++++++++++++ DatabaseImplement/Implements/SupplyStorage.cs | 147 ++++++++++++++++++ DatabaseImplement/Models/Supplier.cs | 109 +++++++++++++ DatabaseImplement/Models/SupplierProduct.cs | 22 +++ DatabaseImplement/Models/Supply.cs | 34 ++-- 14 files changed, 438 insertions(+), 20 deletions(-) create mode 100644 DatabaseImplement/Implements/SupplierStorage.cs create mode 100644 DatabaseImplement/Implements/SupplyStorage.cs create mode 100644 DatabaseImplement/Models/Supplier.cs create mode 100644 DatabaseImplement/Models/SupplierProduct.cs diff --git a/Contracts/BindingModels/SupplierBindingModel.cs b/Contracts/BindingModels/SupplierBindingModel.cs index 20c9701..49f0469 100644 --- a/Contracts/BindingModels/SupplierBindingModel.cs +++ b/Contracts/BindingModels/SupplierBindingModel.cs @@ -11,7 +11,7 @@ namespace Contracts.BindingModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; - public Dictionary AvailibleProducts { get; set; } = new(); + public Dictionary AvailibleProducts { get; set; } = new(); public int Deals { get; set; } = 0; } } diff --git a/Contracts/BindingModels/SupplyBindingModel.cs b/Contracts/BindingModels/SupplyBindingModel.cs index 32f3458..44ba57c 100644 --- a/Contracts/BindingModels/SupplyBindingModel.cs +++ b/Contracts/BindingModels/SupplyBindingModel.cs @@ -12,6 +12,7 @@ namespace Contracts.BindingModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; + public Guid SupplierId { get; set; } public DateTime Date { get; set; } public double Price { get; set; } public SupplyStatus Status { get; set; } diff --git a/Contracts/SearchModels/SupplierSearchModel.cs b/Contracts/SearchModels/SupplierSearchModel.cs index 2082afa..2a889e9 100644 --- a/Contracts/SearchModels/SupplierSearchModel.cs +++ b/Contracts/SearchModels/SupplierSearchModel.cs @@ -10,7 +10,7 @@ namespace Contracts.SearchModels { public Guid? Id { get; set; } public string? Name { get; set; } - public List? AvailibleProducts { get; set; } + //public List? AvailibleProducts { get; set; } public int? Deals { get; set; } } } diff --git a/Contracts/SearchModels/SupplySearchModel.cs b/Contracts/SearchModels/SupplySearchModel.cs index 9f50d53..6fd24c8 100644 --- a/Contracts/SearchModels/SupplySearchModel.cs +++ b/Contracts/SearchModels/SupplySearchModel.cs @@ -1,4 +1,5 @@ -using System; +using DataModels.Enums; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,7 +10,7 @@ namespace Contracts.SearchModels public class SupplySearchModel { public Guid? Id { get; set; } - public List? Products { get; set; } + public SupplyStatus? Status { get; set; } public DateTime? DateStart { get; set; } public DateTime? DateEnd { get; set; } } diff --git a/Contracts/ViewModels/SupplierViewModel.cs b/Contracts/ViewModels/SupplierViewModel.cs index 337dc1d..09b2af2 100644 --- a/Contracts/ViewModels/SupplierViewModel.cs +++ b/Contracts/ViewModels/SupplierViewModel.cs @@ -1,4 +1,5 @@ -using System; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,6 +11,6 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; - public Dictionary AvailibleProducts = new(); + public Dictionary AvailibleProducts = new(); } } diff --git a/Contracts/ViewModels/SupplyViewModel.cs b/Contracts/ViewModels/SupplyViewModel.cs index fca9ae7..08f7046 100644 --- a/Contracts/ViewModels/SupplyViewModel.cs +++ b/Contracts/ViewModels/SupplyViewModel.cs @@ -2,6 +2,7 @@ using DataModels.Models; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -12,6 +13,7 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; + public Guid SupplierId { get; set; } public string SupplierName { get; set; } = string.Empty; public double Price { get; set; } public Dictionary Products { get; set; } = new(); diff --git a/DataModels/Models/ISupplier.cs b/DataModels/Models/ISupplier.cs index d2ac2e2..4289d90 100644 --- a/DataModels/Models/ISupplier.cs +++ b/DataModels/Models/ISupplier.cs @@ -9,7 +9,7 @@ namespace DataModels.Models public interface ISupplier : IId { string Name { get; } - Dictionary AvailibleProducts { get; } + Dictionary AvailibleProducts { get; } int Deals { get; } } } diff --git a/DataModels/Models/ISupply.cs b/DataModels/Models/ISupply.cs index 0fddfba..fff5ab5 100644 --- a/DataModels/Models/ISupply.cs +++ b/DataModels/Models/ISupply.cs @@ -11,6 +11,7 @@ namespace DataModels.Models { string Name { get; } double Price { get; } + Guid SupplierId { get; } DateTime Date { get; } SupplyStatus Status { get; } Dictionary SupplyProducts { get; } diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index b8320eb..f17e76a 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -26,6 +26,9 @@ namespace DatabaseImplement 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!; + public virtual DbSet Suppliers { get; set; } = null!; + public virtual DbSet SupplierProducts { get; set; } = null!; + + public virtual DbSet MediaFiles { get; set; } = null!; } } \ No newline at end of file diff --git a/DatabaseImplement/Implements/SupplierStorage.cs b/DatabaseImplement/Implements/SupplierStorage.cs new file mode 100644 index 0000000..c44c0bf --- /dev/null +++ b/DatabaseImplement/Implements/SupplierStorage.cs @@ -0,0 +1,121 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Implements +{ + public class SupplierStorage : ISupplierStorage + { + public SupplierViewModel? Delete(SupplierBindingModel model) + { + using var context = new Database(); + var element = context.Suppliers + .Include(x => x.Products) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Suppliers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public SupplierViewModel? GetElement(SupplierSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new Database(); + return context.Suppliers + .Include(x => x.Name) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(SupplierSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Deals.HasValue) + { + return new(); + } + using var context = new Database(); + if (!string.IsNullOrEmpty(model.Name)) + { + return context.Suppliers + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => model.Name == x.Name) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Suppliers + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => model.Deals <= x.Deals) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Suppliers + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SupplierViewModel? Insert(SupplierBindingModel model) + { + using var context = new Database(); + var newProduct = Supplier.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Suppliers.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public SupplierViewModel? Update(SupplierBindingModel model) + { + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Suppliers.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(context, model); + context.SaveChanges(); + product.UpdateProducts(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/DatabaseImplement/Implements/SupplyStorage.cs b/DatabaseImplement/Implements/SupplyStorage.cs new file mode 100644 index 0000000..64d3c8f --- /dev/null +++ b/DatabaseImplement/Implements/SupplyStorage.cs @@ -0,0 +1,147 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Implements +{ + public class SupplyStorage : ISupplyStorage + { + public SupplyViewModel? Delete(SupplyBindingModel model) + { + using var context = new Database(); + var element = context.Supplies + .Include(x => x.Products) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Supplies.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public SupplyViewModel? GetElement(SupplySearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new Database(); + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(SupplySearchModel model) + { + if (!model.DateStart.HasValue && !model.DateEnd.HasValue && model.Status == null) + { + return new(); + } + using var context = new Database(); + if (model.DateStart.HasValue && model.DateEnd.HasValue) + { + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => x.Id == model.Id || model.DateStart <= x.Date && x.Date <= model.DateEnd) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.DateEnd.HasValue) + { + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => x.Id == model.Id || x.Date <= model.DateEnd) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.DateStart.HasValue) + { + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => x.Id == model.Id || model.DateStart <= x.Date) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => model.Status.Equals(x.Status)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SupplyViewModel? Insert(SupplyBindingModel model) + { + using var context = new Database(); + var newProduct = Supply.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Supplies.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public SupplyViewModel? Update(SupplyBindingModel model) + { + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Supplies.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateProducts(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/DatabaseImplement/Models/Supplier.cs b/DatabaseImplement/Models/Supplier.cs new file mode 100644 index 0000000..2901ee2 --- /dev/null +++ b/DatabaseImplement/Models/Supplier.cs @@ -0,0 +1,109 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace DatabaseImplement.Models +{ + public class Supplier : ISupplier + { + public Guid Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public int Deals { get; set; } + private Dictionary? _availibleProducts = null; + [NotMapped] + public Dictionary AvailibleProducts + { + get + { + if (_availibleProducts == null) + { + _availibleProducts = Products + .ToDictionary(recPC => recPC.Id, recPC => + (recPC.Product as IProduct, recPC.Count)); + } + return _availibleProducts; + } + } + [ForeignKey("SupplierId")] + public virtual List Products { get; set; } = new(); + + public static Supplier Create(Database context, SupplierBindingModel model) + { + return new Supplier() + { + Id = model.Id, + Name = model.Name, + Products = model.AvailibleProducts.Select(x => new + SupplierProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(Database context, SupplierBindingModel model) + { + Name = model.Name; + Products = model.AvailibleProducts.Select(x => new + SupplierProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(); + } + public SupplierViewModel GetViewModel + { + get + { + var context = new Database(); + return new() + { + Id = Id, + Name = Name, + AvailibleProducts = AvailibleProducts + }; + } + } + public void UpdateProducts(Database context, SupplierBindingModel model) + { + var supplierProducts = context.SupplierProducts.Where(rec => + rec.Id == model.Id).ToList(); + if (supplierProducts != null && supplierProducts.Count > 0) + { // удалили те, которых нет в модели + context.SupplierProducts.RemoveRange(supplierProducts.Where(rec + => !model.AvailibleProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateProduct in supplierProducts) + { + updateProduct.Count = model.AvailibleProducts[updateProduct.ProductId].Item2; + model.AvailibleProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + var supplier = context.Suppliers.First(x => x.Id == Id); + foreach (var pc in model.AvailibleProducts) + { + context.SupplierProducts.Add(new SupplierProduct + { + Supplier = supplier, + Product = context.Products.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _availibleProducts = null; + } + } +} diff --git a/DatabaseImplement/Models/SupplierProduct.cs b/DatabaseImplement/Models/SupplierProduct.cs new file mode 100644 index 0000000..95ce3af --- /dev/null +++ b/DatabaseImplement/Models/SupplierProduct.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class SupplierProduct + { + public Guid Id { get; set; } + [Required] + public Guid SupplierId { get; set; } + [Required] + public Guid ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Supplier Supplier { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} diff --git a/DatabaseImplement/Models/Supply.cs b/DatabaseImplement/Models/Supply.cs index b30e50a..64e75f4 100644 --- a/DatabaseImplement/Models/Supply.cs +++ b/DatabaseImplement/Models/Supply.cs @@ -16,13 +16,14 @@ 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 Guid SupplierId { get; set; } + [Required] public DateTime Date { get; set; } [Required] public SupplyStatus Status { get; set; } = SupplyStatus.Pending; @@ -43,7 +44,7 @@ namespace DatabaseImplement.Models } [ForeignKey("SupplyId")] public virtual List Products { get; set; } = new(); - + public virtual Supplier Supplier { get; set; } public static Supply Create(Database context, SupplyBindingModel model) { return new Supply() @@ -51,6 +52,8 @@ namespace DatabaseImplement.Models Id = model.Id, Name = model.Name, Price = model.Price, + Date = model.Date, + SupplierId = model.SupplierId, Products = model.SupplyProducts.Select(x => new SupplyProduct { @@ -64,17 +67,24 @@ namespace DatabaseImplement.Models Name = model.Name; Price = model.Price; } - public SupplyViewModel GetViewModel => new() + public SupplyViewModel GetViewModel { - Id = Id, - Name = Name, - Price = Price, - Products = SupplyProducts, - //supplierName сделать - Date = Date, - Status = Status - }; - public void UpdateComponents(Database context, SupplyBindingModel model) + get + { + var context = new Database(); + return new() + { + Id = Id, + Name = Name, + Price = Price, + Products = SupplyProducts, + Date = Date, + Status = Status, + SupplierName = context.Suppliers.FirstOrDefault(x => x.Id == Id)?.Name ?? string.Empty, + }; + } + } + public void UpdateProducts(Database context, SupplyBindingModel model) { var supplyProducts = context.SupplyProducts.Where(rec => rec.Id == model.Id).ToList();