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? 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("Аккаунт с таким логином уже есть"); } } } }