From 13c7187a2d1c74931e976a1418ec175d943f0e45 Mon Sep 17 00:00:00 2001 From: revengel66 Date: Tue, 7 May 2024 23:20:02 +0400 Subject: [PATCH] add client in order --- .../BusinessLogic/OrderLogic.cs | 4 ++ .../BindingModels/OrderBindingModel.cs | 1 + .../SearchModels/OrderSearchModel.cs | 1 + .../ViewModels/OrderViewModel.cs | 4 ++ .../PizzeriaDataModels/Models/IOrderModel.cs | 1 + .../Implements/OrderStorage.cs | 71 +++++++++++++++---- .../PizzeriaDatabaseImplement/Models/Order.cs | 5 ++ .../Implements/OrderStorage.cs | 28 +++++--- .../PizzeriaFileImplement/Models/Order.cs | 6 ++ .../PizzeriaListImplement/Models/Order.cs | 3 + 10 files changed, 98 insertions(+), 26 deletions(-) diff --git a/Pizzeria/PizzeriaBusinessLogic/BusinessLogic/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/BusinessLogic/OrderLogic.cs index 335d045..c9fa888 100644 --- a/Pizzeria/PizzeriaBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/BusinessLogic/OrderLogic.cs @@ -116,6 +116,10 @@ namespace PizzeriaBusinessLogic.BusinessLogic { return; } + if (model.ClientId <= 0) + { + throw new ArgumentNullException("Некорректный идентификатор клиента", nameof(model.ClientId)); + } if (model.PizzaId <= 0) { throw new ArgumentNullException("Некорректный идентификатор пиццы", nameof(model.PizzaId)); diff --git a/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs index 575d717..6f0cb64 100644 --- a/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs +++ b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs @@ -7,6 +7,7 @@ namespace PizzeriaContracts.BindingModels { public int Id { get; set; } public int PizzaId { get; set; } + public int ClientId { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs b/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs index cdb2ac5..f796587 100644 --- a/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs +++ b/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs @@ -3,6 +3,7 @@ public class OrderSearchModel { public int? Id { get; set; } + public int? ClientId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs index 1d71477..9d62c5b 100644 --- a/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs +++ b/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs @@ -12,6 +12,10 @@ namespace PizzeriaContracts.ViewModels [DisplayName("Пицца")] public string PizzaName { get; set; } = string.Empty; + public int ClientId { get; set; } + + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } diff --git a/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs b/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs index ed9a65e..3742d1e 100644 --- a/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs +++ b/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs @@ -5,6 +5,7 @@ namespace PizzeriaDataModels.Models public interface IOrderModel : IId { int PizzaId { get; } + int ClientId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs index 140b5cd..0460657 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs @@ -16,29 +16,57 @@ namespace PizzeriaDatabaseImplement.Implements return null; } using var context = new PizzeriaDatabase(); - return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue && !model.DateFrom.HasValue) - { - return new(); - } using var context = new PizzeriaDatabase(); - if (model.DateFrom.HasValue) + if (model.Id.HasValue) { - return context.Orders.Include(x => x.Pizza).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList(); + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } - return context.Orders.Include(x => x.Pizza).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + else if (model.DateFrom != null && model.DateTo != null) + { + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.ClientId.HasValue) + { + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); } public List GetFullList() { using var context = new PizzeriaDatabase(); - return context.Orders.Include(x => x.Pizza).Select(x => x.GetViewModel).ToList(); + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); } + public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -49,20 +77,28 @@ namespace PizzeriaDatabaseImplement.Implements using var context = new PizzeriaDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { using var context = new PizzeriaDatabase(); - var component = context.Orders.FirstOrDefault(x => x.Id == model.Id); - if (component == null) + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { return null; } - component.Update(model); + order.Update(model); context.SaveChanges(); - return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { @@ -70,9 +106,14 @@ namespace PizzeriaDatabaseImplement.Implements var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { + var deletedElement = context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return deletedElement; } return null; } diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs index 22a0856..e565677 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs @@ -12,6 +12,8 @@ namespace PizzeriaDatabaseImplement.Models [Required] public int PizzaId { get; private set; } [Required] + public int ClientId { get; set; } + [Required] public int Count { get; private set; } [Required] public double Sum { get; private set; } @@ -21,6 +23,7 @@ namespace PizzeriaDatabaseImplement.Models public DateTime DateCreate { get; private set; } public DateTime? DateImplement { get; private set; } public virtual Pizza Pizza { get; set; } + public virtual Client Client { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) @@ -31,6 +34,7 @@ namespace PizzeriaDatabaseImplement.Models { Id = model.Id, PizzaId = model.PizzaId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -52,6 +56,7 @@ namespace PizzeriaDatabaseImplement.Models { Id = Id, PizzaId = PizzaId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, diff --git a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs index 413d19b..40ea89a 100644 --- a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs @@ -21,7 +21,7 @@ namespace PizzeriaFileImplement.Implements { source.Orders.Remove(element); source.SaveOrders(); - return AddPizzaName(element.GetViewModel); + return AddData(element.GetViewModel); } return null; } @@ -36,25 +36,29 @@ namespace PizzeriaFileImplement.Implements { return null; } - return AddPizzaName(order.GetViewModel); + return AddData(order.GetViewModel); } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) + if (model.Id.HasValue) { - return new(); + return source.Orders.Where(x => x.Id == model.Id).Select(x => AddData(x.GetViewModel)).ToList(); } - if (model.DateFrom.HasValue) + else if (model.DateFrom.HasValue && model.DateTo.HasValue) { - return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => AddPizzaName(x.GetViewModel)).ToList(); + return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => AddData(x.GetViewModel)).ToList(); } - return source.Orders.Where(x => x.Id == model.Id).Select(x => AddPizzaName(x.GetViewModel)).ToList(); + else if (model.ClientId.HasValue) + { + return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => AddData(x.GetViewModel)).ToList(); + } + return new(); } public List GetFullList() { - return source.Orders.Select(x => AddPizzaName(x.GetViewModel)).ToList(); + return source.Orders.Select(x => AddData(x.GetViewModel)).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -66,7 +70,7 @@ namespace PizzeriaFileImplement.Implements } source.Orders.Add(newOrder); source.SaveOrders(); - return AddPizzaName(newOrder.GetViewModel); + return AddData(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) { @@ -77,13 +81,15 @@ namespace PizzeriaFileImplement.Implements } order.Update(model); source.SaveOrders(); - return AddPizzaName(order.GetViewModel); + return AddData(order.GetViewModel); } - private OrderViewModel AddPizzaName(OrderViewModel model) + private OrderViewModel AddData(OrderViewModel model) { var selectedPizza = source.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId); model.PizzaName = selectedPizza?.PizzaName; + var selectedClient = source.Clients.FirstOrDefault(x => x.Id == model.ClientId); + model.ClientFIO = selectedClient?.ClientFIO ?? string.Empty; return model; } diff --git a/Pizzeria/PizzeriaFileImplement/Models/Order.cs b/Pizzeria/PizzeriaFileImplement/Models/Order.cs index 80c8370..88c5ed5 100644 --- a/Pizzeria/PizzeriaFileImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaFileImplement/Models/Order.cs @@ -11,6 +11,8 @@ namespace PizzeriaFileImplement.Models public int PizzaId { get; private set; } + public int ClientId { get; private set; } + public int Count { get; private set; } public double Sum { get; private set; } @@ -31,6 +33,7 @@ namespace PizzeriaFileImplement.Models { Id = model.Id, PizzaId = model.PizzaId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -48,6 +51,7 @@ namespace PizzeriaFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), @@ -69,6 +73,7 @@ namespace PizzeriaFileImplement.Models { Id = Id, PizzaId = PizzaId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, @@ -79,6 +84,7 @@ namespace PizzeriaFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("PizzaId", PizzaId), + new XElement("ClientId", ClientId), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/Pizzeria/PizzeriaListImplement/Models/Order.cs b/Pizzeria/PizzeriaListImplement/Models/Order.cs index ec76f43..676e9dc 100644 --- a/Pizzeria/PizzeriaListImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaListImplement/Models/Order.cs @@ -11,6 +11,7 @@ namespace PizzeriaListImplement.Models public int PizzaId { get; private set; } + public int ClientId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -31,6 +32,7 @@ namespace PizzeriaListImplement.Models { Id = model.Id, PizzaId = model.PizzaId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -52,6 +54,7 @@ namespace PizzeriaListImplement.Models { Id = Id, PizzaId = PizzaId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status,