PIbd-21_Pyatakov_KM_Markov_.../UniversityBusinessLogic/BusinessLogics/UserLogic.cs

98 lines
2.7 KiB
C#
Raw Normal View History

2023-04-07 19:22:59 +04:00
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
{
2023-04-07 19:30:31 +04:00
private readonly IUserStorage _userStorage;
2023-04-07 19:22:59 +04:00
2023-04-07 19:30:31 +04:00
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("Пользователь с таким логином уже есть");
}
}
2023-04-07 19:22:59 +04:00
}
}