150 lines
4.8 KiB
C#
150 lines
4.8 KiB
C#
using CarServiceContracts.BindingModels;
|
||
using CarServiceContracts.BusinessLogicsContracts;
|
||
using CarServiceContracts.SearchModels;
|
||
using CarServiceContracts.StorageContracts;
|
||
using CarServiceContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace CarServiceBusinessLogic.BusinessLogics
|
||
{
|
||
public class WorkerLogic : IWorkerLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IWorkerStorage _workerStorage;
|
||
public WorkerLogic(ILogger<WorkerLogic> logger, IWorkerStorage workerStorage)
|
||
{
|
||
_logger = logger;
|
||
_workerStorage = workerStorage;
|
||
}
|
||
public List<WorkerViewModel>? ReadList(WorkerSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
||
var list = model == null ? _workerStorage.GetFullList() : _workerStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
||
return list;
|
||
}
|
||
public WorkerViewModel? ReadElement(WorkerSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
|
||
var element = _workerStorage.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(WorkerBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_workerStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(WorkerBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_workerStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(WorkerBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
||
if (_workerStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(WorkerBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
//Введён ли логин?
|
||
if (string.IsNullOrEmpty(model.Login))
|
||
{
|
||
_logger.LogWarning("Login is empty");
|
||
throw new ArgumentException("Не введён логин");
|
||
}
|
||
//Логин не должен быть больше 30 символов
|
||
if (model.Login.Length > 30)
|
||
{
|
||
_logger.LogWarning("Login {Login} length > 30", model.Login);
|
||
throw new ArgumentException("Длина логина не должна превышать 30 символов");
|
||
}
|
||
//Проверяем логин на уникальность
|
||
var existingWorker = _workerStorage.GetElement(new()
|
||
{
|
||
Login = model.Login
|
||
});
|
||
if (existingWorker != null)
|
||
{
|
||
_logger.LogWarning("Worker with login {Login} already exists", model.Login);
|
||
throw new ArgumentException("Сотрудник с таким логином уже существует");
|
||
}
|
||
//Введён ли пароль?
|
||
if (string.IsNullOrEmpty(model.Password))
|
||
{
|
||
_logger.LogWarning("Password is empty");
|
||
throw new ArgumentException("Не введён пароль");
|
||
}
|
||
//Пароль не должен быть менее 7 и более 30 символов
|
||
if (model.Password.Length < 7 || model.Password.Length > 30)
|
||
{
|
||
_logger.LogWarning("Password {Password} length > 30 or < 7", model.Password);
|
||
throw new ArgumentException("Длина пароля должна быть в промежутке между 7 и 30 символами");
|
||
}
|
||
//Введено ли имя?
|
||
if (string.IsNullOrEmpty(model.Name))
|
||
{
|
||
_logger.LogWarning("Name is empty");
|
||
throw new ArgumentException("Не введёно имя");
|
||
}
|
||
//Имя должно быть не более 30 символов
|
||
if (model.Name.Length > 30)
|
||
{
|
||
_logger.LogWarning("Name {Name} length > 30", model.Name);
|
||
throw new ArgumentException("Длина имени не должна превышать 30 символов");
|
||
}
|
||
//Введена ли фамилия?
|
||
if (string.IsNullOrEmpty(model.Surname))
|
||
{
|
||
_logger.LogWarning("Surname is empty");
|
||
throw new ArgumentException("Не введёна фамилия");
|
||
}
|
||
//Фамилия должна быть не более 30 символов
|
||
if (model.Surname.Length > 30)
|
||
{
|
||
_logger.LogWarning("Surname {Surname} length > 30", model.Surname);
|
||
throw new ArgumentException("Длина фамилии не должна превышать 30 символов");
|
||
}
|
||
_logger.LogInformation("Worker. Id: {Id}", model.Id);
|
||
}
|
||
}
|
||
}
|