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

78 lines
2.5 KiB
C#
Raw Permalink 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 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("Аккаунт с таким логином уже есть");
}
}
}
}