120 lines
4.6 KiB
C#

using Microsoft.Extensions.Logging;
using PapaCarloContracts.BusinessLogicContracts;
using PapaCarloContracts.DataModels;
using PapaCarloContracts.Exceptions;
using PapaCarloContracts.Extensions;
using PapaCarloContracts.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace PapaCarloBusinessLogic.Implementations;
internal class CuttingBusinessLogicContract(ICuttingStorageContract cuttingStorageContract, ILogger logger) : ICuttingBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ICuttingStorageContract _cuttingStorageContract = cuttingStorageContract;
public List<CuttingDataModel> GetAllCuttingsByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllCuttings params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _cuttingStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public List<CuttingDataModel> GetAllCuttingsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllCuttings 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 _cuttingStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException();
}
public List<CuttingDataModel> GetAllCuttingsByBuyerByPeriod(string blankId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllCuttings params: {blankId}, {fromDate}, {toDate}", blankId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (blankId.IsEmpty())
{
throw new ArgumentNullException(nameof(blankId));
}
if (!blankId.IsGuid())
{
throw new ValidationException("The value in the field blankId is not a unique identifier.");
}
return _cuttingStorageContract.GetList(fromDate, toDate, blankId: blankId) ?? throw new NullListException();
}
public List<CuttingDataModel> GetAllCuttingsByProductByPeriod(string productId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllCuttings 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 _cuttingStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException();
}
public CuttingDataModel GetCuttingByData(string data)
{
_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 _cuttingStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
public void InsertCutting(CuttingDataModel cuttingDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(cuttingDataModel));
ArgumentNullException.ThrowIfNull(cuttingDataModel);
cuttingDataModel.Validate();
_cuttingStorageContract.AddElement(cuttingDataModel);
}
public void CancelCutting(string id)
{
_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");
}
_cuttingStorageContract.DelElement(id);
}
}