PIbd-23_Elatomtsev_L.K._Cou.../Bank/BankBusinessLogic/BusinessLogics/ClientLogic.cs

166 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BankBusinessLogic.BusinessLogics
{
public class ClientLogic : IClientLogic
{
private readonly ILogger _logger;
private readonly IClientStorage _clientStorage;
public ClientLogic(ILogger<ClientLogic> logger,
IClientStorage clientStorage)
{
_logger = logger;
_clientStorage = clientStorage;
}
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. Snils: {Snils}", model.Snils);
if (_clientStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
_logger.LogInformation(
"ReadList. ClientFullname: {ClientSurname} {ClientName} " +
"{ClientPatronomic}. Snils: {Snils}", model?.ClientSurname,
model?.ClientName, model?.ClientPatronymic, model?.Snils);
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. ClientFullname: {ClientSurname} {ClientName} " +
"{ClientPatronomic}. Snils: {Snils}", model.ClientSurname,
model.ClientName, model.ClientPatronymic, model.Snils);
var element = _clientStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Snils: {Snils}",
element.Snils);
return element;
}
private void CheckModel(ClientBindingModel model,
bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Snils))
{
throw new ArgumentNullException(
"Отсутствует СНИЛС клиента",
nameof(model.Snils));
}
if (string.IsNullOrEmpty(model.ClientSurname))
{
throw new ArgumentNullException(
"Отсутствует фамилия клиента",
nameof(model.ClientSurname));
}
if (string.IsNullOrEmpty(model.ClientName))
{
throw new ArgumentNullException(
"Отсутствует имя клиента",
nameof(model.ClientName));
}
if (string.IsNullOrEmpty(model.ClientPatronymic))
{
throw new ArgumentNullException(
"Отсутствует отчество клиента",
nameof(model.ClientPatronymic));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException(
"Отсутствует номер телефона клиента",
nameof(model.Phone));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException(
"Отсутствует пароль клиента",
nameof(model.Password));
}
if (model.WorkerId <= 0)
{
throw new ArgumentNullException(
"Идентификатор работника должен быть больше 0",
nameof(model.WorkerId));
}
_logger.LogInformation("Client. Snils: {Snils}. ClientFullname: " +
"{ClientSurname} {ClientName} {ClientPatronymic}. Phone: " +
"{Phone}. Email: {Email}. Password: {Password}.",
model.Snils, model.ClientSurname, model.ClientName,
model.ClientPatronymic, model.Phone, model.Email,
model.Password);
var elementByEmail = _clientStorage.GetElement(
new ClientSearchModel
{
Email = model.Email,
});
var elementByPhone = _clientStorage.GetElement(
new ClientSearchModel
{
Phone = model.Phone,
});
if ((elementByEmail != null && elementByEmail.Snils != model.Snils)
|| (elementByPhone != null && elementByPhone.Snils
!= model.Snils))
{
throw new InvalidOperationException("Клиент с такой почтой " +
"или номером телефона уже существует");
}
}
}
}