SUBD-Petrushin-Egor-PIbd-22/TaskTrackerBusinessLogics/BusinessLogic/UserLogic.cs

111 lines
3.1 KiB
C#
Raw Normal View History

2024-05-13 14:29:34 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.BusinessLogicsContracts;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
namespace TaskTrackerBusinessLogics.BusinessLogic
{
public class UserLogic : IUserLogic
{
private readonly IUserStorage _userStorage;
public UserLogic(IUserStorage userStorage)
{
_userStorage = userStorage;
}
public List<UserViewModel>? ReadList(UserSearchModel? model)
{
var list = model == null ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
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 bool Create(UserBindingModel model)
{
CheckModel(model);
if (_userStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(UserBindingModel model)
{
CheckModel(model);
if (_userStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(UserBindingModel model)
{
CheckModel(model, false);
if (_userStorage.Delete(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.UserFIO))
{
throw new ArgumentNullException("Нет ФИО", nameof(model.UserFIO));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет логина(почты)", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля", nameof(model.Password));
}
var element = _userStorage.GetElement(new UserSearchModel
{
Email = model.Email
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Пользователя с таким логином(почтой) уже есть");
}
}
}
}