using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.BusinessLogicContracts; using ComputerStoreContracts.SearchModels; using ComputerStoreContracts.StorageContracts; using ComputerStoreContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ComputerStoreBusinessLogic.BusinessLogic { public class ConsignmentLogic : IConsignmentLogic { private readonly ILogger _logger; private readonly IConsignmentStorage _consignmentStorage; public ConsignmentLogic(ILogger logger, IConsignmentStorage consignmentStorage) { _logger = logger; _consignmentStorage = consignmentStorage; } public bool Create(ConsignmentBindingModel model) { CheckModel(model); if (_consignmentStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(ConsignmentBindingModel model) { CheckModel(model); if (_consignmentStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(ConsignmentBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. ID:{ID}", model.ID); if (_consignmentStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public ConsignmentViewModel? ReadElement(ConsignmentSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Consignment ID:{ ID}", model.ID); var element = _consignmentStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Consignment ID:{ID}", element.ID); return element; } public List? ReadList(ConsignmentSearchModel? model) { _logger.LogInformation("ReadList. Consignment ID:{ ID}", model?.ID); var list = model == null ? _consignmentStorage.GetFullList() : _consignmentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } private void CheckModel(ConsignmentBindingModel model, bool withParams = true) { if (model == null) { return; } if (!withParams) { return; } if (model.Price <= 0) { throw new ArgumentNullException("Invalid consignment's price", nameof(model)); } if (string.IsNullOrEmpty(model.OrderID.ToString())) { throw new ArgumentNullException("Invalid Consignment's order ID", nameof(model)); } _logger.LogInformation("Consignment. Consignment ID:{ ID}. Order ID:{ OrderID}.", model.ID, model.OrderID); } } }