From 172c61b68249f49a113ed2f4ab205706350fd8fe Mon Sep 17 00:00:00 2001 From: Yourdax Date: Fri, 3 May 2024 20:08:53 +0400 Subject: [PATCH] fixes --- .../BusinessLogics/OrderLogic.cs | 2 +- .../BusinessLogics/ReportGuarantorLogic.cs | 44 +++++++ .../BindingModels/OrderBindingModel.cs | 5 +- .../BindingModels/ProductBindingModel.cs | 3 +- .../BindingModels/ReportBindingModel.cs | 15 +++ .../IReportGuarantorLogic.cs | 23 ++++ .../SearchModels/ProductSearchModel.cs | 1 + .../StorageContracts/IComponentStorage.cs | 5 +- .../ViewModels/OrderViewModel.cs | 2 +- .../ViewModels/ProductViewModel.cs | 1 + .../ReportComponentByDateViewModel.cs | 20 +++ .../ReportComponentOrderViewModel.cs | 13 ++ .../DiningRoomDataModels/Models/ICardModel.cs | 3 +- .../Models/IOrderModel.cs | 2 +- .../Models/IProductModel.cs | 8 +- .../Implements/ComponentStorage.cs | 124 +++++++++++++----- .../Implements/ProductStorage.cs | 96 ++++++++------ .../Models/Card.cs | 6 + .../Models/Component.cs | 29 +++- .../Models/Drink.cs | 4 +- .../Models/Order.cs | 33 +++-- .../Models/Product.cs | 51 +++---- 22 files changed, 363 insertions(+), 127 deletions(-) create mode 100644 DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportGuarantorLogic.cs create mode 100644 DiningRoom/DiningRoomContracts/BindingModels/ReportBindingModel.cs create mode 100644 DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportGuarantorLogic.cs create mode 100644 DiningRoom/DiningRoomContracts/ViewModels/ReportComponentByDateViewModel.cs create mode 100644 DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs diff --git a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/OrderLogic.cs b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/OrderLogic.cs index f0c2dc3..4d5660a 100644 --- a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/OrderLogic.cs @@ -27,7 +27,7 @@ namespace DiningRoomBusinessLogic.BusinessLogics public List? ReadList(OrderSearchModel? model) { - //model.UserId = -1 для swagger, чтобы можно было считать все заказы всех пользователей + var list = (model == null || model.UserId == -1) ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { diff --git a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportGuarantorLogic.cs b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportGuarantorLogic.cs new file mode 100644 index 0000000..8431f3b --- /dev/null +++ b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportGuarantorLogic.cs @@ -0,0 +1,44 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.BusinessLogicContracts; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; + +namespace DiningRoomBusinessLogic.BusinessLogics +{ + public class ReportGuarantorLogic : IReportGuarantorLogic + { + private readonly IComponentStorage _componentStorage; + + public ReportGuarantorLogic(IComponentStorage ComponentStorage) + { + _componentStorage = ComponentStorage; + } + + /// + /// Получение отчёта для Word или Excel + /// + public List GetReportComponentsWithShipments(List SelectedComponents) + { + return _componentStorage.GetComponentsOrders(SelectedComponents); + } + + /// + /// Получение отчёта для отправки на почту + /// + public List GetReportComponentsByRequestDate(UserSearchModel CurrentUser, ReportBindingModel Report) + { + return _componentStorage.GetComponentsByDate(Report, CurrentUser); + } + + public void SaveReportToWordFile(ReportBindingModel Model) + { + throw new NotImplementedException(); + } + + public void SaveReportToExcelFile(ReportBindingModel Model) + { + throw new NotImplementedException(); + } + } +} diff --git a/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs b/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs index 9202b01..37ef8a8 100644 --- a/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs +++ b/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs @@ -11,13 +11,10 @@ namespace DiningRoomContracts.BindingModels public class OrderBindingModel : IOrderModel { public int Id { get; set; } - public int UserId { get; set; } public int ProductId { get; set; } - - public DateTime DateCreate { get; set; } = DateTime.Now; public string ProductName { get; set; } = string.Empty; - + public DateTime DateCreate { get; set; } = DateTime.Now; public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public int Count { get; set; } public double Sum { get; set; } diff --git a/DiningRoom/DiningRoomContracts/BindingModels/ProductBindingModel.cs b/DiningRoom/DiningRoomContracts/BindingModels/ProductBindingModel.cs index c4f4740..a4d92d1 100644 --- a/DiningRoom/DiningRoomContracts/BindingModels/ProductBindingModel.cs +++ b/DiningRoom/DiningRoomContracts/BindingModels/ProductBindingModel.cs @@ -7,8 +7,9 @@ namespace DiningRoomContracts.BindingModels public int Id { get; set; } public int UserId { get; set; } + public int? OrderId { get; set; } - public string ProductName { get; set; } = string.Empty; + public string ProductName { get; set; } = string.Empty; public double Cost { get; set; } diff --git a/DiningRoom/DiningRoomContracts/BindingModels/ReportBindingModel.cs b/DiningRoom/DiningRoomContracts/BindingModels/ReportBindingModel.cs new file mode 100644 index 0000000..59db86e --- /dev/null +++ b/DiningRoom/DiningRoomContracts/BindingModels/ReportBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomContracts.BindingModels +{ + public class ReportBindingModel + { + public string FileName { get; set; } = string.Empty; + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + } +} diff --git a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportGuarantorLogic.cs b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportGuarantorLogic.cs new file mode 100644 index 0000000..a831dae --- /dev/null +++ b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportGuarantorLogic.cs @@ -0,0 +1,23 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.ViewModels; + +namespace DiningRoomContracts.BusinessLogicContracts +{ + public interface IReportGuarantorLogic + { + /// + /// Получение отчёта для Word или Excel + /// + List GetReportComponentsWithShipments(List SelectedComponents); + + /// + /// Получение отчёта для отправки на почту + /// + List GetReportComponentsByRequestDate(UserSearchModel CurrentUser, ReportBindingModel Report); + + void SaveReportToWordFile(ReportBindingModel Model); + + void SaveReportToExcelFile(ReportBindingModel Model); + } +} diff --git a/DiningRoom/DiningRoomContracts/SearchModels/ProductSearchModel.cs b/DiningRoom/DiningRoomContracts/SearchModels/ProductSearchModel.cs index 19837f2..b2f9e7c 100644 --- a/DiningRoom/DiningRoomContracts/SearchModels/ProductSearchModel.cs +++ b/DiningRoom/DiningRoomContracts/SearchModels/ProductSearchModel.cs @@ -5,6 +5,7 @@ public int? Id { get; set; } public int? UserId { get; set; } + public int? OrderId { get; set; } public string? ProductName { get; set; } } diff --git a/DiningRoom/DiningRoomContracts/StorageContracts/IComponentStorage.cs b/DiningRoom/DiningRoomContracts/StorageContracts/IComponentStorage.cs index 7df7126..37a1ca2 100644 --- a/DiningRoom/DiningRoomContracts/StorageContracts/IComponentStorage.cs +++ b/DiningRoom/DiningRoomContracts/StorageContracts/IComponentStorage.cs @@ -17,5 +17,8 @@ namespace DiningRoomContracts.StorageContracts ComponentViewModel? Update(ComponentBindingModel Model); ComponentViewModel? Delete(ComponentBindingModel Model); - } + List GetComponentsOrders(List Models); + + List GetComponentsByDate(ReportBindingModel ReportModel, UserSearchModel UserModel); + } } diff --git a/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs index cdcbfa4..e413a26 100644 --- a/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs +++ b/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs @@ -21,7 +21,7 @@ namespace DiningRoomContracts.ViewModels public string ProductName { get; set; } = string.Empty; [DisplayName("Статус")] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [DisplayName("Стоимость")] + [DisplayName("Количество")] public int Count { get; set; } [DisplayName("Стоимость")] public double Sum { get; set; } diff --git a/DiningRoom/DiningRoomContracts/ViewModels/ProductViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/ProductViewModel.cs index 3e38f67..127125c 100644 --- a/DiningRoom/DiningRoomContracts/ViewModels/ProductViewModel.cs +++ b/DiningRoom/DiningRoomContracts/ViewModels/ProductViewModel.cs @@ -8,6 +8,7 @@ namespace DiningRoomContracts.ViewModels public int Id { get; set; } public int UserId { get; set; } + public int? OrderId { get; set; } [DisplayName("Название товара")] public string ProductName { get; set; } = string.Empty; diff --git a/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentByDateViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentByDateViewModel.cs new file mode 100644 index 0000000..9ba29bb --- /dev/null +++ b/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentByDateViewModel.cs @@ -0,0 +1,20 @@ +namespace DiningRoomContracts.ViewModels +{ + public class ReportComponentByDateViewModel + { + public int ComponentId { get; set; } + + public string ComponentName { get; set; } = string.Empty; + + public double ComponentCost { get; set; } + + public int ProductId { get; set; } + + public string ProductName { get; set; } = string.Empty; + + public double ProductPrice { get; set; } + + public int CardId { get; set; } + public string CardName { get; set; } + } +} diff --git a/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs new file mode 100644 index 0000000..9e7b829 --- /dev/null +++ b/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs @@ -0,0 +1,13 @@ +namespace DiningRoomContracts.ViewModels +{ + public class ReportComponentOrderViewModel + { + public int ComponentId { get; set; } + + public string ComponentName { get; set; } = string.Empty; + + public double ComponentCost { get; set; } + + public List<(int Count, string ProductName, double ProductPrice, DateTime OrderDate)> Orders { get; set; } = new(); + } +} diff --git a/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs b/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs index 358d754..46da448 100644 --- a/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs @@ -14,5 +14,6 @@ /// Название карты /// string CardName { get; } - } + DateTime DateCardCreate { get; } + } } diff --git a/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs b/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs index 25dec81..3af22d0 100644 --- a/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs @@ -15,8 +15,8 @@ namespace DiningRoomDataModels.Models /// Дата создания заказа /// DateTime DateCreate { get; } - int ProductId { get; } + string ProductName { get; } /// /// Статус заказа /// diff --git a/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs b/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs index 043224b..65cd61b 100644 --- a/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs @@ -16,7 +16,7 @@ string ProductName { get; } /// - /// Стоимость товара + /// Стоимость блюда /// double Cost { get; } @@ -24,5 +24,9 @@ /// Список продуктов /// Dictionary ProductComponents { get; } - } + /// + /// Привязка блюда к заказу + /// + int? OrderId { get; } + } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs index 573c2b0..94bf19a 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs @@ -3,6 +3,7 @@ using DiningRoomContracts.SearchModels; using DiningRoomContracts.StorageContracts; using DiningRoomContracts.ViewModels; using DiningRoomDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace DiningRoomDatabaseImplement.Implements { @@ -30,56 +31,119 @@ namespace DiningRoomDatabaseImplement.Implements .ToList(); } // - public ComponentViewModel? GetElement(ComponentSearchModel model) + public ComponentViewModel? GetElement(ComponentSearchModel Model) { - if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + using var Context = new DiningRoomDatabase(); + + // ComponentName is unique + if (!string.IsNullOrEmpty(Model.ComponentName)) { - return null; + return Context.Components + .FirstOrDefault(x => x.ComponentName == Model.ComponentName)? + .GetViewModel; } - using var context = new DiningRoomDatabase(); - return context.Components - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; + + return Context.Components + .FirstOrDefault(x => x.Id == Model.Id)? + .GetViewModel; } // public ComponentViewModel? Insert(ComponentBindingModel model) { - var newComponent = Component.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new DiningRoomDatabase(); - context.Components.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; + using var Context = new DiningRoomDatabase(); + + var NewComponent = Component.Create(model); + Context.Components.Add(NewComponent); + Context.SaveChanges(); + + return NewComponent.GetViewModel; } public ComponentViewModel? Update(ComponentBindingModel model) { - using var context = new DiningRoomDatabase(); - var component = context.Components.FirstOrDefault(x => x.Id == model.Id); - if (component == null) + using var Context = new DiningRoomDatabase(); + + var ExistingComponent = Context.Components.FirstOrDefault(x => x.Id == model.Id); + if (ExistingComponent == null) { return null; } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; + + ExistingComponent.Update(model); + Context.SaveChanges(); + + return ExistingComponent.GetViewModel; } public ComponentViewModel? Delete(ComponentBindingModel model) { - using var context = new DiningRoomDatabase(); - var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + using var Context = new DiningRoomDatabase(); + + var ExistingComponent = Context.Components.FirstOrDefault(x => x.Id == model.Id); + if (ExistingComponent == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + + Context.Components.Remove(ExistingComponent); + Context.SaveChanges(); + + return ExistingComponent.GetViewModel; } + public List GetComponentsOrders(List Models) + { + using var Context = new DiningRoomDatabase(); + + return Context.Components + .Include(x => x.ProductComponents) + .ThenInclude(x => x.Product) + .ThenInclude(x => x.Order) + .Where(x => + Models.Select(x => x.Id).Contains(x.Id) // Компонент, указанный пользователем, + && x.ProductComponents.Any(y => y.Product.Order != null)) + .ToList() + .Select(x => new ReportComponentOrderViewModel + { + ComponentId = x.Id, + ComponentName = x.ComponentName, + ComponentCost = x.Cost, + Orders = x.ProductComponents + .Select(y => (y.Count, y.Product.ProductName, y.Product.Cost, y.Product.Order.DateCreate)) + .ToList(), + }) + .ToList(); + } + + public List GetComponentsByDate(ReportBindingModel ReportModel, UserSearchModel UserModel) + { + using var Context = new DiningRoomDatabase(); + + return Context.Components + .Where(c => c.UserId == UserModel.Id) + .Include(x => x.ProductComponents) + .ThenInclude(pc => pc.Product) + .Include(c => c.DrinkComponents) + .ThenInclude(ac => ac.Drink) + .ThenInclude(a => a.Cards.Where(r => r.DateCardCreate >= ReportModel.DateFrom && r.DateCardCreate <= ReportModel.DateTo)) + .ToList() + .SelectMany(c => c.DrinkComponents + .SelectMany(ac => ac.Drink.Cards, (ac, r) => new { ac, r }) + .SelectMany(temp => c.ProductComponents, (temp, pc) => new { temp.ac, temp.r, pc }) + .Select(temp => new ReportComponentByDateViewModel + { + ComponentId = c.Id, + ComponentName = c.ComponentName, + ComponentCost = c.Cost, + ProductId = temp.pc.Product.Id, + ProductName = temp.pc.Product.ProductName, + ProductPrice = temp.pc.Product.Cost, + CardId = temp.r.Id, + CardName = temp.r.CardName, + })) + .ToList(); + } + + + } } \ No newline at end of file diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs index fda76ec..928cb6d 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs @@ -9,49 +9,67 @@ namespace DiningRoomDatabaseImplement.Implements { public class ProductStorage : IProductStorage { - public List GetFullList() - { - using var context = new DiningRoomDatabase(); - return context.Products - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } + public List GetFullList() + { + using var Context = new DiningRoomDatabase(); - public List GetFilteredList(ProductSearchModel model) - { - if (string.IsNullOrEmpty(model.ProductName)) - { - return new(); - } - using var context = new DiningRoomDatabase(); - return context.Products - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.ProductName.Contains(model.ProductName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } + return Context.Products + .Include(x => x.Order) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Select(x => x.GetViewModel) + .ToList(); + } - public ProductViewModel? GetElement(ProductSearchModel model) + public List GetFilteredList(ProductSearchModel model) { - if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue) - { - return null; - } - using var context = new DiningRoomDatabase(); - return context.Products - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } + using var context = new DiningRoomDatabase(); + + // Optional search by order + if (model.OrderId.HasValue) + { + return context.Products + .Include(x => x.Order) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.UserId == model.UserId && x.OrderId == model.OrderId) + .Select(x => x.GetViewModel) + .ToList(); + } - public ProductViewModel? Insert(ProductBindingModel model) + return context.Products + .Include(x => x.Order) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel Model) + { + using var Context = new DiningRoomDatabase(); + + // ProductName is unique + if (!string.IsNullOrEmpty(Model.ProductName)) + { + return Context.Products + .Include(x => x.Order) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => x.ProductName == Model.ProductName)? + .GetViewModel; + } + + return Context.Products + .Include(x => x.Order) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => x.Id == Model.Id)? + .GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) { using var context = new DiningRoomDatabase(); var newProduct = Product.Create(context, model); diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs index 87ebbe9..5dfdb1a 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs @@ -7,6 +7,7 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -17,6 +18,11 @@ namespace DiningRoomDatabaseImplement.Models public int Id { get; set; } public int UserId { get; set; } public string CardName { get; set; } = string.Empty; + public int? DrinkId { get; set; } + + public virtual Drink? Drink { get; set; } = new(); + [Required] + public DateTime DateCardCreate { get; set; } public static Card Create(DiningRoomDatabase context, CardBindingModel model) { return new Card() diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs index b1ddf71..1cd2915 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs @@ -15,6 +15,7 @@ namespace DiningRoomDatabaseImplement.Models [Required] public string ComponentName { get; private set; } = string.Empty; + [Required] public string Unit { get; private set; } = string.Empty; [Required] @@ -25,9 +26,13 @@ namespace DiningRoomDatabaseImplement.Models [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); - public static Component Create(ComponentBindingModel Model) + public static Component? Create(ComponentBindingModel Model) { - return new() + if (Model == null) + { + return null; + } + return new Component() { Id = Model.Id, UserId = Model.UserId, @@ -36,10 +41,24 @@ namespace DiningRoomDatabaseImplement.Models Unit = Model.Unit, }; } - - public void Update(ComponentBindingModel Model) + public static Component Create(ComponentViewModel Model) + { + return new Component + { + Id = Model.Id, + UserId = Model.UserId, + ComponentName = Model.ComponentName, + Cost = Model.Cost, + Unit = Model.Unit, + }; + } + public void Update(ComponentBindingModel Model) { - ComponentName = Model.ComponentName; + if (Model == null) + { + return; + } + ComponentName = Model.ComponentName; Cost = Model.Cost; Unit = Model.Unit; } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs index e7da4c6..98f79af 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs @@ -21,8 +21,10 @@ namespace DiningRoomDatabaseImplement.Models public double Cost { get; private set; } public int CardId { get; private set; } + [ForeignKey("DrinkId")] + public virtual List Cards { get; set; } = new(); - [Required] + [Required] public string Category { get; private set; } = string.Empty; [ForeignKey("ComponentId")] diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs index 6f6f1f8..638a9ef 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs @@ -14,24 +14,22 @@ namespace DiningRoomDatabaseImplement.Models { public class Order : IOrderModel { - public int Id { get; set; } - [ForeignKey("ProductId")] - public int ProductId { get; set; } - [ForeignKey("UserId")] + public int Id { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public string ProductName { get; set; } = string.Empty; + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public virtual Product Product { get; set; } [Required] public int UserId { get; private set; } - public User User { get; set; } - public virtual Product Product { get; set; } - [Required] - public double Sum { get; private set; } - [Required] - public int Count { get; private set; } - - [Required] - public DateTime DateCreate { get; private set; } = DateTime.Now; - - [Required] - public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; public static Order? Create(OrderBindingModel model) { @@ -43,6 +41,7 @@ namespace DiningRoomDatabaseImplement.Models { Id = model.Id, ProductId = model.ProductId, + ProductName = model.ProductName, UserId = model.UserId, DateCreate = model.DateCreate, Status = model.Status, @@ -51,7 +50,7 @@ namespace DiningRoomDatabaseImplement.Models }; } - public void Update(OrderBindingModel model) + public void Update(OrderBindingModel? model) { if (model == null) { diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs index dd1f7f9..0df21d8 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs @@ -16,39 +16,41 @@ namespace DiningRoomDatabaseImplement.Models [Required] public string ProductName { get; set; } = string.Empty; + public int? OrderId { get; set; } - [Required] + public virtual Order? Order { get; set; } + [Required] public double Cost { get; set; } + private Dictionary? _productComponents; - [ForeignKey("ComponentId")] - public virtual List Components { get; set; } = new(); - - private Dictionary? _productComponents; - - [NotMapped] - public Dictionary ProductComponents - { - get - { - if (_productComponents == null) - { - _productComponents = Components.ToDictionary( - ProdComp => ProdComp.ComponentId, - ProdComp => (ProdComp.Component as IComponentModel, ProdComp.Count) - ); - } - - return _productComponents; - } - } + [NotMapped] + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components.ToDictionary( + ProdComp => ProdComp.ComponentId, + ProdComp => (ProdComp.Component as IComponentModel, ProdComp.Count) + ); + } + return _productComponents; + } + } + [ForeignKey("ProductId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ProductId")] + public virtual List Orders { get; set; } = new(); public static Product Create(DiningRoomDatabase Context, ProductBindingModel Model) { return new() { Id = Model.Id, UserId = Model.UserId, - ProductName = Model.ProductName, + OrderId = Model.OrderId, + ProductName = Model.ProductName, Cost = Model.Cost, Components = Model.ProductComponents.Select(x => new ProductComponent { @@ -59,12 +61,15 @@ namespace DiningRoomDatabaseImplement.Models } public void Update(ProductBindingModel model) { + OrderId = model.OrderId; ProductName = model.ProductName; Cost = model.Cost; } public ProductViewModel GetViewModel => new() { Id = Id, + UserId = UserId, + OrderId = OrderId, ProductName = ProductName, Cost = Cost, ProductComponents = ProductComponents