using BlogContracts.BindingModel; using BlogContracts.BusinessLogicContracts; using BlogContracts.SearchModels; using BlogContracts.StorageContracts; using BlogContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BusinessLogic { public class UserLogic : IUserLogic { private readonly IUserStorage _userStorage; public UserLogic(IUserStorage UserStorage) { _userStorage = UserStorage ?? throw new ArgumentNullException(nameof(UserStorage)); } public bool Create(UserBindingModel model) { CheckModel(model); if (_userStorage.Insert(model) == null) { return false; } return true; } public bool Delete(UserBindingModel model) { CheckModel(model); if (_userStorage.Delete(model) == null) { return false; } return true; } public UserViewModel? ReadElement(UserSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _userStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(UserSearchModel? model) { var list = model == null ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(UserBindingModel model) { CheckModel(model); if (_userStorage.Update(model) == null) { return false; } return true; } private void CheckModel(UserBindingModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentException("Отсутвует имя", nameof(model.Name)); } if (string.IsNullOrEmpty(model.DateCreate)) { throw new ArgumentException("Отсутвует дата создания аккаунта", nameof(model.DateCreate)); } if (_userStorage.GetElement(new UserSearchModel { Name = model.Name, }) != null) { throw new InvalidOperationException("Такой пользователь уже существует"); } } } }