2023-04-12 16:52:32 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
2023-03-29 23:33:45 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2023-05-09 15:00:49 +04:00
|
|
|
|
using System.Text.Json;
|
2023-03-29 23:33:45 +04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using TransportCompanyContracts.BindingModels;
|
|
|
|
|
using TransportCompanyContracts.BusinessLogicsContracts;
|
|
|
|
|
using TransportCompanyContracts.SearchModels;
|
2023-04-12 16:52:32 +04:00
|
|
|
|
using TransportCompanyContracts.StoragesContracts;
|
2023-03-29 23:33:45 +04:00
|
|
|
|
using TransportCompanyContracts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace TransportCompanyBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class ClientLogic : IClientLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2023-04-12 16:52:32 +04:00
|
|
|
|
private readonly IClientStorage _clientStorage;
|
|
|
|
|
|
|
|
|
|
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
|
2023-03-29 23:33:45 +04:00
|
|
|
|
{
|
2023-04-12 16:52:32 +04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_clientStorage = clientStorage;
|
2023-03-29 23:33:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
|
|
|
|
|
{
|
2023-04-12 16:52:32 +04:00
|
|
|
|
_logger.LogInformation("ReadList. Surname:{Surname}. Id:{Id}", model?.Surname, model?.Id);
|
|
|
|
|
|
|
|
|
|
//list хранит весь список в случае, если model пришло со значением null на вход метода
|
|
|
|
|
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. Surname:{Surname}. Id:{Id}", model.Surname, model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _clientStorage.GetElement(model);
|
|
|
|
|
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
return element;
|
2023-03-29 23:33:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Create(ClientBindingModel model)
|
|
|
|
|
{
|
2023-04-12 16:52:32 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_clientStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Create operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-03-29 23:33:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 23:44:39 +04:00
|
|
|
|
//для замера времени рандомного добавления
|
2023-04-26 11:31:32 +04:00
|
|
|
|
public string TestRandomCreate(int count, string[] _name, string[] _surname, string[] _patronymic, string[] _telephone, string[] _email)
|
|
|
|
|
{
|
|
|
|
|
return _clientStorage.TestRandomInsert(count, _name, _surname, _patronymic, _telephone, _email);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 23:44:39 +04:00
|
|
|
|
//для проверки времени выполнения сложного запроса
|
|
|
|
|
public string TestSecondJoin()
|
|
|
|
|
{
|
|
|
|
|
return _clientStorage.SecondJoin();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 11:31:32 +04:00
|
|
|
|
public bool Update(ClientBindingModel model)
|
2023-03-29 23:33:45 +04:00
|
|
|
|
{
|
2023-04-12 16:52:32 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_clientStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-03-29 23:33:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 23:44:39 +04:00
|
|
|
|
public bool Delete(ClientBindingModel model)
|
2023-03-29 23:33:45 +04:00
|
|
|
|
{
|
2023-04-12 16:52:32 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_clientStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка входного аргумента для методов Insert, Update и Delete
|
|
|
|
|
private void CheckModel(ClientBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//так как при удалении параметром withParams передаём false
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие имени
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет имени клиента", nameof(model.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие фамилии
|
|
|
|
|
if (string.IsNullOrEmpty(model.Surname))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет фамилии клиента", nameof(model.Surname));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие отчества
|
|
|
|
|
if (string.IsNullOrEmpty(model.Patronymic))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет отчества клиента", nameof(model.Patronymic));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие телефонного номера
|
2023-04-13 01:35:46 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Telephone))
|
2023-04-12 16:52:32 +04:00
|
|
|
|
{
|
2023-04-13 01:35:46 +04:00
|
|
|
|
throw new ArgumentNullException("Нет телефонного номера клиента", nameof(model.Telephone));
|
2023-04-12 16:52:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие почты
|
|
|
|
|
if (string.IsNullOrEmpty(model.Email))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет электронной почты клиента", nameof(model.Email));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Client. Name:{Name}. Surname:{Surname}. Patronymic:{Patronymic}. " +
|
|
|
|
|
"TelephoneNumber:{TelephoneNumber}. Email:{Email}. Id:{Id}",
|
2023-04-13 01:35:46 +04:00
|
|
|
|
model.Name, model.Surname, model.Patronymic, model.Telephone, model.Email, model.Id);
|
2023-04-12 16:52:32 +04:00
|
|
|
|
|
|
|
|
|
//проверка на наличие такой же почты в списке
|
|
|
|
|
var element = _clientStorage.GetElement(new ClientSearchModel
|
|
|
|
|
{
|
|
|
|
|
Email = model.Email,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//если почта найдена и его Id не совпадает с Id объекта, переданного на вход
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Клиент с такой почтой уже есть");
|
|
|
|
|
}
|
2023-03-29 23:33:45 +04:00
|
|
|
|
}
|
2023-04-26 11:31:32 +04:00
|
|
|
|
}
|
2023-03-29 23:33:45 +04:00
|
|
|
|
}
|