useradd #3

Merged
ker73rus merged 2 commits from myprov into provider 2023-04-07 19:32:19 +04:00
Showing only changes of commit 5fcd7a7018 - Show all commits

View File

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