using Microsoft.Extensions.Logging; using SewingDressesContracts.BindingModels; using SewingDressesContracts.BusinessLogicsContracts; using SewingDressesContracts.SearchModels; using SewingDressesContracts.StoragesContracts; using SewingDressesContracts.ViewModels; namespace SewingDressesBusinessLogic.BusinessLogic { public class DressLogic : IDressLogic { private readonly ILogger _logger; private readonly IDressStorage _dressStorage; public DressLogic(ILogger logger, IDressStorage dressStorage) { _logger = logger; _dressStorage = dressStorage; } public List? ReadList(DressSearchModel? model) { _logger.LogInformation("ReadList. DressName:{DressName}. Id:{Id}", model?.DressName, model?.Id); var list = model == null ? _dressStorage.GetFullList() : _dressStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public DressViewModel? ReadElement(DressSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. DressName:{DressName}. Id: {Id}", model.DressName, model.Id); var element = _dressStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } private void CheckModel(DressBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.DressName)) { throw new ArgumentNullException("Нет названия платья", nameof(model.DressComponents)); } if (model.Price <= 0) { throw new ArgumentNullException("Цена платья должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("Dress. DressName:{DressName}. Price:{Price}. Id:{Id}", model.DressName, model.Price, model.Id); var element = _dressStorage.GetElement(new DressSearchModel { DressName = model.DressName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Платье с таким названием уже есть"); } } public bool Create(DressBindingModel model) { CheckModel(model); if (_dressStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(DressBindingModel model) { CheckModel(model); if (_dressStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(DressBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_dressStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } } }