diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/StoreKeeperLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/StoreKeeperLogic.cs new file mode 100644 index 0000000..aa056f6 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/StoreKeeperLogic.cs @@ -0,0 +1,107 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class StoreKeeperLogic : IStoreKeeperLogic + { + private readonly ILogger _logger; + private readonly IStoreKeeperStorage _storeKeeperStorage; + public StoreKeeperLogic(ILogger logger, IStoreKeeperStorage storeKeeperStorage) + { + _logger = logger; + _storeKeeperStorage = storeKeeperStorage; + } + public List? ReadList(StoreKeeperSearchModel? model) + { + _logger.LogInformation("ReadList. Login:{Login}.Id:{Id}", model?.Login, model?.Id); + var list = model == null ? _storeKeeperStorage.GetFullList() : _storeKeeperStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public StoreKeeperViewModel? ReadElement(StoreKeeperSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login:{Login}.Id:{Id}", model.Login, model.Id); + var element = _storeKeeperStorage.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(StoreKeeperBindingModel model) + { + CheckModel(model); + if (_storeKeeperStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(StoreKeeperBindingModel model) + { + CheckModel(model); + if (_storeKeeperStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(StoreKeeperBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_storeKeeperStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(StoreKeeperBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина кладовщика", nameof(model.Login)); + } + var element = _storeKeeperStorage.GetElement(new StoreKeeperSearchModel + { + Login = model.Login + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Кладовщик с таким логином уже есть"); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs new file mode 100644 index 0000000..449400c --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs @@ -0,0 +1,107 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class VendorLogic : IVendorLogic + { + private readonly ILogger _logger; + private readonly IVendorStorage _vendorStorage; + public VendorLogic(ILogger logger, IVendorStorage vendorStorage) + { + _logger = logger; + _vendorStorage = vendorStorage; + } + public List? ReadList(VendorSearchModel? model) + { + _logger.LogInformation("ReadList. Login:{Login}.Id:{Id}", model?.Login, model?.Id); + var list = model == null ? _vendorStorage.GetFullList() : _vendorStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public VendorViewModel? ReadElement(VendorSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login:{Login}.Id:{Id}", model.Login, model.Id); + var element = _vendorStorage.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(VendorBindingModel model) + { + CheckModel(model); + if (_vendorStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(VendorBindingModel model) + { + CheckModel(model); + if (_vendorStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(VendorBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_vendorStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(VendorBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина работника", nameof(model.Login)); + } + var element = _vendorStorage.GetElement(new VendorSearchModel + { + Login = model.Login + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Работник с таким логином уже есть"); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs index e069ba1..ed748fe 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs @@ -3,7 +3,7 @@ public class StoreKeeperSearchModel { public int? Id { get; set; } - public string? Email { get; set; } + public string? Login { get; set; } public string? Password { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs index f15394a..fc54068 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs @@ -3,7 +3,7 @@ public class VendorSearchModel { public int? Id { get; set; } - public string? Email { get; set; } + public string? Login { get; set; } public string? Password { get; set; } } }