using AccountsContracts.BindingModels; using AccountsContracts.BusinessLogicContracts; using AccountsContracts.SearchModels; using AccountsContracts.StorageContracts; using AccountsContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AccountsBusinessLogic.BusinessLogics { public class AccountLogic : IAccountLogic { private readonly IAccountStorage _accountStorage; public AccountLogic(IAccountStorage accountStorage) { _accountStorage = accountStorage; } public List? ReadList(AccountSearchModel? model) { var list = model == null ? _accountStorage.GetFullList() : _accountStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public AccountViewModel? ReadElement(AccountSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _accountStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(AccountBindingModel model) { CheckModel(model); if (_accountStorage.Insert(model) == null) { return false; } return true; } public bool Update(AccountBindingModel model) { CheckModel(model); if (_accountStorage.Update(model) == null) { return false; } return true; } public bool Delete(AccountBindingModel model) { CheckModel(model, false); if (_accountStorage.Delete(model) == null) { return false; } return true; } 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 (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); } if (string.IsNullOrEmpty(model.Email)) { throw new ArgumentNullException("Нет электронной почты пользователя", nameof(model.Email)); } } } }