PIbd-31_Rodionov.I.A._COP_28/COP/PortalAccountsBusinessLogic/BusinessLogics/RoleLogic.cs

74 lines
2.2 KiB
C#
Raw Normal View History

2024-10-22 19:42:03 +04:00
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<RoleViewModel>? 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("Роль с таким названием уже есть");
}
}
}
}