using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UniversityContracts.BindingModels; using UniversityContracts.BusinessLogicContracts; using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; namespace UniversityBusinessLogic.BusinessLogics { public class UserLogic : IUserLogic { private readonly IUserStorage _userStorage; public UserLogic(IUserStorage userStorage) { _userStorage = userStorage; } public bool Create(UserBindingModel model) { CheckModel(model); if (_userStorage.Insert(model) == null) { return false; } return true; } public bool Delete(UserBindingModel model) { CheckModel(model, false); if (_userStorage.Delete(model) == null) { return false; } return true; } public UserViewModel? ReadElement(UserSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var user = _userStorage.GetElement(model); if (user == null) { return null; } return user; } 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, false); if (_userStorage.Update(model) == null) { return false; } return true; } private void CheckModel(UserBindingModel 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)); } var user = _userStorage.GetElement(new UserSearchModel{Login = model.Login}); if (user != null) { throw new InvalidOperationException("Пользователь с таким логином уже есть"); } } } }