PIbd-31_Putincev.D.M._COP_29/COP/PortalAccountsBusinessLogic/BusinessLogics/AccountLogic.cs

78 lines
2.5 KiB
C#
Raw Normal View History

2024-11-19 23:18:49 +04:00
using PortalAccountsContracts.BindingModels;
using PortalAccountsContracts.BusinessLogicsContracts;
using PortalAccountsContracts.SearchModels;
using PortalAccountsContracts.StoragesContracts;
using PortalAccountsContracts.ViewModels;
namespace PortalAccountsBusinessLogic.BusinessLogics
{
public class AccountLogic : IAccountLogic
{
private readonly IAccountStorage _accountStorage;
public AccountLogic(IAccountStorage AccountStorage)
{
_accountStorage = AccountStorage;
}
public List<AccountViewModel>? ReadList(AccountSearchModel? model)
{
return model == null ? _accountStorage.GetFullList() : _accountStorage.GetFilteredList(model);
}
public AccountViewModel? ReadElement(AccountSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
return _accountStorage.GetElement(model);
}
public bool Create(AccountBindingModel model)
{
CheckModel(model);
return _accountStorage.Insert(model) != null;
}
public bool Update(AccountBindingModel model)
{
CheckModel(model);
return _accountStorage.Update(model) != null;
}
public bool Delete(AccountBindingModel model)
{
CheckModel(model, false);
return _accountStorage.Delete(model) != null;
}
private void CheckModel(AccountBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина у аккаунта", nameof(model.Login));
}
if (model.InterestId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор интерес", nameof(model.InterestId));
}
var element = _accountStorage.GetElement(new AccountSearchModel
{
Login = model.Login
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Аккаунт с таким логином уже есть");
}
}
}
}