using Microsoft.Extensions.Logging; using Squirrel.BusinessLogicsContracts; using Squirrel.DataModels; using Squirrel.Enums; using Squirrel.Exceptions; using Squirrel.Extensions; using Squirrel.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace SquirelBusinessLogic.Implementations; internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract { private readonly ILogger _logger = logger; private readonly IProductStorageContract _productStorageContract = productStorageContract; public List GetAllProducts(bool onlyActive) { _logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive); return _productStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetProductHistoryByProduct(string productId) { _logger.LogInformation("GetProductHistoryByProduct for {productId}", productId); if (productId.IsEmpty()) { throw new ArgumentNullException(nameof(productId)); } if (!productId.IsGuid()) { throw new ValidationException("The value in the field productId is not a unique identifier."); } return _productStorageContract.GetHistoryByProductId(productId) ?? throw new NullListException(); } public ProductDataModel GetProductByData(string data) { _logger.LogInformation("Get element by data: {data}", data); if (data.IsEmpty()) { throw new ArgumentNullException(nameof(data)); } if (data.IsGuid()) { return _productStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); } return _productStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); } public void InsertProduct(ProductDataModel productDataModel) { _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(productDataModel)); ArgumentNullException.ThrowIfNull(productDataModel); productDataModel.Validate(); _productStorageContract.AddElement(productDataModel); } public void UpdateProduct(ProductDataModel productDataModel) { _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(productDataModel)); ArgumentNullException.ThrowIfNull(productDataModel); productDataModel.Validate(); _productStorageContract.UpdElement(productDataModel); } public void DeleteProduct(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"); } _productStorageContract.DelElement(id); } }