From 6d669290c5afeb6b43d43fa81f0a16b129488296 Mon Sep 17 00:00:00 2001 From: revengel66 Date: Fri, 21 Jun 2024 15:37:13 +0400 Subject: [PATCH] 6 laba storages --- .../Implements/OrderStorage.cs | 70 ++++++++++++++++--- .../PizzeriaDatabase.cs | 1 + .../DataFileSingleton.cs | 4 ++ .../Implements/OrderStorage.cs | 17 +++-- .../PizzeriaFileImplement/Models/Order.cs | 6 +- .../DataListSingleton.cs | 3 + .../PizzeriaListImplement/Models/Order.cs | 3 + 7 files changed, 87 insertions(+), 17 deletions(-) diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs index 527e0e7..f81c544 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs @@ -4,8 +4,6 @@ using PizzeriaContracts.ViewModels; using PizzeriaDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; using PizzeriaContracts.StorageContracts; -using System.Collections.Generic; -using System.Linq; namespace PizzeriaDatabaseImplement.Implements { @@ -27,13 +25,41 @@ namespace PizzeriaDatabaseImplement.Implements using var context = new PizzeriaDatabase(); if (model.DateFrom.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) + .Include(x => x.Implementer) + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); } if (model.ClientId.HasValue) { - return context.Orders.Include(x => x.Pizza).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.ClientId == model.ClientId) + .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.ImplementerId.HasValue) + { + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.ImplementerId == model.ImplementerId) + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -43,7 +69,15 @@ namespace PizzeriaDatabaseImplement.Implements return new(); } using var context = new PizzeriaDatabase(); - 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) + .Include(x => x.Implementer) + .FirstOrDefault(x => + model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId && model.Status != null && x.Status.Equals(model.Status) + || model.Status == null && model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId + || model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) @@ -58,7 +92,12 @@ namespace PizzeriaDatabaseImplement.Implements } context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) @@ -69,9 +108,14 @@ namespace PizzeriaDatabaseImplement.Implements { return null; } - order.Update(model); + order.Update(context,model); context.SaveChanges(); - return order.GetViewModel; + return context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) @@ -80,9 +124,15 @@ namespace PizzeriaDatabaseImplement.Implements var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (order != null) { + var deletedElement = context.Orders + .Include(x => x.Pizza) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; context.Orders.Remove(order); context.SaveChanges(); - return order.GetViewModel; + return deletedElement; } return null; } diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs index b6361d7..a76d949 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -18,6 +18,7 @@ namespace PizzeriaDatabaseImplement public virtual DbSet PizzaComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } + public virtual DbSet Implementers { set; get; } } } diff --git a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs index 54025c2..ba9325b 100644 --- a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs @@ -10,16 +10,19 @@ namespace PizzeriaFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string PizzaFileName = "Pizza.xml"; private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pizzas { get; private set; } public List Clients { get; private set; } + public List Implementers { get; private set; } private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Pizzas = LoadData(PizzaFileName, "Pizza", x => Pizza.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; } public static DataFileSingleton GetInstance() { @@ -34,6 +37,7 @@ namespace PizzeriaFileImplement public void SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); private static List? LoadData(string filename, string xmlNodeName,Func selectFunction) { diff --git a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs index 40ea89a..d33c14c 100644 --- a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs @@ -31,12 +31,11 @@ namespace PizzeriaFileImplement.Implements { return null; } - var order = source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)); - if (order == null) - { - return null; - } - return AddData(order.GetViewModel); + var order = source.Orders.FirstOrDefault(x => + model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId && model.Status != null && x.Status.Equals(model.Status) + || model.Status == null && model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId + || model.Id.HasValue && x.Id == model.Id); + return order?.GetViewModel != null ? AddData(order.GetViewModel) : null; } public List GetFilteredList(OrderSearchModel model) @@ -53,6 +52,11 @@ namespace PizzeriaFileImplement.Implements { return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => AddData(x.GetViewModel)).ToList(); } + if (!model.ImplementerId.HasValue && !model.Id.HasValue) + { + return source.Orders.Where(x => x.ImplementerId == model.ImplementerId).Select(x => x.GetViewModel).ToList(); + + } return new(); } @@ -60,6 +64,7 @@ namespace PizzeriaFileImplement.Implements { return source.Orders.Select(x => AddData(x.GetViewModel)).ToList(); } + public OrderViewModel? Insert(OrderBindingModel model) { model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; diff --git a/Pizzeria/PizzeriaFileImplement/Models/Order.cs b/Pizzeria/PizzeriaFileImplement/Models/Order.cs index 88c5ed5..8475342 100644 --- a/Pizzeria/PizzeriaFileImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaFileImplement/Models/Order.cs @@ -12,7 +12,7 @@ namespace PizzeriaFileImplement.Models public int PizzaId { get; private set; } public int ClientId { get; private set; } - + public int? ImplementerId { get; set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -33,6 +33,7 @@ namespace PizzeriaFileImplement.Models { Id = model.Id, PizzaId = model.PizzaId, + ImplementerId = model.ImplementerId, ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, @@ -51,6 +52,7 @@ namespace PizzeriaFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), @@ -74,6 +76,7 @@ namespace PizzeriaFileImplement.Models Id = Id, PizzaId = PizzaId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status, @@ -85,6 +88,7 @@ namespace PizzeriaFileImplement.Models new XAttribute("Id", Id), new XElement("PizzaId", PizzaId), new XElement("ClientId", ClientId), + new XElement("ImplementerId", ImplementerId), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs index 0325445..366111a 100644 --- a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs +++ b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs @@ -9,11 +9,14 @@ namespace PizzeriaListImplement public List Orders { get; set; } public List Pizzas { get; set; } public List Clients { get; set; } + public List Implementers { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pizzas = new List(); + Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() { diff --git a/Pizzeria/PizzeriaListImplement/Models/Order.cs b/Pizzeria/PizzeriaListImplement/Models/Order.cs index 676e9dc..ec6ddd1 100644 --- a/Pizzeria/PizzeriaListImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaListImplement/Models/Order.cs @@ -12,6 +12,7 @@ namespace PizzeriaListImplement.Models public int PizzaId { get; private set; } public int ClientId { get; private set; } + public int? ImplementerId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -33,6 +34,7 @@ namespace PizzeriaListImplement.Models Id = model.Id, PizzaId = model.PizzaId, ClientId = model.ClientId, + ImplementerId = model.ImplementerId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -55,6 +57,7 @@ namespace PizzeriaListImplement.Models Id = Id, PizzaId = PizzaId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status,