diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CategoryProductLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CategoryProductLogic.cs index 436daf2..e785229 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CategoryProductLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CategoryProductLogic.cs @@ -1,6 +1,7 @@ using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using Microsoft.Extensions.Logging; using System; @@ -14,18 +15,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public class CategoryProductLogic : ICategoryProductLogic { private readonly ILogger _logger; - //private readonly ICategoryProductStorage _storage; + private readonly ICategoryProductStorage _storage; - // todo нет интерфейса хранилища - public CategoryProductLogic(ILogger logger) { + public CategoryProductLogic(ILogger logger, ICategoryProductStorage storage) { _logger = logger; + _storage = storage; } public bool Create(CategoryProductBindingModel model) { CheckModel(model); - // todo _storage.Insert(model) == null - if (model == null) { + if (_storage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } @@ -36,8 +36,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic { CheckModel(model, false); _logger.LogInformation($"Delete. ID:{model.ID}"); - // todo _storage.Insert(model) == null - if (model == null) { + if (_storage.Insert(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } @@ -46,8 +45,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public bool Update(CategoryProductBindingModel model) { CheckModel(model); - // todo _storage.Insert(model) == null - if (model == null) { + if (_storage.Insert(model) == null) { _logger.LogWarning("Update operation failed"); return false; } @@ -57,16 +55,13 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public List? ReadList(CategoryProductSearchModel? model) { _logger.LogInformation($"ReadList. ID:{model?.ID}"); - // todo model == null _CategoryProductStorage.GetFullList() : _CategoryProductStorage.GetFilteredList(model); - var list = model; + var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } - // todo $"ReadList. Count:{list.count}" - _logger.LogInformation("ReadList. Count:{Count}"); - // todo return list; - return null; + _logger.LogInformation($"ReadList. Count:{list.Count}"); + return list; } private void CheckModel(CategoryProductBindingModel model, bool withParams = true) { @@ -79,17 +74,16 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Нет названия категории продукта", nameof(model.Name)); } - _logger.LogInformation($"CategoryProduct. Name:{model.Name}"); - /* - var element = _ CategoryProductStorage.GetElement(new CategoryProductSearchModel + _logger.LogInformation($"CategoryProduct. ID:{model.ID}.Name:{model.Name}"); + + var element = _storage.GetElement(new CategoryProductSearchModel { Name = model.Name }); - if (element != null && element.Id != model.ID) - { + if (element != null && element.ID != model.ID) { throw new InvalidOperationException("Категория с таким названием уже есть"); } - */ + } } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs index 17b8f7d..cdd7c61 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -16,19 +16,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public class OrderLogic : IOrderLogic { private readonly ILogger _logger; - //private readonly IOrderStorage _storage; + private readonly IOrderStorage _storage; - // todo нет интерфейса хранилища - public OrderLogic(ILogger logger) { + public OrderLogic(ILogger logger, IOrderStorage storage) { _logger = logger; - //storage = _storage; + _storage = storage; } public bool CreateOrder(OrderBindingModel model) { CheckModel(model); model.Status = OrderStatus.Принят; - // todo _orderStorage.Insert(model) == null - if (model == null) { + if (_storage.Insert(model) == null) { _logger.LogInformation("Insert operation failed"); return false; } @@ -53,16 +51,13 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public List? ReadList(OrderSearchModel? model) { _logger.LogInformation($"ReadList:ID:{model?.ID}"); - // todo model == null? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); - var list = model; + var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ; if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } - // todo "$ReadList.Count:{list.count}" - _logger.LogInformation("ReadList.Count:{Count}"); - //todo return list - return null; + _logger.LogInformation($"ReadList.Count:{list.Count}"); + return list; } private void CheckModel(OrderBindingModel model, bool withParams = true) { @@ -79,37 +74,27 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum)); } _logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.Status:{model.Status}.PaymeantOption:{model.PaymeantOption}." + - $"DateCreate:{model.DateCreate}.DataImplement:{model.DateImplemet}"); + $"DateCreate:{model.DateCreate}.DataImplement:{model.DateImplement}"); } private bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus) { CheckModel(model, false); - //todo - var viewModel = model; // model == _orderStorage.GetElement(new OrderSearchModel { ID = model.ID }); - if (viewModel == null) - { + var viewModel = _storage.GetElement(new OrderSearchModel { ID = model.ID }); + if (viewModel == null) { throw new ArgumentNullException(nameof(model)); } - if (viewModel.Status + 1 != newOrderStatus) - { + if (viewModel.Status + 1 != newOrderStatus) { _logger.LogWarning("Status update to " + newOrderStatus.ToString() + " operation failed."); return false; } model.Status = newOrderStatus; - if (model.Status == OrderStatus.Готов) - { - //todo подключить бд - //model.DateImplement = DateTime.Now; + if (model.Status == OrderStatus.Готов) { + model.DateImplement = DateTime.Now; } - else - { - // todo подключить бд - //model.DateImplement = viewModel.DateImplement; + else { + model.DateImplement = viewModel.DateImplement; } - // todo - // _orderStorage.Update(model) == null - if (model == null) - { + if (_storage.Update(model) == null) { _logger.LogWarning("Update operarion failed"); return false; } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs new file mode 100644 index 0000000..27ef70e --- /dev/null +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs @@ -0,0 +1,99 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.BusinessLogicContracts; +using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; +using ElectronicsShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopBusinessLogic.BusinessLogic +{ + public class ProductLogic : IProductLogic + { + private readonly ILogger _logger; + private readonly IProductStorage _storage; + + public ProductLogic(ILogger logger, IProductStorage productStorage) { + _logger = logger; + _storage = productStorage; + } + + public bool Create(ProductBindingModel model) + { + CheckModel(model); + if (_storage.Insert(model) == null) { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(ProductBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation($"Delete. ID:{model.ID}"); + if (_storage.Delete(model) == null) { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public bool Update(ProductBindingModel model) + { + CheckModel(model); + if (_storage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public ProductViewModel? ReadElement(ProductSearchModel? model) + { + if (model == null) throw new ArgumentNullException(nameof(model)); + _logger.LogInformation($"ReadElement. ProductName:{model.ProductName}. ID:{model.ID}"); + var element = _storage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement. elementn not found"); + return null; + } + _logger.LogInformation($"ReadElement find. ID:{element.ID}"); + return element; + } + + public List? ReadList(ProductSearchModel? model) + { + _logger.LogInformation($"ReadList. ProductName:{model?.ProductName}. ID:{model?.ID}"); + var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation($"ReadList. Count:{list.Count}"); + return list; + } + + private void CheckModel(ProductBindingModel model, bool withParams = true) + { + if (model == null) throw new ArgumentNullException(nameof(model)); + if (!withParams) return; + if (string.IsNullOrEmpty(model.ProductName)) + throw new ArgumentNullException("Нет названия продукта", nameof(model.ProductName)); + if (model.Price <= 0) + throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price)); + _logger.LogInformation($"Product. ID:{model.ID}.ProductName:{model.ProductName}.Price:{model.Price}.Count:{model.Count}" + + $".CategoryID:{model.CategoryID}"); + var element = _storage.GetElement(new ProductSearchModel { ProductName = model.ProductName }); + if (element != null && element.ID != model.ID) + throw new InvalidOperationException("Продукт с таким названием уже есть"); + } + } +} diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/RoleLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/RoleLogic.cs index d0b888a..409ab56 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/RoleLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/RoleLogic.cs @@ -1,6 +1,7 @@ using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using Microsoft.Extensions.Logging; using System; @@ -14,19 +15,18 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public class RoleLogic : IRoleLogic { private readonly ILogger _logger; - //private readonly ICategoryProductStorage _storage; + private readonly IRoleStorage _storage; - // todo нет интерфейса хранилища - public RoleLogic(ILogger logger) + public RoleLogic(ILogger logger, IRoleStorage storage) { _logger = logger; + _storage = storage; } public bool Create(RoleBindingModel model) { CheckModel(model); - // todo _storage.Insert(model) == null - if (model == null) + if (_storage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; @@ -38,8 +38,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic { CheckModel(model, false); _logger.LogInformation($"Delete. ID:{model.ID}"); - // todo _storage.Insert(model) == null - if (model == null) + if (_storage.Insert(model) == null) { _logger.LogWarning("Delete operation failed"); return false; @@ -49,8 +48,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public bool Update(RoleBindingModel model) { CheckModel(model); - // todo _storage.Insert(model) == null - if (model == null) + if (_storage.Insert(model) == null) { _logger.LogWarning("Update operation failed"); return false; @@ -61,17 +59,14 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public List? ReadList(RoleSearchModel? model) { _logger.LogInformation($"ReadList. ID:{model?.ID}"); - // todo model == null _RoleStorage.GetFullList() : _RoleStorage.GetFilteredList(model); - var list = model; + var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } - // todo $"ReadList. Count:{list.count}" - _logger.LogInformation("ReadList. Count:{Count}"); - // todo return list; - return null; + _logger.LogInformation($"ReadList. Count:{list.Count}"); + return list; } private void CheckModel(RoleBindingModel model, bool withParams = true) @@ -88,17 +83,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic { throw new ArgumentNullException("Нет названия категории продукта", nameof(model.Name)); } - _logger.LogInformation($"CategoryProduct. Name:{model.Name}"); - /* - var element = _RoleStorage.GetElement(new RoleSearchModel + _logger.LogInformation($"CategoryProduct. ID:{model.ID}.Name:{model.Name}"); + + var element = _storage.GetElement(new RoleSearchModel { Name = model.Name }); - if (element != null && element.Id != model.ID) + if (element != null && element.ID != model.ID) { throw new InvalidOperationException("Такая роль уже есть"); } - */ + } } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/UserLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/UserLogic.cs index a3106cf..138cdb3 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/UserLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/UserLogic.cs @@ -1,6 +1,7 @@ using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using Microsoft.Extensions.Logging; using System; @@ -14,20 +15,19 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic internal class UserLogic : IUserLogic { private readonly ILogger _logger; - //private readonly IClientStorage _storage; + private readonly IUserStorage _storage; // todo нет интерфейса хранилища - public UserLogic(ILogger logger) + public UserLogic(ILogger logger, IUserStorage storage) { _logger = logger; - //storage = _storage; + _storage = storage; } public bool Add(UserBindingModel model) { CheckModel(model); - // todo логика добавления в _clientStorage:_clientStorage.Insert(model) == null - if (model == null) + if (_storage.Insert(model) == null) { _logger.LogWarning("Add operation failed"); return false; @@ -38,8 +38,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public bool Update(UserBindingModel model) { CheckModel(model); - // todo логика добавления в _clientStorage:_clientStorage.Update(model) == null - if (model == null) + if (_storage.Insert(model) == null) { _logger.LogWarning("Update operation failed"); return false; @@ -51,8 +50,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic { CheckModel(model, false); _logger.LogInformation($"Delete.ID:{model.ID}"); - // todo логика добавления в _clientStorage:_clientStorage.Delete(model) == null - if (model == null) + if (_storage.Insert(model) == null) { _logger.LogWarning("Delete operation failes"); return false; @@ -68,33 +66,28 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic throw new ArgumentNullException(nameof(model)); } _logger.LogInformation($"ReadElement: logint:{model.Login}.ID:{model.ID}"); - // todo element = _clientStorage.GetElement(model); - var element = model; + var element = _storage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not fount"); return null; } _logger.LogInformation($"ReadElement: find.ID:{element.ID}"); - // todo retun element; - return null; + return element; } public List? ReadList(UserSearchModel? model) { _logger.LogInformation($"ReadList: ClientID:{model?.ID}"); - // todo получение списка из хранилища, model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); - var list = model; + var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ; if (list == null) { _logger.LogWarning("ReadList: return null list"); return null; } - // toto ReadList:Count:{list.count}, - _logger.LogInformation("ReadList:Count:{Count}"); - // todo return list; - return null; + _logger.LogInformation($"ReadList:Count:{list.Count}"); + return list; } private void CheckModel(UserBindingModel model, bool withParams = true) @@ -131,18 +124,19 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic { throw new ArgumentNullException("Нет номер телефона пользователя", nameof(model.PhoneNumber)); } - _logger.LogInformation($"Client. Login:{model.Login}.FirstName:{model.FirstName}.LastName:{model.LastName}.Email:{model.Email}." + - $"Password:{model.Password}.PhoneNumber:{model.PhoneNumber}"); - /* - var element = _clientStorage.GetElement(new ClientSearchModel + _logger.LogInformation($"Client. ID:{model.ID}.RoleID:{model.RoleID}.FirstName:{model.FirstName}." + + $"LastName:{model.LastName}.Password:{model.Password}.PhoneNumber:{model.PhoneNumber}.Login:{model.Login}." + + $"Email:{model.Email}"); + + var element = _storage.GetElement(new UserSearchModel { Login = model.Login }); - if (element != null && element.Id != model.ID) + if (element != null && element.ID != model.ID) { throw new InvalidOperationException("Клиент с таким логином уже есть"); } - */ + } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs b/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs index f6007ab..59b88f6 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BindingModels/OrderBindingModel.cs @@ -20,7 +20,7 @@ namespace ElectronicsShopContracts.BindingModels public DateTime DateCreate { get; set; } = DateTime.Now; - public DateTime? DateImplemet { get; set; } + public DateTime? DateImplement { get; set; } public Dictionary ProductList { get; set; } = new(); diff --git a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs index 3616e74..a4c3d3b 100644 --- a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs +++ b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs @@ -12,10 +12,10 @@ namespace ElectronicsShopContracts.StorageContracts public interface IProductStorage { List GetFullList(); - List GetFilteredList(OrderSearchModel model); - ProductViewModel? GetElement(OrderSearchModel model); - ProductViewModel? Insert(OrderBindingModel model); - ProductViewModel? Update(OrderBindingModel model); - ProductViewModel? Delete(OrderBindingModel model); + List GetFilteredList(ProductSearchModel model); + ProductViewModel? GetElement(ProductSearchModel model); + ProductViewModel? Insert(ProductBindingModel model); + ProductViewModel? Update(ProductBindingModel model); + ProductViewModel? Delete(ProductBindingModel model); } } diff --git a/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs b/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs index d063fca..10c59a2 100644 --- a/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/ViewModels/OrderViewModel.cs @@ -26,7 +26,7 @@ namespace ElectronicsShopContracts.ViewModels public DateTime DateCreate { get; set; } = DateTime.Now; [DisplayName("Дата выполнения")] - public DateTime? DateImplemet { get; set; } + public DateTime? DateImplement { get; set; } public Dictionary ProductList { get; set; } = new(); } diff --git a/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs b/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs index fe56d4a..b569027 100644 --- a/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs +++ b/ElectronicsShop/ElectronicsShopDataModels/Models/IOrderModel.cs @@ -13,7 +13,7 @@ namespace ElectronicsShopDataModels.Models OrderStatus Status { get; } PaymeantOption PaymeantOption { get; } DateTime DateCreate { get; } - DateTime? DateImplemet { get; } + DateTime? DateImplement { get; } //список товаров в заказе Dictionary ProductList { get; } }