using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.BusinessLogicsContracts; using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.StoragesContract; using PrecastConcretePlantContracts.ViewModels; using Microsoft.Extensions.Logging; namespace ConfectioneryBusinessLogic.BusinessLogic { public class ReinforcedLogic : IReinforcedLogic { private readonly ILogger _logger; private readonly IReinforcedStorage _componentStorage; public ReinforcedLogic(ILogger logger, IReinforcedStorage componentStorage) { _logger = logger; _componentStorage = componentStorage; } public List? ReadList(ReinforcedSearchModel? model) { _logger.LogInformation("ReadList. ReinforcedName:{ReinforcedName}.Id:{ Id} ", model?.ReinforcedName, model?.Id); var list = (model == null) ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public ReinforcedViewModel? ReadElement(ReinforcedSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. ReinforcedName:{ReinforcedName}.Id:{ Id}", model.ReinforcedName, model.Id); var element = _componentStorage.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(ReinforcedBindingModel model) { CheckModel(model); if (_componentStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(ReinforcedBindingModel model) { CheckModel(model); if (_componentStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(ReinforcedBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_componentStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(ReinforcedBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ReinforcedName)) { throw new ArgumentNullException("Нет названия железобетонного изделия", nameof(model.ReinforcedName)); } if (model.Price <= 0) { throw new ArgumentNullException("Цена железобетонного изделия должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("Reinforced. ReinforcedName:{ReinforcedName}.Price:{ Price}. Id: { Id}", model.ReinforcedName, model.Price, model.Id); var element = _componentStorage.GetElement(new ReinforcedSearchModel { ReinforcedName = model.ReinforcedName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Железобетонное изделие с таким названием уже есть"); } } } }