2024-04-28 21:02:22 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.BusinessLogicContracts;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2024-04-28 21:55:25 +04:00
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogics.BusinessLogic
|
|
|
|
|
{
|
2024-04-28 21:02:22 +04:00
|
|
|
|
public class EmployeeLogic : IEmployeeLogic
|
2024-04-28 21:55:25 +04:00
|
|
|
|
{
|
2024-04-28 21:02:22 +04:00
|
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IEmployeeStorage _employeeStorage;
|
|
|
|
|
public EmployeeLogic(ILogger<EmployeeLogic> logger, IEmployeeStorage employeeStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_employeeStorage = employeeStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(EmployeeBindingModel model, bool withParams = true)
|
|
|
|
|
{/*
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.PhoneNumber))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model.PhoneNumber), "Нет логина клиента");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Password))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model.Password), "Нет пароля клиента");
|
|
|
|
|
}
|
|
|
|
|
if (model.PhoneNumber.Length is < 12)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(nameof(model.PhoneNumber), "Длина номера телефона должна быть 11 цифр");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.Password.Length < 5)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(nameof(model.Password),
|
|
|
|
|
"Пароль пользователя должен быть не менее 5 символов");
|
|
|
|
|
}
|
|
|
|
|
if (!Regex.IsMatch(model.Password, "[0-9]+"))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(nameof(model.Password),
|
|
|
|
|
"Пароль пользователя должен содержать хотя бы одну цифру");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogDebug("{level} Проверка логина пользователя на уникальность {@Employee}", model);
|
|
|
|
|
var element = _employeeStorage.GetElement(new EmployeeSearchModel
|
|
|
|
|
{
|
|
|
|
|
PhoneNumber = model.PhoneNumber,
|
|
|
|
|
});
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("С номером {PhoneNumber}, уже есть пользователь: {@ExistEmployee}", model.PhoneNumber, element);
|
|
|
|
|
throw new InvalidOperationException($"Сотрудник с таким номером телефона уже есть");
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Create(EmployeeBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
var result = _employeeStorage.Insert(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Сотрудник не создался");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Создана сущность: {@EmployeeViewModel}", result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Ошибка при попытки создать элемент по {@EmployeeBindingModel} модели", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmployeeViewModel ReadElement(EmployeeSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _employeeStorage.GetElement(model);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"Результат получения элемента с id {model.Id} оказался нулевым");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Извлечение элемента {@EmployeeViewModel} c сотрудника по модели: {@EmployeeSearchModel}", result, model);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Произошла ошибка при попытки получить элемент по модели: {@EmployeeSearchModel}", model);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-28 21:55:25 +04:00
|
|
|
|
}
|
|
|
|
|
}
|