Files
PIbd-23_Rachek.A.S_ThreeFat…/ThreeFatMenProject/ThreeFatMenBusinessLogic/Implementations/LunchBusinessLogicContract.cs
2025-04-16 00:15:22 +04:00

86 lines
3.0 KiB
C#

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using ThreeFatMenContracts.BusinessLogicsContracts;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenBusinessLogic.Implementations;
internal class LunchBusinessLogicContract(ILunchStorageContract lunchStorageContract, ILogger logger) : ILunchBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ILunchStorageContract _lunchStorageContract = lunchStorageContract;
public List<LunchDataModel> GetAllLunches(bool onlyActive)
{
_logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive);
return _lunchStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<LunchHistoryDataModel> GetLunchHistoryByLunch(string lunchId)
{
_logger.LogInformation("GetProductHistoryByProduct for {productId}", lunchId);
if (lunchId.IsEmpty())
{
throw new ArgumentNullException(nameof(lunchId));
}
if (!lunchId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
}
return _lunchStorageContract.GetHistoryByLunchId(lunchId) ??
throw new NullListException();
}
public LunchDataModel GetLunchByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _lunchStorageContract.GetElementById(data) ?? throw
new ElementNotFoundException(data);
}
return _lunchStorageContract.GetElementByName(data) ?? throw new
ElementNotFoundException(data);
}
public void InsertLunch(LunchDataModel lunchDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(lunchDataModel));
ArgumentNullException.ThrowIfNull(lunchDataModel);
lunchDataModel.Validate();
_lunchStorageContract.AddElement(lunchDataModel);
}
public void UpdateLunch(LunchDataModel lunchDataModel)
{
_logger.LogInformation("Update data: {json}",
JsonSerializer.Serialize(lunchDataModel));
ArgumentNullException.ThrowIfNull(lunchDataModel);
lunchDataModel.Validate();
_lunchStorageContract.UpdElement(lunchDataModel);
}
public void DeleteLunch(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");
}
_lunchStorageContract.DelElement(id);
}
}