From c6d13da0f3811a3cd226e774582eec7719c683a8 Mon Sep 17 00:00:00 2001 From: Kate Date: Fri, 28 Apr 2023 21:24:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BB=D1=806=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 5 + .../Implements/ImplementerStorage.cs | 103 ++++++++++++++ .../Implements/OrderStorage.cs | 14 ++ .../Models/Implementer.cs | 82 +++++++++++ PrecastConcreteFileImplement/Models/Order.cs | 9 +- .../ViewModels/OrderViewModel.cs | 1 - .../Implements/ImplementerStorage.cs | 83 +++++++---- .../Models/Implementer.cs | 4 + .../Models/Order.cs | 5 +- .../DataListSingleton.cs | 2 + .../Implements/ImplementerStorage.cs | 130 ++++++++++++++++++ .../Implements/OrderStorage.cs | 21 +++ 12 files changed, 424 insertions(+), 35 deletions(-) create mode 100644 PrecastConcreteFileImplement/Implements/ImplementerStorage.cs create mode 100644 PrecastConcreteFileImplement/Models/Implementer.cs create mode 100644 PrecastConcretePlantListImplement/Implements/ImplementerStorage.cs diff --git a/PrecastConcreteFileImplement/DataFileSingleton.cs b/PrecastConcreteFileImplement/DataFileSingleton.cs index c9d656c..0a20983 100644 --- a/PrecastConcreteFileImplement/DataFileSingleton.cs +++ b/PrecastConcreteFileImplement/DataFileSingleton.cs @@ -15,10 +15,12 @@ namespace PrecastConcretePlantFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string ReinforcedFileName = "Reinforced.xml"; private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Reinforceds { get; private set; } public List Clients { get; private set; } + public List Implementers { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -35,6 +37,8 @@ namespace PrecastConcretePlantFileImplement => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Implementers, OrderFileName, "Implementers", x + => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => @@ -43,6 +47,7 @@ namespace PrecastConcretePlantFileImplement Reinforced.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers=LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/PrecastConcreteFileImplement/Implements/ImplementerStorage.cs b/PrecastConcreteFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..e16961c --- /dev/null +++ b/PrecastConcreteFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + + if (res != null) + { + _source.Implementers.Remove(res); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (model.Id.HasValue) + return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + + if (model.ImplementerFIO != null && model.Password != null) + return _source.Implementers + .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) + && x.Password.Equals(model.Password)) + ?.GetViewModel; + + if (model.ImplementerFIO != null) + return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; + + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + + if (model.Id.HasValue) + { + var res = GetElement(model); + + return res != null ? new() { res } : new(); + } + + if (model.ImplementerFIO != null) + { + return _source.Implementers + .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + + return new(); + } + + public List GetFullList() + { + return _source.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; + + var res = Implementer.Create(model); + + if (res != null) + { + _source.Implementers.Add(res); + _source.SaveImplementers(); + } + + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + + if (res != null) + { + res.Update(model); + _source.SaveImplementers(); + } + + return res?.GetViewModel; + } + } +} diff --git a/PrecastConcreteFileImplement/Implements/OrderStorage.cs b/PrecastConcreteFileImplement/Implements/OrderStorage.cs index 5e98405..d6a7c66 100644 --- a/PrecastConcreteFileImplement/Implements/OrderStorage.cs +++ b/PrecastConcreteFileImplement/Implements/OrderStorage.cs @@ -37,6 +37,20 @@ namespace PrecastConcretePlantFileImplement.Implements public OrderViewModel? GetElement(OrderSearchModel model) { + if (model.ImplementerId.HasValue && model.Statuses != null) + { + return source.Orders + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId && + model.Statuses.Contains(x.Status)) + ?.GetViewModel; + } + + if (model.ImplementerId.HasValue) + { + return source.Orders + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId) + ?.GetViewModel; + } if (!model.Id.HasValue) { return null; diff --git a/PrecastConcreteFileImplement/Models/Implementer.cs b/PrecastConcreteFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..cb887cb --- /dev/null +++ b/PrecastConcreteFileImplement/Models/Implementer.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantFileImplement.Models +{ + public class Implementer : IImplementerModel + { + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public int Id { get; private set; } + + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value), + }; + } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + WorkExperience = WorkExperience + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("Password", Password), + new XElement("FIO", ImplementerFIO), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} diff --git a/PrecastConcreteFileImplement/Models/Order.cs b/PrecastConcreteFileImplement/Models/Order.cs index c7db726..9447a29 100644 --- a/PrecastConcreteFileImplement/Models/Order.cs +++ b/PrecastConcreteFileImplement/Models/Order.cs @@ -13,6 +13,7 @@ namespace PrecastConcretePlantFileImplement.Models { public class Order : IOrderModel { + public int? ImplementerId { get; set; } public int ClientId { get; private set; } public int ReinforcedId { get; private set; } public int Count { get; private set; } @@ -36,6 +37,7 @@ namespace PrecastConcretePlantFileImplement.Models return new Order() { Id = model.Id, + ImplementerId = model.ImplementerId, ClientId = model.ClientId, ReinforcedId = model.ReinforcedId, Count = model.Count, @@ -56,8 +58,9 @@ namespace PrecastConcretePlantFileImplement.Models var order = new Order() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value) ReinforcedId = Convert.ToInt32(element.Element("ReinforcedId")!.Value), - ClientId=Convert.ToInt32(element.Element("ClientId")!.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), @@ -85,6 +88,7 @@ namespace PrecastConcretePlantFileImplement.Models { Id = Id, ClientId = ClientId, + ImplementerId = ImplementerId, ReinforcedId = ReinforcedId, Count = Count, Sum = Sum, @@ -95,7 +99,8 @@ namespace PrecastConcretePlantFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), - new XAttribute("ClientId",ClientId.ToString()), + new XAttribute("ClientId", ClientId.ToString()), + new XElement("ImplementerId", ImplementerId.ToString()), new XElement("ReinforcedId", ReinforcedId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), diff --git a/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs b/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs index 8ca2a5d..4ef43de 100644 --- a/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs +++ b/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs @@ -19,7 +19,6 @@ namespace PrecastConcretePlantContracts.ViewModels public int? ImplementerId { get; set; } [DisplayName("ФИО исполнителя")] public string? ImplementerFIO { get; set; } = string.Empty; - [DisplayName("Количество")] public int ReinforcedId { get; set; } [DisplayName("ЖБИ")] public string ReinforcedName { get; set; } = string.Empty; diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/ImplementerStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/ImplementerStorage.cs index c337f92..7027c5d 100644 --- a/PrecastConcretePlantDataBaseImplemet/Implements/ImplementerStorage.cs +++ b/PrecastConcretePlantDataBaseImplemet/Implements/ImplementerStorage.cs @@ -24,52 +24,80 @@ namespace PrecastConcretePlantDatabaseImplement.Implements public List GetFilteredList(ImplementerSearchModel model) { - using var context = new PrecastConcretePlantDatabase(); - return context.Implementers + if (model == null) + { + return new(); + } + + if (model.Id.HasValue) + { + var implementer = GetElement(model); + + return implementer != null ? new() { implementer } : new(); + } + + if (model.ImplementerFIO != null) + { + using var context = new SoftwareInstallationDataBase(); + + return context.Implementers + .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) .Select(x => x.GetViewModel) .ToList(); + } + + return new(); } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { - if (!model.Id.HasValue) - { - return null; - } using var context = new PrecastConcretePlantDatabase(); - return context.Implementers - .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + if (model.Id.HasValue) + return context.Implementers + .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; + + if (model.ImplementerFIO != null && model.Password != null) + return context.Implementers + .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) + && x.Password.Equals(model.Password)) + ?.GetViewModel; + + if (model.ImplementerFIO != null) + return context.Implementers + .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) + ?.GetViewModel; + + return null; } public ImplementerViewModel? Insert(ImplementerBindingModel model) { - var newImplementer = Implementer.Create(model); - if (newImplementer == null) - { - return null; - } using var context = new PrecastConcretePlantDatabase(); - context.Implementers.Add(newImplementer); - context.SaveChanges(); - return context.Implementers - .FirstOrDefault(x => x.Id == newImplementer.Id) - ?.GetViewModel; + var implementer = Implementer.Create(model); + + if (implementer != null) + { + context.Implementers.Add(implementer); + context.SaveChanges(); + } + + return implementer?.GetViewModel; } public ImplementerViewModel? Update(ImplementerBindingModel model) { using var context = new PrecastConcretePlantDatabase(); - var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); - if (implementer == null) + var implementer = context.Implementers + .FirstOrDefault(x => x.Id == model.Id); + + if (implementer != null) { - return null; + implementer.Update(model); + context.SaveChanges(); } - implementer.Update(model); - context.SaveChanges(); - return context.Implementers - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; + + return implementer?.GetViewModel; } public ImplementerViewModel? Delete(ImplementerBindingModel model) @@ -81,9 +109,8 @@ namespace PrecastConcretePlantDatabaseImplement.Implements { context.Implementers.Remove(implementer); context.SaveChanges(); - return implementer.GetViewModel; } - return null; + return implementer?.GetViewModel; } } } \ No newline at end of file diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Implementer.cs b/PrecastConcretePlantDataBaseImplemet/Models/Implementer.cs index aed2a84..0db20f3 100644 --- a/PrecastConcretePlantDataBaseImplemet/Models/Implementer.cs +++ b/PrecastConcretePlantDataBaseImplemet/Models/Implementer.cs @@ -48,6 +48,10 @@ namespace PrecastConcretePlantDatabaseImplement.Models public void Update(ImplementerBindingModel model) { + if (model == null) + { + return; + } ImplementerFIO = model.ImplementerFIO; Password = model.Password; WorkExperience = model.WorkExperience; diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Order.cs b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs index 224cb5a..2ecc9a1 100644 --- a/PrecastConcretePlantDataBaseImplemet/Models/Order.cs +++ b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs @@ -60,10 +60,7 @@ namespace PrecastConcretePlantDatabaseImplement.Models } Status = model.Status; DateImplement = model.DateImplement; - if (model.ImplementerId.HasValue) - { - ImplementerId = model.ImplementerId; - } + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel diff --git a/PrecastConcretePlantListImplement/DataListSingleton.cs b/PrecastConcretePlantListImplement/DataListSingleton.cs index 1a9a863..cd739ac 100644 --- a/PrecastConcretePlantListImplement/DataListSingleton.cs +++ b/PrecastConcretePlantListImplement/DataListSingleton.cs @@ -14,12 +14,14 @@ namespace PrecastConcretePlantListImplement public List Orders { get; set; } public List Reinforceds { get; set; } public List Clients { get; set; } + public List Implementers { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Reinforceds = new List(); Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() { diff --git a/PrecastConcretePlantListImplement/Implements/ImplementerStorage.cs b/PrecastConcretePlantListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..fdb06d6 --- /dev/null +++ b/PrecastConcretePlantListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,130 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + + return element.GetViewModel; + } + } + return null; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + foreach (var x in _source.Implementers) + { + if (model.Id.HasValue && x.Id == model.Id) + return x.GetViewModel; + + if (model.ImplementerFIO != null && model.Password != null && + x.ImplementerFIO.Equals(model.ImplementerFIO) && + x.Password.Equals(model.Password)) + return x.GetViewModel; + + if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO)) + return x.GetViewModel; + } + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + + if (model.Id.HasValue) + { + var res = GetElement(model); + + return res != null ? new() { res } : new(); + } + + List result = new(); + + if (model.ImplementerFIO != null) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Equals(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + + return result; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = 1; + + foreach (var implementer in _source.Implementers) + { + if (model.Id <= implementer.Id) + { + model.Id = implementer.Id + 1; + } + } + + var res = Implementer.Create(model); + + if (res != null) + { + _source.Implementers.Add(res); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.Id == model.Id) + { + implementer.Update(model); + + return implementer.GetViewModel; + } + } + return null; + } + } +} diff --git a/PrecastConcretePlantListImplement/Implements/OrderStorage.cs b/PrecastConcretePlantListImplement/Implements/OrderStorage.cs index f368705..3ededc3 100644 --- a/PrecastConcretePlantListImplement/Implements/OrderStorage.cs +++ b/PrecastConcretePlantListImplement/Implements/OrderStorage.cs @@ -47,6 +47,18 @@ namespace PrecastConcretePlantListImplement.Implements { return order.GetViewModel; } + else if (model.ImplementerId.HasValue && model.Statuses != null && + order.ImplementerId == model.ImplementerId && + model.Statuses.Contains(order.Status)) + { + return GetViewModel(order); + } + + else if (model.ImplementerId.HasValue && + model.ImplementerId == order.ImplementerId) + { + return GetViewModel(order); + } } return null; @@ -69,6 +81,15 @@ namespace PrecastConcretePlantListImplement.Implements { result.Add(order.GetViewModel); } + else if (model.ImplementerId.HasValue && order.ImplementerId == model.ImplementerId) + { + result.Add(order.GetViewModel); + } + + else if (model.Status != null && model.Status.Contains(order.Status)) + { + result.Add(order.GetViewModel); + } } } else if (model.DateFrom != null && model.DateTo != null)