From b4081cb9115779d7361553464f7a1630c7ac669b Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Sun, 21 Apr 2024 14:04:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D0=B0=D1=8E=D1=89=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=9A=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 7 +- .../Implements/ClientStorage.cs | 85 ++++++++++++++ .../Implements/OrderStorage.cs | 17 ++- .../Models/Client.cs | 72 ++++++++++++ .../Models/Order.cs | 4 + .../DataListSingleton.cs | 2 + .../Implements/ClientStorage.cs | 104 ++++++++++++++++++ .../Implements/OrderStorage.cs | 59 +++++----- .../Models/Client.cs | 50 +++++++++ .../Models/Order.cs | 2 + 10 files changed, 369 insertions(+), 33 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ClientStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Models/Client.cs create mode 100644 SoftwareInstallation/SoftwareInstallationListImplement/Implements/ClientStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationListImplement/Models/Client.cs diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs index 6c1a577..70a0c44 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace SoftwareInstallationFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PackageFileName = "Package.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Packages { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -28,18 +30,19 @@ namespace SoftwareInstallationFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", 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)!)!; Packages = LoadData(PackageFileName, "Package", x => Package.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) { if (File.Exists(filename)) { - return - XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); } return new List(); } diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ClientStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..7fad79b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,85 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationFileImplement.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.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/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs index f68c384..9accc80 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs @@ -6,6 +6,7 @@ using SoftwareInstallationFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -50,16 +51,18 @@ namespace SoftwareInstallationFileImplement.Implements return source.Orders.Where(x => ( (!model.Id.HasValue || x.Id == model.Id) && (!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) && - (!model.DateTo.HasValue || x.DateCreate <= model.DateTo) + (!model.DateTo.HasValue || x.DateCreate <= model.DateTo) && + (!model.ClientId.HasValue || x.ClientId == model.ClientId) ) ) .Select(x => GetPackageName(x.GetViewModel)) + .Select(GetClientName) .ToList(); } public List GetFullList() { - return source.Orders.Select(x => GetPackageName(x.GetViewModel)).ToList(); + return source.Orders.Select(x => GetPackageName(x.GetViewModel)).Select(GetClientName).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -100,5 +103,15 @@ namespace SoftwareInstallationFileImplement.Implements } return viewModel; } + + private OrderViewModel GetClientName(OrderViewModel viewModel) + { + var client = source.Clients.FirstOrDefault(x => x.Id == viewModel.ClientId); + if (client != null) + { + viewModel.ClientFIO = client.ClientFIO; + } + return viewModel; + } } } diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Client.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Client.cs new file mode 100644 index 0000000..36167c0 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Client.cs @@ -0,0 +1,72 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement.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/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs index 934a043..3dc73c6 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs @@ -31,6 +31,7 @@ namespace SoftwareInstallationFileImplement.Models { Id = model.Id, PackageId = model.PackageId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -49,6 +50,7 @@ namespace SoftwareInstallationFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), PackageId = Convert.ToInt32(element.Element("PackageId")!.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), @@ -77,6 +79,7 @@ namespace SoftwareInstallationFileImplement.Models { Id = Id, PackageId = PackageId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, @@ -86,6 +89,7 @@ namespace SoftwareInstallationFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("PackageId", PackageId.ToString()), + new XElement("ClientId", ClientId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs b/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs index 00565cd..fd99be3 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace SoftwareInstallationListImplement public List Components { get; set; } public List Orders { get; set; } public List Packages { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Packages = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Implements/ClientStorage.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..e10bff8 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/ClientStorage.cs @@ -0,0 +1,104 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationListImplement.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/SoftwareInstallation/SoftwareInstallationListImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/OrderStorage.cs index 904cfeb..e33e1a9 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/Implements/OrderStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/OrderStorage.cs @@ -54,37 +54,26 @@ namespace SoftwareInstallationListImplement.Implements public List GetFilteredList(OrderSearchModel model) { var result = new List(); - - if (!model.Id.HasValue) - { - return result; - } - - foreach (var order in _source.Orders) - { - if (model.Id.HasValue && order.Id == model.Id) - { - result.Add(GetViewModelName(order)); - } - } - - return result; - } - - public List GetFullList(OrderSearchModel model) - { - var result = new List(); - foreach (var order in _source.Orders) { if ((!model.Id.HasValue || order.Id == model.Id) && - (!model.DateFrom.HasValue || order.DateCreate >= model.DateFrom) && - (!model.DateTo.HasValue || order.DateCreate <= model.DateTo)) + (!model.DateFrom.HasValue || order.DateCreate >= model.DateFrom) && + (!model.DateTo.HasValue || order.DateCreate <= model.DateTo) && + (!model.ClientId.HasValue || order.ClientId == model.ClientId)) ; { - result.Add(GetViewModelName(order.GetViewModel)); + result.Add(GetClientName(GetViewModelName(order.GetViewModel))); } } + return result; + } + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(GetClientName(GetViewModelName(order.GetViewModel))); + } return result; } @@ -125,18 +114,30 @@ namespace SoftwareInstallationListImplement.Implements return null; } - private OrderViewModel GetViewModelName(Order model) + private OrderViewModel GetViewModelName(OrderViewModel viewModel) { - var res = model.GetViewModel; foreach (var package in _source.Packages) { - if (package.Id == model.PackageId) + if (package.Id == viewModel.PackageId) { - res.PackageName = package.PackageName; + viewModel.PackageName = package.PackageName; break; } } - return res; + return viewModel; + } + + private OrderViewModel GetClientName(OrderViewModel viewModel) + { + foreach (var client in _source.Clients) + { + if (client.Id == viewModel.ClientId) + { + viewModel.ClientFIO = client.ClientFIO; + break; + } + } + return viewModel; } } } diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Client.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Client.cs new file mode 100644 index 0000000..adea200 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Client.cs @@ -0,0 +1,50 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationListImplement.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 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 + }; + } +} diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs index d2a6332..653fee2 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs @@ -31,6 +31,7 @@ namespace SoftwareInstallationListImplement.Models return new Order { PackageId = model.PackageId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -53,6 +54,7 @@ namespace SoftwareInstallationListImplement.Models { Id = Id, PackageId = PackageId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status,