using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.SearchModels; using DressAtelierContracts.StorageContracts; using DressAtelierContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DressAtelierBusinessLogic.BusinessLogic { public class MaterialLogic : IMaterialLogic { private readonly ILogger _logger; private readonly IMaterialStorage _componentStorage; public MaterialLogic(ILogger logger, IMaterialStorage componentStorage) { _logger = logger; _componentStorage = componentStorage; } public bool Create(MaterialBindingModel model) { CheckModel(model); if (_componentStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(MaterialBindingModel model) { CheckModel(model); if (_componentStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(MaterialBindingModel 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; } public MaterialViewModel? ReadElement(MaterialSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. ComponentName:{ComponentName}.ID:{ ID}", model.ComponentName, 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 List? ReadList(MaterialSearchModel? model) { _logger.LogInformation("ReadList. ComponentName:{ComponentName}. ID:{ ID}", model?.ComponentName, 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; } private void CheckModel(MaterialBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ComponentName)) { throw new ArgumentNullException("Invalid name of component", nameof(model.ComponentName)); } if (model.Cost <= 0) { throw new ArgumentNullException("Price of component should be higher than 0", nameof(model.Cost)); } _logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. ID: { ID} ", model.ComponentName, model.Cost, model.ID); var element = _componentStorage.GetElement(new MaterialSearchModel { ComponentName = model.ComponentName }); if (element != null && element.ID != model.ID) { throw new InvalidOperationException("Component with such name already exists."); } } } }