From 57d43005e6e9c2edc815cec7a0140395674d4eff Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Tue, 9 Apr 2024 22:14:14 +0400 Subject: [PATCH] fix BusinessLogic --- .../BusinessLogics/ShopLogic.cs | 178 +++++++++--------- 1 file changed, 90 insertions(+), 88 deletions(-) diff --git a/AutoWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs b/AutoWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs index 995d417..632228d 100644 --- a/AutoWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AutoWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -1,157 +1,159 @@ -using Microsoft.Extensions.Logging; -using PizzeriaContracts.BindingModels; -using PizzeriaContracts.BusinessLogicsContracts; -using PizzeriaContracts.SearchModels; -using PizzeriaContracts.StoragesContracts; -using PizzeriaContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.BusinessLogicsContracts; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using Microsoft.Extensions.Logging; -namespace PizzeriaBusinessLogic.BusinessLogics +namespace AutoWorkshopBusinessLogic.BusinessLogics { public class ShopLogic : IShopLogic { private readonly ILogger _logger; private readonly IShopStorage _shopStorage; - private readonly IPizzaStorage _pizzaStorage; + private readonly IRepairStorage _repairStorage; - public ShopLogic(ILogger logger, IShopStorage shopStorage, IPizzaStorage pizzaStorage) + public ShopLogic(ILogger Logger, IShopStorage ShopStorage, IRepairStorage RepairStorage) { - _logger = logger; - _shopStorage = shopStorage; - _pizzaStorage = pizzaStorage; + _logger = Logger; + _shopStorage = ShopStorage; + _repairStorage = RepairStorage; } - public List? ReadList(ShopSearchModel? model) + 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.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; + + _logger.LogInformation("ReadList. Count:{Count}", List.Count); + return List; } - public ShopViewModel? ReadElement(ShopSearchModel model) + 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) + 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; + + _logger.LogInformation("ReadElement find. Id:{Id}", Element.Id); + return Element; } - public bool Create(ShopBindingModel model) + public bool Create(ShopBindingModel Model) { - CheckModel(model); - if (_shopStorage.Insert(model) == null) + CheckModel(Model); + + if (_shopStorage.Insert(Model) == null) { _logger.LogWarning("Insert operation failed"); return false; } + return true; } - public bool Update(ShopBindingModel model) + public bool Update(ShopBindingModel Model) { - CheckModel(model); - if (_shopStorage.Update(model) == null) + CheckModel(Model); + + if (_shopStorage.Update(Model) == null) { _logger.LogWarning("Update operation failed"); return false; } + return true; } - public bool Delete(ShopBindingModel model) + public bool Delete(ShopBindingModel Model) { - CheckModel(model, false); - _logger.LogInformation("Delete. Id:{Id}", model.Id); - if (_shopStorage.Delete(model) == null) + CheckModel(Model, false); + _logger.LogInformation("Delete. Id:{Id}", Model.Id); + + if (_shopStorage.Delete(Model) == null) { _logger.LogWarning("Delete operation failed"); return false; } + return true; } - public bool MakeSupply(SupplyBindingModel model) + public bool MakeSupply(SupplyBindingModel Model) { - if (model == null) + if (Model == null) + throw new ArgumentNullException(nameof(Model)); + + if (Model.Count <= 0) + throw new ArgumentException("Количество ремонтов должно быть больше 0"); + + var Shop = _shopStorage.GetElement(new ShopSearchModel { - throw new ArgumentNullException(nameof(model)); - } - if (model.Count <= 0) - { - throw new ArgumentException("Количество изделий должно быть больше 0"); - } - var shop = _shopStorage.GetElement(new ShopSearchModel - { - Id = model.ShopId + Id = Model.ShopId }); - if (shop == null) - { + + if (Shop == null) throw new ArgumentException("Магазина не существует"); - } - if (shop.ShopPizzas.ContainsKey(model.PizzaId)) + + if (Shop.ShopRepairs.ContainsKey(Model.RepairId)) { - var oldValue = shop.ShopPizzas[model.PizzaId]; - oldValue.Item2 += model.Count; - shop.ShopPizzas[model.PizzaId] = oldValue; + var OldValue = Shop.ShopRepairs[Model.RepairId]; + OldValue.Item2 += Model.Count; + Shop.ShopRepairs[Model.RepairId] = OldValue; } else { - var pizza = _pizzaStorage.GetElement(new PizzaSearchModel + var Repair = _repairStorage.GetElement(new RepairSearchModel { - Id = model.PizzaId + Id = Model.RepairId }); - if (pizza == null) - { - throw new ArgumentException($"Поставка: Товар с id:{model.PizzaId} не найденн"); - } - shop.ShopPizzas.Add(model.PizzaId, (pizza, model.Count)); + + if (Repair == null) + throw new ArgumentException($"Поставка: Товар с id:{Model.RepairId} не найденн"); + + Shop.ShopRepairs.Add(Model.RepairId, (Repair, Model.Count)); } + return true; } - private void CheckModel(ShopBindingModel model, bool withParams = true) + private void CheckModel(ShopBindingModel Model, bool WithParams=true) { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - if (!withParams) - { + if (Model == null) + throw new ArgumentNullException(nameof(Model)); + + if (!WithParams) return; - } - if (string.IsNullOrEmpty(model.Adress)) + + if (string.IsNullOrEmpty(Model.Address)) + throw new ArgumentException("Адрес магазина длжен быть заполнен", nameof(Model.Address)); + + if (string.IsNullOrEmpty(Model.ShopName)) + throw new ArgumentException("Название магазина должно быть заполнено", nameof(Model.ShopName)); + + _logger.LogInformation("Shop. ShopName: {ShopName}. Address: {Address}. OpeningDate: {OpeningDate}. Id:{Id}", + Model.ShopName, Model.Address, Model.OpeningDate, Model.Id); + + var Element = _shopStorage.GetElement(new ShopSearchModel { - throw new ArgumentException("Адрес магазина длжен быть заполнен", nameof(model.Adress)); - } - if (string.IsNullOrEmpty(model.ShopName)) - { - throw new ArgumentException("Название магазина должно быть заполнено", nameof(model.ShopName)); - } - _logger.LogInformation("Shop. ShopName:{ShopName}.Address:{Address}.OpeningDate:{OpeningDate}.Id:{ Id}", model.ShopName, model.Adress, model.OpeningDate, model.Id); - var element = _shopStorage.GetElement(new ShopSearchModel - { - ShopName = model.ShopName + ShopName = Model.ShopName }); - if (element != null && element.Id != model.Id) + if (Element != null && Element.Id != Model.Id) { throw new InvalidOperationException("Магазин с таким названием уже есть"); }