74 lines
2.8 KiB
C#
74 lines
2.8 KiB
C#
using SquirrelContract.BusinessLogicContracts;
|
|
using SquirrelContract.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
using SquirrelContract.Exceptions;
|
|
using SquirrelContract.Extensions;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
using SquirrelContract.DataModels;
|
|
using Microsoft.Extensions.Localization;
|
|
using SquirrelContract.Resources;
|
|
|
|
namespace SquirrelBusinessLogic.Implementations;
|
|
|
|
internal class ClientBusinessLogicContract(IClientStorageContract clientStorageContract, IStringLocalizer<Messages> localizer, ILogger logger) : IClientBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly IClientStorageContract _clientStorageContract = clientStorageContract;
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
|
|
public List<ClientDataModel> GetAllClients()
|
|
{
|
|
_logger.LogInformation("GetAllClients");
|
|
return _clientStorageContract.GetList();
|
|
}
|
|
|
|
public ClientDataModel GetClientByData(string data)
|
|
{
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
if (data.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(data));
|
|
}
|
|
if (data.IsGuid())
|
|
{
|
|
return _clientStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data, _localizer);
|
|
}
|
|
if (Regex.IsMatch(data, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
|
|
{
|
|
return _clientStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data, _localizer);
|
|
}
|
|
return _clientStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data, _localizer);
|
|
}
|
|
|
|
public void InsertClient(ClientDataModel clientDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(clientDataModel));
|
|
ArgumentNullException.ThrowIfNull(clientDataModel);
|
|
clientDataModel.Validate(_localizer);
|
|
_clientStorageContract.AddElement(clientDataModel);
|
|
}
|
|
|
|
public void UpdateClient(ClientDataModel clientDataModel)
|
|
{
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(clientDataModel));
|
|
ArgumentNullException.ThrowIfNull(clientDataModel);
|
|
clientDataModel.Validate(_localizer);
|
|
_clientStorageContract.UpdElement(clientDataModel);
|
|
}
|
|
|
|
public void DeleteClient(string id)
|
|
{
|
|
_logger.LogInformation("Delete by id: {id}", id);
|
|
if (id.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(id));
|
|
}
|
|
if (!id.IsGuid())
|
|
{
|
|
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
|
|
}
|
|
_clientStorageContract.DelElement(id);
|
|
}
|
|
}
|