using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.BusinessLogicContracts; using ConstructionCompanyContracts.SearchModels; using ConstructionCompanyContracts.StorageContracts; using ConstructionCompanyContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConstructionCompanyBusinessLogic.BusinessLogics { public class MaterialOrderLogic : IMaterialOrderLogic { private readonly ILogger _logger; private readonly IMaterialOrderStorage _employeeOrderStorage; public MaterialOrderLogic(ILogger logger, IMaterialOrderStorage employeeOrderStorage) { _logger = logger; _employeeOrderStorage = employeeOrderStorage; } public List? ReadList(MaterialOrderSearchModel? model) { _logger.LogInformation("ReadList. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId); var list = model == null ? _employeeOrderStorage.GetFullList() : _employeeOrderStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public MaterialOrderViewModel? ReadElement(MaterialOrderSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadList. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId); var element = _employeeOrderStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId); return element; } public bool Create(MaterialOrderBindingModel model) { CheckModel(model); if (_employeeOrderStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(MaterialOrderBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId); if (_employeeOrderStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(MaterialOrderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.Quantity <= 0) { throw new InvalidOperationException("Количество должна быть больше 0!"); } _logger.LogInformation("MaterialOrder. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId); } } }