71 lines
2.5 KiB
C#
71 lines
2.5 KiB
C#
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
using CandyHouseContracts.BuisnessLogicContracts;
|
|
using CandyHouseContracts.DataModels;
|
|
using CandyHouseContracts.Exceptions;
|
|
using CandyHouseContracts.Extensions;
|
|
using CandyHouseContracts.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CandyHouseBusinessLogic.Implementations;
|
|
|
|
public class ClientBusinessLogicContract(IClientStorageContract clientStorageContract, ILogger logger) : IClientBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly IClientStorageContract _clientStorageContract = clientStorageContract;
|
|
|
|
public List<ClientDataModel> GetAllClients()
|
|
{
|
|
_logger.LogInformation("GetAllClients");
|
|
return _clientStorageContract.GetList() ?? throw new NullListException();
|
|
}
|
|
|
|
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);
|
|
}
|
|
if (Regex.IsMatch(data, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
|
|
{
|
|
return _clientStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
return _clientStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
|
|
public void InsertClient(ClientDataModel clientDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(clientDataModel));
|
|
ArgumentNullException.ThrowIfNull(clientDataModel);
|
|
clientDataModel.Validate();
|
|
_clientStorageContract.AddElement(clientDataModel);
|
|
}
|
|
|
|
public void UpdateClient(ClientDataModel clientDataModel)
|
|
{
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(clientDataModel));
|
|
ArgumentNullException.ThrowIfNull(clientDataModel);
|
|
clientDataModel.Validate();
|
|
_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("Id is not a unique identifier");
|
|
}
|
|
_clientStorageContract.DelElement(id);
|
|
}
|
|
}
|