From b41ff6cc25b7b4fee1b543e8b40e06cfe8cec702 Mon Sep 17 00:00:00 2001 From: kamilia Date: Sun, 5 May 2024 23:57:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=B0=D0=BA=D0=BE=D0=B9=20=D1=82=D0=BE?= =?UTF-8?q?=20=D1=82=D1=80=D0=B5=D1=88=20=D0=BF=D1=80=D0=BE=D0=B8=D1=81?= =?UTF-8?q?=D1=85=D0=BE=D0=B4=D0=B8=D1=82=20=D1=8F=20=D1=83=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftPlantDataModels/IOrderModel.cs | 5 + .../AircraftPlantFileImplement/Client.cs | 77 +++++++++++++++ .../ClientStorage.cs | 97 +++++++++++++++++++ .../DataFileSingleton.cs | 4 + .../OrderStorage.cs | 2 +- 5 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 AircraftPlant/AircraftPlantFileImplement/Client.cs create mode 100644 AircraftPlant/AircraftPlantFileImplement/ClientStorage.cs diff --git a/AircraftPlant/AircraftPlantDataModels/IOrderModel.cs b/AircraftPlant/AircraftPlantDataModels/IOrderModel.cs index 5e60ef2..dc17f38 100644 --- a/AircraftPlant/AircraftPlantDataModels/IOrderModel.cs +++ b/AircraftPlant/AircraftPlantDataModels/IOrderModel.cs @@ -17,6 +17,11 @@ namespace AircraftPlantDataModels.Models /// int PlaneId { get; } + /// + /// Идентификатор клиента + /// + int ClientId { get; } + /// /// Количество изделий /// diff --git a/AircraftPlant/AircraftPlantFileImplement/Client.cs b/AircraftPlant/AircraftPlantFileImplement/Client.cs new file mode 100644 index 0000000..9390971 --- /dev/null +++ b/AircraftPlant/AircraftPlantFileImplement/Client.cs @@ -0,0 +1,77 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AircraftPlantFileImplement +{ + public class Client : IClientModel + { + public int Id { get; set; } + public string ClientFIO { get; 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.Attribute("ClientFIO")!.Value, + Email = element.Attribute("Email")!.Value, + Password = element.Attribute("Password")!.Value + }; + } + + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + + ClientFIO = model.ClientFIO; + Password = model.Password; + Email = model.Email; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Password = Password, + Email = Email + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password)); + } +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantFileImplement/ClientStorage.cs b/AircraftPlant/AircraftPlantFileImplement/ClientStorage.cs new file mode 100644 index 0000000..66b8717 --- /dev/null +++ b/AircraftPlant/AircraftPlantFileImplement/ClientStorage.cs @@ -0,0 +1,97 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantFileImplement +{ + 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.Email)) + { + return new(); + } + + return _source.Clients + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + + return _source.Clients + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && + x.Email == model.Email) || + (model.Id.HasValue && x.Id == model.Id)) + ?.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(x => x.Id == model.Id); + if (element != null) + { + _source.Clients.Remove(element); + _source.SaveClients(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs b/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs index 4d2ea23..fac96aa 100644 --- a/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs +++ b/AircraftPlant/AircraftPlantFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace AircraftPlantFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PlaneFileName = "Plane.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Planes { get; private set; } + public List Clients { get; set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -28,11 +30,13 @@ namespace AircraftPlantFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePlanes() => SaveData(Planes, PlaneFileName, "Planes", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Planes = LoadData(PlaneFileName, "Plane", x => Plane.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/AircraftPlant/AircraftPlantFileImplement/OrderStorage.cs b/AircraftPlant/AircraftPlantFileImplement/OrderStorage.cs index ca152f7..959eb3a 100644 --- a/AircraftPlant/AircraftPlantFileImplement/OrderStorage.cs +++ b/AircraftPlant/AircraftPlantFileImplement/OrderStorage.cs @@ -24,7 +24,7 @@ namespace AircraftPlantFileImplement.Implements } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue || !model.DateFrom.HasValue || !model.DateTo.HasValue || !model.ClientId.HasValue) { return new(); }