using MagicCarpetContracts.BuisnessLogicContracts; using MagicCarpetContracts.DataModels; using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Extensions; using MagicCarpetContracts.StoragesContracts; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace MagicCarpetBusinessLogic.Implementations; internal class ClientBusinessLogicContract(IClientStorageContract clientStorageContract, ILogger logger) : IClientBusinessLogicContract { private readonly ILogger _logger = logger; private readonly IClientStorageContract _clientStorageContract = clientStorageContract; public List 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); } }