From 5fcd7a7018f8fc2d44998479e745d731f72cae1e Mon Sep 17 00:00:00 2001 From: ker73rus Date: Fri, 7 Apr 2023 19:30:31 +0400 Subject: [PATCH] fix --- .../BusinessLogics/UserLogic.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/UniversityBusinessLogic/BusinessLogics/UserLogic.cs b/UniversityBusinessLogic/BusinessLogics/UserLogic.cs index 866aa64..8b522ea 100644 --- a/UniversityBusinessLogic/BusinessLogics/UserLogic.cs +++ b/UniversityBusinessLogic/BusinessLogics/UserLogic.cs @@ -13,6 +13,85 @@ 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("Пользователь с таким логином уже есть"); + } + } } }