using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ElectronicsShopBusinessLogic.BusinessLogic { public class RoleLogic : IRoleLogic { private readonly ILogger _logger; private readonly IRoleStorage _storage; public RoleLogic(ILogger logger, IRoleStorage storage) { _logger = logger; _storage = storage; } public bool Create(RoleBindingModel model) { CheckModel(model); if (_storage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(RoleBindingModel model) { CheckModel(model, false); _logger.LogInformation($"Delete. ID:{model.ID}"); if (_storage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public bool Update(RoleBindingModel model) { CheckModel(model); if (_storage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public List? ReadList(RoleSearchModel? model) { _logger.LogInformation($"ReadList. ID:{model?.ID}"); var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation($"ReadList. Count:{list.Count}"); return list; } private void CheckModel(RoleBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Нет названия категории продукта", nameof(model.Name)); } _logger.LogInformation($"CategoryProduct. ID:{model.ID}.Name:{model.Name}"); var element = _storage.GetElement(new RoleSearchModel { Name = model.Name }); if (element != null && element.ID != model.ID) { throw new InvalidOperationException("Такая роль уже есть"); } } } }