PIbd-22-Ismailov_SUBD/BlogDataModels/BusinessLogic/RoleLogic.cs
2023-09-06 22:02:39 +04:00

109 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BlogContracts.BindingModels;
using BlogContracts.BusinessLogicContracts;
using BlogContracts.SearchModels;
using BlogContracts.StoragesContracts;
using BlogContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlogBusinessLogic
{
public class RoleLogic : IRoleLogic
{
private readonly IRoleStorage _roleStorage;
public RoleLogic(IRoleStorage roleStorage)
{
_roleStorage = roleStorage;
}
public bool Create(RoleBindingModel model)
{
CheckModel(model);
if (_roleStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(RoleBindingModel model)
{
CheckModel(model, false);
if (_roleStorage.Delete(model) == null)
{
return false;
}
return true;
}
public RoleViewModel? ReadElement(RoleSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _roleStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<RoleViewModel>? ReadList(RoleSearchModel? model)
{
var list = model == null ? _roleStorage.GetFullList() : _roleStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public void RoleInsertList(int num)
{
_roleStorage.RoleInsertList(num);
}
public bool Update(RoleBindingModel model)
{
CheckModel(model);
if (_roleStorage.Update(model) == null)
{
return false;
}
return true;
}
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("Роль с таким названием уже есть");
}
}
}
}