forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
79 lines
3.0 KiB
C#
79 lines
3.0 KiB
C#
using MagicCarpetContracts.BusinessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Enums;
|
|
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.Threading.Tasks;
|
|
|
|
namespace MagicCarpetBusinessLogic.Implementations;
|
|
|
|
public class SuppliesBusinessLogicContract(ISuppliesStorageContract suppliesStorageContract, ILogger logger) : ISuppliesBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly ISuppliesStorageContract _supplyStorageContract = suppliesStorageContract;
|
|
public List<SuppliesDataModel> GetAllSuppliesByPeriod(DateTime fromDate, DateTime toDate)
|
|
{
|
|
_logger.LogInformation("GetAllSupplies params: {fromDate}, {toDate}", fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
return _supplyStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
|
}
|
|
|
|
public List<SuppliesDataModel> GetAllSuppliesByCocktailByPeriod(string tourId, DateTime fromDate, DateTime toDate)
|
|
{
|
|
_logger.LogInformation("GetAllSupplies params: {cocktailId}, {fromDate}, {toDate}", tourId, fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
if (tourId.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(tourId));
|
|
}
|
|
if (!tourId.IsGuid())
|
|
{
|
|
throw new ValidationException("The value in the field tourId is not a unique identifier.");
|
|
}
|
|
return _supplyStorageContract.GetList(fromDate, toDate, tourId: tourId) ?? throw new NullListException();
|
|
}
|
|
|
|
public SuppliesDataModel GetSupplyByData(string data)
|
|
{
|
|
_logger.LogInformation("Get supply by data: {data}", data);
|
|
if (data.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(data));
|
|
}
|
|
if (!data.IsGuid())
|
|
{
|
|
throw new ElementNotFoundException(data);
|
|
}
|
|
return _supplyStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
|
|
public void InsertSupply(SuppliesDataModel supplyDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(supplyDataModel));
|
|
ArgumentNullException.ThrowIfNull(supplyDataModel);
|
|
supplyDataModel.Validate();
|
|
_supplyStorageContract.AddElement(supplyDataModel);
|
|
}
|
|
|
|
public void UpdateSupply(SuppliesDataModel supplyDataModel)
|
|
{
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(supplyDataModel));
|
|
ArgumentNullException.ThrowIfNull(supplyDataModel);
|
|
supplyDataModel.Validate();
|
|
_supplyStorageContract.UpdElement(supplyDataModel);
|
|
}
|
|
}
|