From 3e7ccbdc6222532883893c0d8cfcdc1e373ad800 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Thu, 11 Apr 2024 00:57:19 +0400 Subject: [PATCH] File implement --- .../DataFileSingleton.cs | 9 +- .../Implements/ClientStorage.cs | 84 +++++++++++++++++++ .../Implements/OrderStorage.cs | 11 ++- AutoWorkshopFileImplement/Models/Client.cs | 72 ++++++++++++++++ AutoWorkshopFileImplement/Models/Order.cs | 6 ++ 5 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 AutoWorkshopFileImplement/Implements/ClientStorage.cs create mode 100644 AutoWorkshopFileImplement/Models/Client.cs diff --git a/AutoWorkshopFileImplement/DataFileSingleton.cs b/AutoWorkshopFileImplement/DataFileSingleton.cs index 266f506..f638cc5 100644 --- a/AutoWorkshopFileImplement/DataFileSingleton.cs +++ b/AutoWorkshopFileImplement/DataFileSingleton.cs @@ -10,18 +10,22 @@ namespace AutoWorkshopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string RepairFileName = "Repair.xml"; - + private readonly string ClientFileName = "Client.xml"; + public List Components { get; private set; } public List Orders { get; private set; } public List Repairs { get; private set; } - + + public List Clients { get; private set; } + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Repairs = LoadData(RepairFileName, "Repair", x => Repair.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } public static DataFileSingleton GetInstance() @@ -37,6 +41,7 @@ namespace AutoWorkshopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveRepairs() => SaveData(Repairs, RepairFileName, "Repairs", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); private static List? LoadData(string FileName, string XmlNodeName, Func SelectFunction) { diff --git a/AutoWorkshopFileImplement/Implements/ClientStorage.cs b/AutoWorkshopFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..6b9fa6c --- /dev/null +++ b/AutoWorkshopFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,84 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopFileImplement.Models; + +namespace AutoWorkshopFileImplement.Implements +{ + public class ClientStorage : IClientStorage + { + 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.Email.Contains(Model.Email)) && + (string.IsNullOrEmpty(Model.Password) || x.Password.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 Client = _source.Clients.FirstOrDefault(rec => rec.Id == Model.Id); + if (Client == null) + return null; + + _source.Clients.Remove(Client); + _source.SaveClients(); + + return Client.GetViewModel; + } + } +} diff --git a/AutoWorkshopFileImplement/Implements/OrderStorage.cs b/AutoWorkshopFileImplement/Implements/OrderStorage.cs index ec98ded..90263f0 100644 --- a/AutoWorkshopFileImplement/Implements/OrderStorage.cs +++ b/AutoWorkshopFileImplement/Implements/OrderStorage.cs @@ -26,7 +26,8 @@ namespace AutoWorkshopFileImplement.Implements return new(); return _source.Orders - .Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo) + .Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo && + (!Model.ClientId.HasValue || x.ClientId == Model.ClientId)) .Select(x => AddRepairName(x.GetViewModel)) .ToList(); } @@ -94,5 +95,13 @@ namespace AutoWorkshopFileImplement.Implements Model.RepairName = SelectedRepair?.RepairName ?? string.Empty; return Model; } + + public OrderViewModel AddClientFIO(OrderViewModel Model) + { + var SelectedClient = _source.Clients.FirstOrDefault(x => x.Id == Model.Id); + + Model.ClientFIO = SelectedClient?.ClientFIO ?? string.Empty; + return Model; + } } } diff --git a/AutoWorkshopFileImplement/Models/Client.cs b/AutoWorkshopFileImplement/Models/Client.cs new file mode 100644 index 0000000..b27a7a9 --- /dev/null +++ b/AutoWorkshopFileImplement/Models/Client.cs @@ -0,0 +1,72 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; +using System.Xml.Linq; + +namespace AutoWorkshopFileImplement.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/AutoWorkshopFileImplement/Models/Order.cs b/AutoWorkshopFileImplement/Models/Order.cs index 97a67fd..808823d 100644 --- a/AutoWorkshopFileImplement/Models/Order.cs +++ b/AutoWorkshopFileImplement/Models/Order.cs @@ -12,6 +12,8 @@ namespace AutoWorkshopFileImplement.Models public int RepairId { 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 AutoWorkshopFileImplement.Models { Id = Model.Id, RepairId = Model.RepairId, + ClientId = Model.ClientId, Count = Model.Count, Sum = Model.Sum, Status = Model.Status, @@ -48,6 +51,7 @@ namespace AutoWorkshopFileImplement.Models { Id = Convert.ToInt32(Element.Attribute("Id")!.Value), RepairId = Convert.ToInt32(Element.Element("RepairId")!.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 AutoWorkshopFileImplement.Models { Id = Id, RepairId = RepairId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, @@ -80,6 +85,7 @@ namespace AutoWorkshopFileImplement.Models "Order", new XAttribute("Id", Id), new XElement("RepairId", RepairId), + new XElement("ClientId", ClientId), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()),