From de1f96bbe67ae7ee6003871c57274a6c6a14f000 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Wed, 14 Feb 2024 21:27:41 +0300 Subject: [PATCH] =?UTF-8?q?=D1=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FlowerShopBusinessLogic/ShopLogic.cs | 135 +++++++++++++++++ .../BindingModels/ShopBindingModel.cs | 18 +++ .../BusinessLogicsContracts/IShopLogic.cs | 22 +++ .../SearchModels/ShopSearchModel.cs | 14 ++ .../StoragesContracts/IShopStorage.cs | 23 +++ .../ViewModels/ShopViewModel.cs | 22 +++ FlowerShopDataModels/IShopModel.cs | 16 ++ FlowerShopListImplement/DataListSingleton.cs | 2 + FlowerShopListImplement/Shop.cs | 55 +++++++ FlowerShopListImplement/ShopStorage.cs | 140 ++++++++++++++++++ 10 files changed, 447 insertions(+) create mode 100644 FlowerShopBusinessLogic/ShopLogic.cs create mode 100644 FlowerShopContracts/BindingModels/ShopBindingModel.cs create mode 100644 FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 FlowerShopContracts/SearchModels/ShopSearchModel.cs create mode 100644 FlowerShopContracts/StoragesContracts/IShopStorage.cs create mode 100644 FlowerShopContracts/ViewModels/ShopViewModel.cs create mode 100644 FlowerShopDataModels/IShopModel.cs create mode 100644 FlowerShopListImplement/Shop.cs create mode 100644 FlowerShopListImplement/ShopStorage.cs diff --git a/FlowerShopBusinessLogic/ShopLogic.cs b/FlowerShopBusinessLogic/ShopLogic.cs new file mode 100644 index 0000000..050e6ca --- /dev/null +++ b/FlowerShopBusinessLogic/ShopLogic.cs @@ -0,0 +1,135 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopBusinessLogic +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + + public ShopLogic(ILogger logger, IShopStorage shopStorage) + { + _logger = logger; + _shopStorage = shopStorage; + } + + public List ReadList(ShopSearchModel model) + { + _logger.LogInformation("ReadList. ShopName:{Name}. Id:{ Id}", model?.Name, 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 MakeSupply(ShopSearchModel model, IFlowerModel flower, int count) + { + if (model == null) + return false; + return _shopStorage.SupplyFlowers(model, flower, count); + } + + public ShopViewModel ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.Name, 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 bool Create(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentNullException("Нет названия магазина", + nameof(model.ShopName)); + } + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Нет адресса магазина", + nameof(model.ShopName)); + } + if (model.DateOpen == null) + { + throw new ArgumentNullException("Нет даты открытия магазина", + nameof(model.ShopName)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Address:{Address}. DateOpen:{DateOpen}. Id: { Id}", model.ShopName, model.Address, model.DateOpen, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + Name = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/FlowerShopContracts/BindingModels/ShopBindingModel.cs b/FlowerShopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..9afd7ed --- /dev/null +++ b/FlowerShopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,18 @@ +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } + public string Address { get; set; } + public DateTime DateOpen { get; set; } + public Dictionary ShopFlowers { get; set; } = new(); + } +} diff --git a/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs b/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..c9a8e57 --- /dev/null +++ b/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,22 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.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 MakeSupply(ShopSearchModel model, IFlowerModel flower, int count); + } +} diff --git a/FlowerShopContracts/SearchModels/ShopSearchModel.cs b/FlowerShopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..8a99201 --- /dev/null +++ b/FlowerShopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/FlowerShopContracts/StoragesContracts/IShopStorage.cs b/FlowerShopContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..de5a976 --- /dev/null +++ b/FlowerShopContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,23 @@ +using FlowerShopContracts.ViewModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.BindingModels; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.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); + bool SupplyFlowers(ShopSearchModel model, IFlowerModel flower, int Count); + } +} diff --git a/FlowerShopContracts/ViewModels/ShopViewModel.cs b/FlowerShopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..bb7556f --- /dev/null +++ b/FlowerShopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FlowerShopDataModels.Models; + +namespace FlowerShopContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public int Id { get; set; } + [DisplayName("Название магазина")] + public string ShopName { get; set; } + [DisplayName("Адрес магазина")] + public string Address { get; set; } + [DisplayName("Дата открытия")] + public DateTime DateOpen { get; set; } + public Dictionary ShopFlowers { get; set; } = new(); + } +} diff --git a/FlowerShopDataModels/IShopModel.cs b/FlowerShopDataModels/IShopModel.cs new file mode 100644 index 0000000..54bba9f --- /dev/null +++ b/FlowerShopDataModels/IShopModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDataModels.Models +{ + public interface IShopModel : IId + { + string ShopName { get; } + string Address { get; } + DateTime DateOpen { get; } + Dictionary ShopFlowers { get; } + } +} diff --git a/FlowerShopListImplement/DataListSingleton.cs b/FlowerShopListImplement/DataListSingleton.cs index 117b155..92f37db 100644 --- a/FlowerShopListImplement/DataListSingleton.cs +++ b/FlowerShopListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace FlowerShopListImplement public List Components { get; set; } public List Orders { get; set; } public List Flowers { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Flowers = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/FlowerShopListImplement/Shop.cs b/FlowerShopListImplement/Shop.cs new file mode 100644 index 0000000..e7bba4a --- /dev/null +++ b/FlowerShopListImplement/Shop.cs @@ -0,0 +1,55 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } + public string Address { get; private set; } + public DateTime DateOpen { get; private set; } + public Dictionary ShopFlowers { get; private set; } = new(); + + 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, + ShopFlowers = new() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + ShopFlowers = model.ShopFlowers; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopFlowers = ShopFlowers + }; + } +} diff --git a/FlowerShopListImplement/ShopStorage.cs b/FlowerShopListImplement/ShopStorage.cs new file mode 100644 index 0000000..77219a3 --- /dev/null +++ b/FlowerShopListImplement/ShopStorage.cs @@ -0,0 +1,140 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using FlowerShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopListImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + public List GetFilteredList(ShopSearchModel + model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.Name)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && + shop.ShopName == model.Name) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; + } + 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; + } + + public bool SupplyFlowers(ShopSearchModel model, IFlowerModel flower, int count) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (flower == null) + throw new ArgumentNullException(nameof(flower)); + if (count <= 0) + throw new ArgumentNullException("Количество должно быть положительным числом"); + + ShopViewModel curModel = GetElement(model); + if (curModel == null) + throw new ArgumentNullException(nameof(curModel)); + if (curModel.ShopFlowers.TryGetValue(flower.Id, out var pair)) + { + curModel.ShopFlowers[flower.Id] = (pair.Item1, pair.Item2 + count); + } + else + { + curModel.ShopFlowers.Add(flower.Id, (flower, count)); + } + Update(new() + { + Id = curModel.Id, + ShopName = curModel.ShopName, + DateOpen = curModel.DateOpen, + Address = curModel.Address, + ShopFlowers = curModel.ShopFlowers, + }); + return true; + } + } +}