2023-04-07 01:01:36 +04:00
|
|
|
|
using BankContracts.BusinessLogicsContracts;
|
2023-04-07 01:58:12 +04:00
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StorageContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-04-07 01:01:36 +04:00
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class ClientLogic : IClientLogic
|
|
|
|
|
{
|
2023-04-07 01:58:12 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IClientStorage _clientStorage;
|
|
|
|
|
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_clientStorage = clientStorage;
|
|
|
|
|
}
|
|
|
|
|
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
|
|
|
|
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
public ClientViewModel? ReadElement(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
|
|
|
|
|
var element = _clientStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement found. Id: {Id}", element.Id);
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
public bool Create(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_clientStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Update(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_clientStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Delete(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
if (_clientStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
private void CheckModel(ClientBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//пустой логин
|
|
|
|
|
if (string.IsNullOrEmpty(model.Login))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Login is empty");
|
|
|
|
|
throw new ArgumentException("Не введён логин");
|
|
|
|
|
}
|
|
|
|
|
//Длинна логина не больше 20
|
|
|
|
|
if (model.Login.Length > 20)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Login {Login} length > 20", model.Login);
|
|
|
|
|
throw new ArgumentException("Длина логина не должна превышать 20 символов");
|
|
|
|
|
}
|
|
|
|
|
//Проверяем логин на уникальность
|
|
|
|
|
var existingClient = _clientStorage.GetElement(new()
|
|
|
|
|
{
|
|
|
|
|
Login = model.Login
|
|
|
|
|
});
|
|
|
|
|
if (existingClient != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Client with login {Login} already exists", model.Login);
|
|
|
|
|
throw new ArgumentException("клиент с таким логином уже существует");
|
|
|
|
|
}
|
|
|
|
|
//Пустой пароль
|
|
|
|
|
if (string.IsNullOrEmpty(model.Password))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Password is empty");
|
|
|
|
|
throw new ArgumentException("Не введён пароль");
|
|
|
|
|
}
|
|
|
|
|
//Пароль не должен быть менее 5 и более 32 символов
|
|
|
|
|
if (model.Password.Length < 5 || model.Password.Length > 32)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Password {Password} length > 32 or < 5", model.Password);
|
|
|
|
|
throw new ArgumentException("Длина пароля должна быть в промежутке между 5 и 32 символами");
|
|
|
|
|
}
|
|
|
|
|
//Пустое имя
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Name is empty");
|
|
|
|
|
throw new ArgumentException("Не введено имя");
|
|
|
|
|
}
|
|
|
|
|
//Имя должно быть не более 32 символов
|
|
|
|
|
if (model.Name.Length > 32)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Name {Name} length > 32", model.Name);
|
|
|
|
|
throw new ArgumentException("Длина имени не должна превышать 32 символов");
|
|
|
|
|
}
|
|
|
|
|
//пустая фамилия
|
|
|
|
|
if (string.IsNullOrEmpty(model.Surname))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Surname is empty");
|
|
|
|
|
throw new ArgumentException("Не введена фамилия");
|
|
|
|
|
}
|
|
|
|
|
//Фамилия должна быть не более 32 символов
|
|
|
|
|
if (model.Surname.Length > 32)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Surname {Surname} length > 32", model.Surname);
|
|
|
|
|
throw new ArgumentException("Длина фамилии не должна превышать 32 символов");
|
|
|
|
|
}
|
|
|
|
|
//пустое отчество
|
|
|
|
|
if (string.IsNullOrEmpty(model.Patronymic))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Patronymic is empty");
|
|
|
|
|
throw new ArgumentException("Не введено отчество");
|
|
|
|
|
}
|
|
|
|
|
//Отчество должна быть не более 32 символов
|
|
|
|
|
if (model.Patronymic.Length > 32)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Patronymic {Patronymic} length > 32", model.Patronymic);
|
|
|
|
|
throw new ArgumentException("Длина отчества не должна превышать 32 символов");
|
|
|
|
|
}
|
|
|
|
|
//Пустой емаил
|
|
|
|
|
if (string.IsNullOrEmpty(model.Email))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Email is empty");
|
|
|
|
|
throw new ArgumentException("Не введён Email");
|
|
|
|
|
}
|
|
|
|
|
//Фамилия должна быть не более 32 символов
|
|
|
|
|
if (model.Email.Length > 32)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Email {Email} length > 32", model.Email);
|
|
|
|
|
throw new ArgumentException("Длина Email не должна превышать 32 символов");
|
|
|
|
|
}
|
|
|
|
|
//Проверяем почту на уникальность
|
|
|
|
|
var existingEmail = _clientStorage.GetElement(new()
|
|
|
|
|
{
|
|
|
|
|
Email = model.Email
|
|
|
|
|
});
|
|
|
|
|
if (existingEmail != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Client with Email {Email} already exists", model.Email);
|
|
|
|
|
throw new ArgumentException("клиент с такой почтой уже существует");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Client. Id: {Id}", model.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-04-07 01:01:36 +04:00
|
|
|
|
}
|