diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/BuildLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/BuildLogic.cs new file mode 100644 index 0000000..1e4064b --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/BuildLogic.cs @@ -0,0 +1,113 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class BuildLogic : IBuildLogic + { + private readonly ILogger _logger; + private readonly IBuildStorage _buildStorage; + + public BuildLogic(ILogger logger, IBuildStorage buildStorage) + { + _logger = logger; + _buildStorage = buildStorage; + } + + public List? ReadList(BuildSearchModel? model) + { + _logger.LogInformation("ReadList Build. Name:{Name}. Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _buildStorage.GetFullList() : + _buildStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList Build return null list"); + return null; + } + _logger.LogInformation("ReadList Build. Count:{Count}", list.Count); + return list; + } + + public BuildViewModel? ReadElement(BuildSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement Build. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _buildStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement Build element not found"); + return null; + } + _logger.LogInformation("ReadElement Build find. Id:{Id}", element.Id); + return element; + } + + public bool Create(BuildBindingModel model) + { + CheckModel(model); + if (_buildStorage.Insert(model) == null) + { + _logger.LogWarning("Insert Build operation failed"); + return false; + } + return true; + } + + public bool Update(BuildBindingModel model) + { + CheckModel(model); + if (_buildStorage.Update(model) == null) + { + _logger.LogWarning("Update Build operation failed"); + return false; + } + return true; + } + + public bool Delete(BuildBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_buildStorage.Delete(model) == null) + { + _logger.LogWarning("Delete Build operation failed"); + return false; + } + return true; + } + private void CheckModel(BuildBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия сборки", nameof(model.Name)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена сборки должна быть больше 0", nameof(model.Price)); + } + var element = _buildStorage.GetElement(new BuildSearchModel + { + Name = model.Name + }); + if (element != null) + { + throw new InvalidOperationException("Сборка с таким названием уже есть"); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/CommentLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/CommentLogic.cs new file mode 100644 index 0000000..6764089 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/CommentLogic.cs @@ -0,0 +1,101 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class CommentLogic : ICommentLogic + { + private readonly ILogger _logger; + private readonly ICommentStorage _commentStorage; + + public CommentLogic(ILogger logger, ICommentStorage commentStorage) + { + _logger = logger; + _commentStorage = commentStorage; + } + + public List? ReadList(CommentSearchModel? model) + { + _logger.LogInformation("ReadList Comment. Date:{Date}. Id:{Id}", model?.Date, model?.Id); + var list = model == null ? _commentStorage.GetFullList() : + _commentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList Comment return null list"); + return null; + } + _logger.LogInformation("ReadList Comment. Count:{Count}", list.Count); + return list; + } + + public CommentViewModel? ReadElement(CommentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement Comment. Date:{Date}. Id:{ Id}", model.Date, model.Id); + var element = _commentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement Comment element not found"); + return null; + } + _logger.LogInformation("ReadElement Comment find. Id:{Id}", element.Id); + return element; + } + + public bool Create(CommentBindingModel model) + { + CheckModel(model); + if (_commentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert Comment operation failed"); + return false; + } + return true; + } + + public bool Update(CommentBindingModel model) + { + CheckModel(model); + if (_commentStorage.Update(model) == null) + { + _logger.LogWarning("Update Comment operation failed"); + return false; + } + return true; + } + + public bool Delete(CommentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_commentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete Comment operation failed"); + return false; + } + return true; + } + private void CheckModel(CommentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Date)) + { + throw new ArgumentNullException("Нет даты у комментария", nameof(model.Date)); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs new file mode 100644 index 0000000..ce693a0 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs @@ -0,0 +1,105 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class PurchaseLogic : IPurchaseLogic + { + private readonly ILogger _logger; + private readonly IPurchaseStorage _purchaseStorage; + + public PurchaseLogic(ILogger logger, IPurchaseStorage purchaseStorage) + { + _logger = logger; + _purchaseStorage = purchaseStorage; + } + + public List? ReadList(PurchaseSearchModel? model) + { + _logger.LogInformation("ReadList Purchase. Id:{ Id}", model?.Id); + var list = model == null ? _purchaseStorage.GetFullList() : + _purchaseStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList Purchase return null list"); + return null; + } + _logger.LogInformation("ReadList Purchase. Count:{Count}", list.Count); + return list; + } + + public PurchaseViewModel? ReadElement(PurchaseSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement Purchase. Id:{ Id}", model.Id); + var element = _purchaseStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement Purchase element not found"); + return null; + } + _logger.LogInformation("ReadElement Purchase find. Id:{Id}", element.Id); + return element; + } + + public bool Create(PurchaseBindingModel model) + { + CheckModel(model); + if (_purchaseStorage.Insert(model) == null) + { + _logger.LogWarning("Insert Purchase operation failed"); + return false; + } + return true; + } + + public bool Update(PurchaseBindingModel model) + { + CheckModel(model); + if (_purchaseStorage.Update(model) == null) + { + _logger.LogWarning("Update Purchase operation failed"); + return false; + } + return true; + } + + public bool Delete(PurchaseBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_purchaseStorage.Delete(model) == null) + { + _logger.LogWarning("Delete Purchase operation failed"); + return false; + } + return true; + } + private void CheckModel(PurchaseBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.DateCreate)) + { + throw new ArgumentNullException("Нет даты создания покупки", nameof(model.Date)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Стоимость покупки должна быть больше 0", nameof(model.Cost)); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs index 1e27ca0..af08205 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs @@ -5,10 +5,9 @@ namespace ComputerHardwareStoreContracts.BindingModels public class PurchaseBindingModel : IPurchaseModel { public int Id { get; set; } + public int VendorId { get; set; } public double Cost { get; set; } public DateTime DateCreate { get; set; } - public int VendorId { get; set; } - public double Sum { get; set; } public Dictionary PurchaseBuild { get; set; } = new(); public Dictionary PurchaseProduct { get; set; } = new(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs index 3ad2c4a..49022a5 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs @@ -3,6 +3,6 @@ public class BuildSearchModel { public int? Id { get; set; } - public string? BuildName { get; set; } + public string? Name { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs index 9e8ffb6..9afa7b6 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs @@ -2,10 +2,9 @@ { public interface IPurchaseModel : IId { + int VendorId { get; } double Cost { get; } DateTime DateCreate { get; } - int VendorId { get; } - double Sum { get; } public Dictionary PurchaseBuild { get; } public Dictionary PurchaseProduct { get; } }