From 7fb40e2c881e8abb1c344811057b0a61a4d7802b Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 28 Feb 2023 10:35:11 +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 --- .../BusinessLogic/ShopLogic.cs | 86 ++++++++++++ .../BindingModels/ShopBindingModel.cs | 22 +++ .../BusinessLogicsContracts/IShopLogic.cs | 27 ++++ .../SearchModels/ShopSearchModel.cs | 17 +++ .../StoragesContracts/IShopStorage.cs | 26 ++++ .../ViewModels/ShopViewModel.cs | 26 ++++ .../Models/{IStoreModel.cs => IShopModel.cs} | 7 +- .../DataListSingleton.cs | 4 + .../Implements/ShopStorage.cs | 128 ++++++++++++++++++ .../Models/Shop.cs | 64 +++++++++ 10 files changed, 405 insertions(+), 2 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs rename BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/{IStoreModel.cs => IShopModel.cs} (71%) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs new file mode 100644 index 0000000..123d366 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs @@ -0,0 +1,86 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopBusinessLogic.BusinessLogic +{ + internal class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + + public ShopLogic(ILogger logger, IShopStorage shopStorage) + { + _logger = logger; + _shopStorage = shopStorage; + } + + public ShopViewModel? ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. ShopName:{ShopName}. Id:{Id}", model.ShopName, model.Id); + var element = _shopStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + + return element; + } + + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id); + var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public bool AddManufacture(ShopSearchModel model, IManufactureModel manufacture, int count) + { + throw new NotImplementedException(); + } + + public bool Create(ShopBindingModel model) + { + throw new NotImplementedException(); + } + + public bool Update(ShopBindingModel model) + { + throw new NotImplementedException(); + } + + public bool Delete(ShopBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..ac86b1c --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,22 @@ +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public string ShopName { get; set; } = string.Empty; + + public string Address { get; set; } = string.Empty; + + public DateTime DateOpen { get; set; } = DateTime.Now; + + public Dictionary Manufactures { get; set; } = new(); + + public int Id { get; set; } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..5264ee8 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,27 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.BusinessLogicsContracts +{ + public interface IShopLogic + { + List? ReadList(ShopSearchModel? model); + + ShopViewModel? ReadElement(ShopSearchModel model); + + bool Create(ShopBindingModel model); + + bool Update(ShopBindingModel model); + + bool Delete(ShopBindingModel model); + + bool AddManufacture(ShopSearchModel model, IManufactureModel manufacture, int count); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..18e9323 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.SearchModels +{ + public class ShopSearchModel + { + //для поиска по идентификатору + public int? Id { get; set; } + + //для поиска по названию + public string? ShopName { get; set; } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..dcc0c25 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,26 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.StoragesContracts +{ + public interface IShopStorage + { + List GetFullList(); + + List GetFilteredList(ShopSearchModel model); + + ShopViewModel? GetElement(ShopSearchModel model); + + ShopViewModel? Insert(ShopBindingModel model); + + ShopViewModel? Update(ShopBindingModel model); + + ShopViewModel? Delete(ShopBindingModel model); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..1248cd6 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,26 @@ +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.ViewModels +{ + public class ShopViewModel + { + public int Id { get; set; } + + [DisplayName("Название магазина")] + public string ShopName { get; set; } = string.Empty; + + [DisplayName("Адрес магазина")] + public string Address { get; set; } = string.Empty; + + [DisplayName("Дата открытия")] + public DateTime DateOpen { get; set; } = DateTime.Now; + + public Dictionary Manufactures { get; set; } = new(); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IStoreModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IShopModel.cs similarity index 71% rename from BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IStoreModel.cs rename to BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IShopModel.cs index 2e9b059..f2e631b 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IStoreModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IShopModel.cs @@ -8,15 +8,18 @@ using System.Threading.Tasks; namespace BlacksmithWorkshopDataModels.Models { //интерфес сущности "Магазин" - public interface IStoreModel + public interface IShopModel : IId { //название магазина - string StoreName { get; } + string ShopName { get; } //адрес магазина string Address { get; } //дата открытия магазина DateTime DateOpen { get; } + + //изделия в магазине + Dictionary Manufactures { get; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs index e2b2a4c..fc10a40 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs @@ -21,11 +21,15 @@ namespace BlacksmithWorkshopListImplement //список для хранения заказов public List Orders { get; set; } + //список для хранения Магазинов + public List Shops { get; set; } + public DataListSingleton() { WorkPieces = new List(); Manufactures = new List(); Orders = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..5499622 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs @@ -0,0 +1,128 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.ShopName) && shop.ShopName == model.ShopName) || (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + + return null; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.ShopName)) + { + return result; + } + + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.ShopName)) + { + result.Add(shop.GetViewModel); + } + } + + return result; + } + + public List GetFullList() + { + var result = new List(); + + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + + return result; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + + var newShop = Shop.Create(model); + + if (newShop == null) + { + return null; + } + + _source.Shops.Add(newShop); + + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + + return shop.GetViewModel; + } + } + + return null; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + + return element.GetViewModel; + } + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs new file mode 100644 index 0000000..f5ca3bf --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs @@ -0,0 +1,64 @@ +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 Shop : IShopModel + { + public string ShopName { get; set; } = string.Empty; + + public string Address { get; set; } = string.Empty; + + public DateTime DateOpen { get; set; } + + public int Id { get; set; } + + public Dictionary Manufactures { get; private set; } = + new Dictionary(); + + public static Shop? Create(ShopBindingModel? model) + { + if(model == null) + { + return null; + } + + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + Manufactures = model.Manufactures + }; + } + + public void Update(ShopBindingModel? model) + { + if(model == null) + { + return; + } + + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + Manufactures = model.Manufactures; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + Manufactures = Manufactures + }; + } +}