2025-02-21 00:57:12 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
2025-02-17 22:42:11 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2025-02-21 00:57:12 +04:00
|
|
|
|
using System.Text.Json;
|
2025-02-17 22:42:11 +04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using TheButcherShopContracts.BusinessLogicContracts;
|
|
|
|
|
using TheButcherShopContracts.DataModels;
|
2025-02-21 00:57:12 +04:00
|
|
|
|
using TheButcherShopContracts.Exceptions;
|
|
|
|
|
using TheButcherShopContracts.Extensions;
|
|
|
|
|
using TheButcherShopContracts.StoragesContracts;
|
2025-02-17 22:42:11 +04:00
|
|
|
|
|
|
|
|
|
namespace ButcherShopBusinessLogic.Implementations;
|
|
|
|
|
|
2025-02-21 00:57:12 +04:00
|
|
|
|
internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
2025-02-17 22:42:11 +04:00
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
|
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
|
|
|
|
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate);
|
|
|
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
|
|
|
{
|
|
|
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
|
|
|
}
|
|
|
|
|
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
public List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
|
2025-02-17 22:42:11 +04:00
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("GetAllSales params: {workerId}, {fromDate}, {toDate}", workerId, fromDate, toDate);
|
|
|
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
|
|
|
{
|
|
|
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
|
|
|
}
|
|
|
|
|
if (workerId.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(workerId));
|
|
|
|
|
}
|
|
|
|
|
if (!workerId.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("The value in the field workerId is not a unique identifier.");
|
|
|
|
|
}
|
|
|
|
|
return _saleStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException();
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
public List<SaleDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate)
|
2025-02-17 22:42:11 +04:00
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("GetAllSales params: {productId}, {fromDate}, {toDate}", productId, fromDate, toDate);
|
|
|
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
|
|
|
{
|
|
|
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
|
|
|
}
|
|
|
|
|
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 _saleStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException();
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public SaleDataModel GetSaleByData(string data)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
|
|
|
if (data.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(data));
|
|
|
|
|
}
|
|
|
|
|
if (!data.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
|
|
|
|
return _saleStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public void InsertSale(SaleDataModel saleDataModel)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(saleDataModel);
|
|
|
|
|
saleDataModel.Validate();
|
|
|
|
|
_saleStorageContract.AddElement(saleDataModel);
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CancelSale(string id)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("Cancel by id: {id}", id);
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
|
}
|
|
|
|
|
if (!id.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
|
|
|
|
_saleStorageContract.DelElement(id);
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
|
|
|
|
}
|