From 660ea2894fabad3b0a408e6c3f5d470bd0d93f5d Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 14 Feb 2023 09:48:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=B7=D0=B0=D0=BA?= =?UTF-8?q?=D0=B0=D0=B7=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ManufactureStorage.cs | 97 +++++++++++++++++++ .../Implements/OrderStorage.cs | 96 ++++++++++++++++++ .../Implements/WorkPieceStorage.cs | 3 +- 3 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs new file mode 100644 index 0000000..753b070 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs @@ -0,0 +1,97 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + //реализация интерфейса хранилища изделий + public class ManufactureStorage : IManufactureStorage + { + private readonly DataFileSingleton source; + + public ManufactureStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Manufactures.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName)) + { + return new(); + } + + return source.Manufactures.Where(x => x.ManufactureName.Contains(model.ManufactureName)).Select(x => x.GetViewModel).ToList(); + } + + public ManufactureViewModel? GetElement(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName) && !model.Id.HasValue) + { + return null; + } + + return source.Manufactures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ManufactureName) && x.ManufactureName == model.ManufactureName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ManufactureViewModel? Insert(ManufactureBindingModel model) + { + model.Id = source.Manufactures.Count > 0 ? source.Manufactures.Max(x => x.Id) + 1 : 1; + + var newManufacture = Manufacture.Create(model); + + if (newManufacture == null) + { + return null; + } + + source.Manufactures.Add(newManufacture); + source.SaveWorkPieces(); + + return newManufacture.GetViewModel; + } + + public ManufactureViewModel? Update(ManufactureBindingModel model) + { + var manufacture = source.Manufactures.FirstOrDefault(x => x.Id == model.Id); + + if (manufacture == null) + { + return null; + } + + manufacture.Update(model); + source.SaveWorkPieces(); + + return manufacture.GetViewModel; + } + + public ManufactureViewModel? Delete(ManufactureBindingModel model) + { + var element = source.Manufactures.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Manufactures.Remove(element); + source.SaveWorkPieces(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..819b55a --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,96 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + //реализация интерфейса хранилища заказов + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Orders.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + return source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + return source.Orders.FirstOrDefault(x =>(model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + source.Orders.Add(newOrder); + source.SaveWorkPieces(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + source.SaveWorkPieces(); + + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Orders.Remove(element); + source.SaveWorkPieces(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/WorkPieceStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/WorkPieceStorage.cs index cd66646..b4e6084 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/WorkPieceStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/WorkPieceStorage.cs @@ -76,6 +76,7 @@ namespace BlacksmithWorkshopFileImplement.Implements workPiece.Update(model); source.SaveWorkPieces(); + return workPiece.GetViewModel; } @@ -87,11 +88,11 @@ namespace BlacksmithWorkshopFileImplement.Implements { source.WorkPieces.Remove(element); source.SaveWorkPieces(); + return element.GetViewModel; } return null; - } } }