From 23c9d3a280d6fcc524db87a3e9ad960e41a9f9d6 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Sun, 19 May 2024 17:58:47 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 4 + .../Implements/ClientStorage.cs | 78 +++++++++++++++ .../Implements/OrderStorage.cs | 58 +++++++---- .../SushiBarFileImplement/Models/Client.cs | 66 +++++++++++++ .../SushiBarFileImplement/Models/Order.cs | 9 +- .../DataListSingleton.cs | 2 + .../Implements/ClientStorage.cs | 99 +++++++++++++++++++ .../Implements/OrderStorage.cs | 32 +++--- .../SushiBarListImplement/Models/Client.cs | 67 +++++++++++++ .../SushiBarListImplement/Models/Order.cs | 3 + 10 files changed, 380 insertions(+), 38 deletions(-) create mode 100644 SushiBar/SushiBarFileImplement/Implements/ClientStorage.cs create mode 100644 SushiBar/SushiBarFileImplement/Models/Client.cs create mode 100644 SushiBar/SushiBarListImplement/Implements/ClientStorage.cs create mode 100644 SushiBar/SushiBarListImplement/Models/Client.cs diff --git a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs index 7c23c6c..aba69c7 100644 --- a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs +++ b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs @@ -10,9 +10,11 @@ namespace SushiBarFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string SushiFileName = "Sushi.xml"; + private readonly string ClientFileName = "Sushi.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Sushis { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -24,11 +26,13 @@ namespace SushiBarFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Client", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/SushiBar/SushiBarFileImplement/Implements/ClientStorage.cs b/SushiBar/SushiBarFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..9950acb --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,78 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.Implements +{ + public class ClientStorage + { + private readonly DataFileSingleton source; + public ClientStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)) + { + return new(); + } + return source.Clients + .Where(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) || x.ClientFIO.Contains(model.Email) && + string.IsNullOrEmpty(model.Password) || x.ClientFIO.Contains(model.Password))) + .Select(x => x.GetViewModel) + .ToList(); + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + return source.Clients + .FirstOrDefault(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && + (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password)) + ?.GetViewModel; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => + x.Id) + 1 : 1; + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + source.Clients.Add(newClient); + source.SaveClients(); + return newClient.GetViewModel; + } + public ClientViewModel? Update(ClientBindingModel model) + { + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + source.SaveClients(); + return client.GetViewModel; + } + public ClientViewModel? Delete(ClientBindingModel model) + { + var element = source.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + source.Clients.Remove(element); + source.SaveClients(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs index d1e2b75..18f9f47 100644 --- a/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs @@ -16,21 +16,18 @@ namespace SushiBarFileImplement.Implements public List GetFullList() { return source.Orders - .Select(x => AttachSushiName(x.GetViewModel)) + .Select(x => AccessClientStorage(AccessSushiStorage(x.GetViewModel))!) .ToList(); } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return new(); - } return source.Orders - .Where(o => - (model.Id.HasValue && o.Id == model.Id) || - (model.DateFrom.HasValue && model.DateTo.HasValue && - model.DateFrom < o.DateCreate && o.DateCreate < model.DateTo)) - .Select(x => AttachSushiName(x.GetViewModel)) + .Where(o => + (!model.Id.HasValue || o.Id == model.Id) && + (!model.DateFrom.HasValue || o.DateCreate >= model.DateFrom) && + (!model.DateTo.HasValue || o.DateCreate <= model.DateTo) && + (!model.ClientId.HasValue || o.ClientId == model.ClientId)) + .Select(o => AccessClientStorage(AccessSushiStorage(o.GetViewModel))!) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -39,8 +36,9 @@ namespace SushiBarFileImplement.Implements { return null; } - return AttachSushiName(source.Orders - .FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel); + return AccessClientStorage(AccessSushiStorage( + source.Orders + .FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel)); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -52,18 +50,18 @@ namespace SushiBarFileImplement.Implements } source.Orders.Add(newOrder); source.SaveOrders(); - return AttachSushiName(newOrder.GetViewModel); + return AccessClientStorage(AccessSushiStorage(newOrder.GetViewModel)); } public OrderViewModel? Update(OrderBindingModel model) { - var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order == null) + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element == null) { return null; } - order.Update(model); + element.Update(model); source.SaveOrders(); - return AttachSushiName(order.GetViewModel); + return AccessClientStorage(AccessSushiStorage(element.GetViewModel)); } public OrderViewModel? Delete(OrderBindingModel model) { @@ -72,14 +70,32 @@ namespace SushiBarFileImplement.Implements { source.Orders.Remove(element); source.SaveOrders(); - return AttachSushiName(element.GetViewModel); + return AccessClientStorage(AccessSushiStorage(element.GetViewModel)); } return null; } - private OrderViewModel AttachSushiName(OrderViewModel ?model) + public OrderViewModel? AccessSushiStorage(OrderViewModel? model) { - if (model == null) { return null; } - model.SushiName = source.Sushis.First(x => x.Id == model.SushiId).SushiName; + if (model == null) + return null; + foreach (var iceCream in source.Sushis) + { + if (iceCream.Id == model.SushiId) + { + model.SushiName = iceCream.SushiName; + break; + } + } + return model; + } + + public OrderViewModel? AccessClientStorage(OrderViewModel? model) + { + if (model == null) + return null; + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client != null) + model.ClientFIO = client.ClientFIO; return model; } } diff --git a/SushiBar/SushiBarFileImplement/Models/Client.cs b/SushiBar/SushiBarFileImplement/Models/Client.cs new file mode 100644 index 0000000..26bd8e5 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Client.cs @@ -0,0 +1,66 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using System.Xml.Linq; + +namespace SushiBarFileImplement.Models +{ + public class Client + { + public int Id { get; private set; } + public string ClientFIO { get; private set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + public static Client? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Client() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("ClientFIO")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value + }; + } + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email.ToString()), + new XElement("Password", Password.ToString()) + ); + } +} diff --git a/SushiBar/SushiBarFileImplement/Models/Order.cs b/SushiBar/SushiBarFileImplement/Models/Order.cs index 44de8ef..69a9fc1 100644 --- a/SushiBar/SushiBarFileImplement/Models/Order.cs +++ b/SushiBar/SushiBarFileImplement/Models/Order.cs @@ -2,11 +2,6 @@ using SushiBarContracts.ViewModels; using SushiBarDataModels.Enums; using SushiBarDataModels.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Xml.Linq; namespace SushiBarFileImplement.Models @@ -31,6 +26,7 @@ namespace SushiBarFileImplement.Models { Id = model.Id, SushiId = model.SushiId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -48,6 +44,7 @@ namespace SushiBarFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), SushiId = Convert.ToInt32(element.Element("SushiId")!.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.ToString()), @@ -69,6 +66,7 @@ namespace SushiBarFileImplement.Models { Id = Id, SushiId = SushiId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, @@ -78,6 +76,7 @@ namespace SushiBarFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("SushiId", SushiId), + new XElement("ClientId", ClientId), new XElement("Count", Count), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/SushiBar/SushiBarListImplement/DataListSingleton.cs b/SushiBar/SushiBarListImplement/DataListSingleton.cs index 3df79fa..97217d2 100644 --- a/SushiBar/SushiBarListImplement/DataListSingleton.cs +++ b/SushiBar/SushiBarListImplement/DataListSingleton.cs @@ -8,11 +8,13 @@ namespace SushiBarListImplement public List Components { get; set; } public List Orders { get; set; } public List Sushis { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Sushis = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() diff --git a/SushiBar/SushiBarListImplement/Implements/ClientStorage.cs b/SushiBar/SushiBarListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..e4c3ae2 --- /dev/null +++ b/SushiBar/SushiBarListImplement/Implements/ClientStorage.cs @@ -0,0 +1,99 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarListImplement.Models; + +namespace SushiBarListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataListSingleton _source; + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } + public List GetFilteredList(ClientSearchModel + model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)) + { + return result; + } + foreach (var client in _source.Clients) + { + if (client.ClientFIO.Contains(model.ClientFIO)) + { + result.Add(client.GetViewModel); + } + } + return result; + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + foreach (var client in _source.Clients) + { + if ((string.IsNullOrEmpty(model.ClientFIO) || client.ClientFIO == model.ClientFIO) && + (!model.Id.HasValue || client.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || client.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || client.Password == model.Password)) + { + return client.GetViewModel; + } + } + return null; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = 1; + foreach (var client in _source.Clients) + { + if (model.Id <= client.Id) + { + model.Id = client.Id + 1; + } + } + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + _source.Clients.Add(newClient); + return newClient.GetViewModel; + } + public ClientViewModel? Update(ClientBindingModel model) + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + client.Update(model); + return client.GetViewModel; + } + } + return null; + } + public ClientViewModel? Delete(ClientBindingModel model) + { + for (int i = 0; i < _source.Clients.Count; ++i) + { + if (_source.Clients[i].Id == model.Id) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/SushiBar/SushiBarListImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarListImplement/Implements/OrderStorage.cs index 200d951..c285af1 100644 --- a/SushiBar/SushiBarListImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarListImplement/Implements/OrderStorage.cs @@ -18,7 +18,7 @@ namespace SushiBarListImplement.Implements var result = new List(); foreach (var order in _source.Orders) { - result.Add(AttachSushiName(order.GetViewModel)); + result.Add(AccessClientStorage(AccessSushiStorage(order.GetViewModel))); } return result; } @@ -29,9 +29,9 @@ namespace SushiBarListImplement.Implements { if (order.Id == model.Id || (model.DateFrom.HasValue && model.DateTo.HasValue && - model.DateFrom < order.DateCreate && order.DateCreate < model.DateTo))) + model.DateFrom < order.DateCreate && order.DateCreate < model.DateTo)) { - result.Add(AttachSushiName(order.GetViewModel)); + result.Add(AccessClientStorage(AccessSushiStorage(order.GetViewModel))); } } return result; @@ -46,7 +46,7 @@ namespace SushiBarListImplement.Implements { if (model.Id.HasValue && order.Id == model.Id) { - return AttachSushiName(order.GetViewModel); + return AccessClientStorage(AccessSushiStorage(order.GetViewModel)); } } return null; @@ -67,7 +67,7 @@ namespace SushiBarListImplement.Implements return null; } _source.Orders.Add(newOrder); - return AttachSushiName(newOrder.GetViewModel); + return AccessClientStorage(AccessSushiStorage(newOrder.GetViewModel)); } public OrderViewModel? Update(OrderBindingModel model) { @@ -76,7 +76,7 @@ namespace SushiBarListImplement.Implements if (order.Id == model.Id) { order.Update(model); - return AttachSushiName(order.GetViewModel); + return AccessClientStorage(AccessSushiStorage(order.GetViewModel)); } } return null; @@ -89,22 +89,30 @@ namespace SushiBarListImplement.Implements { var element = _source.Orders[i]; _source.Orders.RemoveAt(i); - return AttachSushiName(element.GetViewModel); + return AccessClientStorage(AccessSushiStorage(element.GetViewModel)); } } return null; } - private OrderViewModel AttachSushiName(OrderViewModel model) + public OrderViewModel AccessSushiStorage(OrderViewModel model) { - foreach (var sushi in _source.Sushis) + foreach (var iceCream in _source.Sushis) { - if (sushi.Id == model.SushiId) + if (iceCream.Id == model.SushiId) { - model.SushiName = sushi.SushiName; - return model; + model.SushiName = iceCream.SushiName; + break; } } return model; } + + public OrderViewModel AccessClientStorage(OrderViewModel model) + { + var client = _source.Clients.FirstOrDefault(x => x.Id == model.ClientId); + if (client != null) + model.ClientFIO = client.ClientFIO; + return model; + } } } diff --git a/SushiBar/SushiBarListImplement/Models/Client.cs b/SushiBar/SushiBarListImplement/Models/Client.cs new file mode 100644 index 0000000..5990bc9 --- /dev/null +++ b/SushiBar/SushiBarListImplement/Models/Client.cs @@ -0,0 +1,67 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.Xml.Linq; + +namespace SushiBarListImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + public string ClientFIO { get; private set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + public static Client? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Client() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("ClientFIO")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value + }; + } + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email.ToString()), + new XElement("Password", Password.ToString()) + ); + } +} diff --git a/SushiBar/SushiBarListImplement/Models/Order.cs b/SushiBar/SushiBarListImplement/Models/Order.cs index b5668ee..2d9c126 100644 --- a/SushiBar/SushiBarListImplement/Models/Order.cs +++ b/SushiBar/SushiBarListImplement/Models/Order.cs @@ -29,6 +29,7 @@ namespace SushiBarListImplement.Models { Id = model.Id, SushiId = model.SushiId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -43,6 +44,7 @@ namespace SushiBarListImplement.Models return; } SushiId = model.SushiId; + ClientId = model.ClientId; Count = model.Count; Sum = model.Sum; Status = model.Status; @@ -52,6 +54,7 @@ namespace SushiBarListImplement.Models { Id = Id, SushiId = SushiId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status,