using PortalAccountsContracts.BindingModels; using PortalAccountsContracts.BusinessLogicsContracts; using PortalAccountsContracts.SearchModels; using PortalAccountsContracts.StoragesContracts; using PortalAccountsContracts.ViewModels; namespace PortalAccountsBusinessLogic.BusinessLogics { public class RoleLogic : IRoleLogic { private readonly IRoleStorage _roleStorage; public RoleLogic(IRoleStorage RoleStorage) { _roleStorage = RoleStorage; } public List? ReadList(RoleSearchModel? model) { return model == null ? _roleStorage.GetFullList() : _roleStorage.GetFilteredList(model); } public RoleViewModel? ReadElement(RoleSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } return _roleStorage.GetElement(model); } public bool Create(RoleBindingModel model) { CheckModel(model); return _roleStorage.Insert(model) != null; } public bool Update(RoleBindingModel model) { CheckModel(model); return _roleStorage.Update(model) != null; } public bool Delete(RoleBindingModel model) { CheckModel(model, false); return _roleStorage.Delete(model) != null; } 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)); } var element = _roleStorage.GetElement(new RoleSearchModel { Name = model.Name }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Роль с таким названием уже есть"); } } } }