From 95b2759a12647d0393d231566da0d3bd316c676c Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 17:05:42 +0400 Subject: [PATCH] + --- .../ComputerHardwareStore.sln | 8 +++- .../BindingModels/OrderBindingModel.cs | 2 +- .../ViewModels/OrderViewModel.cs | 2 +- .../Models/IOrderModel.cs | 2 +- .../ComputerHardwareStoreDBContext.cs | 1 + .../Models/Order.cs | 38 ++++++++++++++----- .../Models/OrderProduct.cs | 17 +++++++++ 7 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs diff --git a/ComputerHardwareStore/ComputerHardwareStore.sln b/ComputerHardwareStore/ComputerHardwareStore.sln index 1edd1ac..1f6cda1 100644 --- a/ComputerHardwareStore/ComputerHardwareStore.sln +++ b/ComputerHardwareStore/ComputerHardwareStore.sln @@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDataMo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreContracts", "ComputerHardwareStoreContracts\ComputerHardwareStoreContracts.csproj", "{5F394E21-2597-432B-AE73-BBAFD8D9F50E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDatabaseImplement", "ComputerHardwareStoreDatabaseImplement\ComputerHardwareStoreDatabaseImplement.csproj", "{93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Debug|Any CPU.Build.0 = Debug|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.ActiveCfg = Release|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.Build.0 = Release|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs index 9cf80bd..e1017a0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime? DateImplement { get; set; } = null; - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs index d2f0ff1..a0b6602 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs @@ -16,6 +16,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public DateTime DateCreate { get; set; } [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs index 06f933b..3e7f94e 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs @@ -8,6 +8,6 @@ namespace ComputerHardwareStoreDataModels.Models OrderStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } - public Dictionary OrderProduct { get; } + public Dictionary OrderProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 9226f42..850515c 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -31,5 +31,6 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Components { set; get; } public virtual DbSet Products { set; get; } public virtual DbSet ProductComponents { set; get; } + public virtual DbSet OrderProducts { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs index eebf5a7..1754b65 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Enums; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -18,9 +19,25 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime? DateImplement { get; set; } - [Required] - public int ProductId { get; private set; } - public virtual Product? Product { get; set; } + + private Dictionary? _orderProducts = null; + [NotMapped] + public Dictionary OrderProducts + { + get + { + if (_orderProducts == null) + { + _orderProducts = Products + .ToDictionary( + op => op.ProductId, + op => (op.Product as IProductModel, op.Count)); + } + return _orderProducts; + } + } + [ForeignKey("OrderId")] + public virtual List Products { get; set; } = new(); public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model) { if (model == null) @@ -30,13 +47,18 @@ namespace ComputerHardwareStoreDatabaseImplement.Models return new Order() { Id = model.Id, - Count = model.Count, Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, - ProductId = model.CannedId, - Product = context.Products.First(x => x.Id == model.ProductId) + Products = model.OrderProducts + .Select(op => new OrderProduct() + { + OrderId = model.Id, + ProductId = op.Key, + Count = op.Value.Item2 + }) + .ToList() }; } public void Update(OrderBindingModel model) @@ -51,12 +73,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, - ProductId = ProductId, - Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - ProductName = Product.ProductName }; } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs new file mode 100644 index 0000000..9930fe7 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class OrderProduct + { + public int Id { get; set; } + [Required] + public int OrderId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Product Product { get; set; } = new(); + public virtual Order Order { get; set; } = new(); + } +}