87 lines
3.1 KiB
C#
Raw Normal View History

2025-02-23 23:23:11 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2025-02-27 00:09:42 +04:00
using System.Text.Json;
2025-02-23 23:23:11 +04:00
using System.Threading.Tasks;
2025-02-24 17:25:59 +04:00
using Microsoft.Extensions.Logging;
2025-02-23 23:23:11 +04:00
using PuferFishContracts.BusinessLogicsContracts;
using PuferFishContracts.DataModels;
using PuferFishContracts.Enums;
2025-02-27 00:09:42 +04:00
using PuferFishContracts.Exceptions;
using PuferFishContracts.Extensions;
2025-02-23 23:23:11 +04:00
using PuferFishContracts.StoragesContracts;
namespace PuferFishBusinessLogic.Implementations;
internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract
{
private readonly ILogger _logger = logger;
2025-02-27 00:09:42 +04:00
private readonly IProductStorageContract _productStorageContract = productStorageContract;
2025-02-27 16:30:42 +04:00
2025-02-23 23:23:11 +04:00
public List<ProductDataModel> GetAllProducts(bool onlyActive)
{
2025-02-27 00:09:42 +04:00
_logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive);
return _productStorageContract.GetList(onlyActive) ?? throw new NullListException();
2025-02-23 23:23:11 +04:00
}
public List<ProductHistoryDataModel> GetProductHistoryByProduct(string
productId)
{
2025-02-27 00:09:42 +04:00
_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();
2025-02-23 23:23:11 +04:00
}
public ProductDataModel GetProductByData(string data)
{
2025-02-27 00:09:42 +04:00
_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);
2025-02-23 23:23:11 +04:00
}
public void InsertProduct(ProductDataModel productDataModel)
{
2025-02-27 00:09:42 +04:00
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
_productStorageContract.AddElement(productDataModel);
2025-02-23 23:23:11 +04:00
}
public void UpdateProduct(ProductDataModel productDataModel)
{
2025-02-27 00:09:42 +04:00
_logger.LogInformation("Update data: {json}",
JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
_productStorageContract.UpdElement(productDataModel);
2025-02-23 23:23:11 +04:00
}
public void DeleteProduct(string id)
{
2025-02-27 00:09:42 +04:00
_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);
2025-02-23 23:23:11 +04:00
}
2025-02-27 16:30:42 +04:00
}