From a093ca253d759638155e5627385bd306b9b94dd7 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Sat, 25 Mar 2023 20:49:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/ClientSearchModel.cs | 8 +- .../DataFileSingleton.cs | 13 +- .../Implements/ClientStorage.cs | 53 +++++--- .../Models/Client.cs | 84 ++++++++++++ .../DataListSingleton.cs | 4 + .../Implements/ClientStorage.cs | 125 +++++++++++++++--- .../Models/Client.cs | 62 +++++++++ 7 files changed, 307 insertions(+), 42 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs index 31d7c40..eca7101 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs @@ -8,12 +8,12 @@ namespace BlacksmithWorkshopContracts.SearchModels { public class ClientSearchModel { - public int Id { get; set; } + public int? Id { get; set; } - public string ClientFIO { get; set; } + public string? ClientFIO { get; set; } - public string Email { get; set; } + public string? Email { get; set; } - public string Password { get; set; } + public string? Password { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs index b6628a3..0900792 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs @@ -18,13 +18,17 @@ namespace BlacksmithWorkshopFileImplement private readonly string ManufactureFileName = "Manufacture.xml"; - public List WorkPieces { get; private set; } + private readonly string ClientFileName = "Client.xml"; + + public List WorkPieces { get; private set; } public List Orders { get; private set; } public List Manufactures { get; private set; } - public static DataFileSingleton GetInstance() + public List Clients { get; private set; } + + public static DataFileSingleton GetInstance() { if (instance == null) { @@ -40,11 +44,14 @@ namespace BlacksmithWorkshopFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - private DataFileSingleton() + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + + private DataFileSingleton() { WorkPieces = LoadData(WorkPieceFileName, "WorkPiece", x => WorkPiece.Create(x)!)!; Manufactures = LoadData(ManufactureFileName, "Manufacture", x => Manufacture.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/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs index 8c81a04..cce5138 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs @@ -2,6 +2,7 @@ using BlacksmithWorkshopContracts.SearchModels; using BlacksmithWorkshopContracts.StoragesContracts; using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; @@ -12,27 +13,44 @@ namespace BlacksmithWorkshopFileImplement.Implements { public class ClientStorage : IClientStorage { - public ClientViewModel? Delete(ClientBindingModel model) + private readonly DataFileSingleton source; + + public ClientStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Clients.Select(x => GetViewModel(x)).ToList(); + } + + public List GetFilteredList(ClientSearchModel model) { throw new NotImplementedException(); } - public ClientViewModel? GetElement(ClientSearchModel model) - { - throw new NotImplementedException(); - } + public ClientViewModel? GetElement(ClientSearchModel model) + { + throw new NotImplementedException(); + } - public List GetFilteredList(ClientSearchModel model) - { - throw new NotImplementedException(); - } + //для загрузки названий изделия в заказе + private ClientViewModel GetViewModel(Client client) + { + var viewModel = order.GetViewModel; - public List GetFullList() - { - throw new NotImplementedException(); - } + var manufacture = source.Manufactures.FirstOrDefault(x => x.Id == order.ManufactureId); - public ClientViewModel? Insert(ClientBindingModel model) + if (manufacture != null) + { + viewModel.ManufactureName = manufacture.ManufactureName; + } + + return viewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) { throw new NotImplementedException(); } @@ -41,5 +59,10 @@ namespace BlacksmithWorkshopFileImplement.Implements { throw new NotImplementedException(); } - } + + public ClientViewModel? Delete(ClientBindingModel model) + { + throw new NotImplementedException(); + } + } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs new file mode 100644 index 0000000..85bff7a --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs @@ -0,0 +1,84 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Enums; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace BlacksmithWorkshopFileImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + + public string ClientFIO { get; private set; } = string.Empty; + + public string Email { get; private set; } = string.Empty; + + public string Password { get; private 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("Order", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password)); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs index e2b2a4c..c19f551 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs @@ -21,11 +21,15 @@ namespace BlacksmithWorkshopListImplement //список для хранения заказов public List Orders { get; set; } + //список для хранения клиентов + public List Clients { get; set; } + public DataListSingleton() { WorkPieces = new List(); Manufactures = new List(); Orders = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs index 4d34d6d..7e39b39 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs @@ -2,6 +2,7 @@ using BlacksmithWorkshopContracts.SearchModels; using BlacksmithWorkshopContracts.StoragesContracts; using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopListImplement.Models; using System; using System.Collections.Generic; using System.Linq; @@ -12,34 +13,118 @@ namespace BlacksmithWorkshopListImplement.Implements { public class ClientStorage : IClientStorage { - public ClientViewModel? Delete(ClientBindingModel model) + //поле для работы со списком изделий + private readonly DataListSingleton _source; + + public ClientStorage() { - throw new NotImplementedException(); + _source = DataListSingleton.GetInstance(); } - public ClientViewModel? GetElement(ClientSearchModel model) - { - throw new NotImplementedException(); - } + public List GetFullList() + { + var result = new List(); - public List GetFilteredList(ClientSearchModel model) - { - throw new NotImplementedException(); - } + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } - public List GetFullList() - { - throw new NotImplementedException(); - } + return result; + } - public ClientViewModel? Insert(ClientBindingModel model) + public List GetFilteredList(ClientSearchModel model) { - throw new NotImplementedException(); - } + var result = new List(); + + if (string.IsNullOrEmpty(model.Email)) + { + return result; + } + + foreach (var client in _source.Clients) + { + if (client.Email.Contains(model.Email)) + { + result.Add(client.GetViewModel); + } + } + + return result; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + + foreach (var client in _source.Clients) + { + if ((!string.IsNullOrEmpty(model.Email) && client.Email == model.Email) || + (model.Id.HasValue && client.Id == model.Id)) + { + 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) { - throw new NotImplementedException(); - } - } + 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/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs new file mode 100644 index 0000000..c27a8e5 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs @@ -0,0 +1,62 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Models +{ + public class Client : IClientModel + { + //методы set делаем приватным, чтобы исключить неразрешённые манипуляции + public int Id { get; private set; } + + public string ClientFIO { get; private set; } = string.Empty; + + public string Email { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + //метод для создания объекта от класса-компонента на основе класса-BindingModel + 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 void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + //метод для создания объекта класса ViewModel на основе данных объекта класса-компонента + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +}