Files
PIBD24_BoikoM.S._Candyhouse/CandyHouseSolution/CandyHouseBusinessLogic/Implementations/StorageBusinessLogicContract.cs
2025-04-15 02:40:51 +04:00

75 lines
2.5 KiB
C#

using CandyHouseContracts.BusinessLogicContracts;
using CandyHouseContracts.DataModels;
using CandyHouseContracts.Enums;
using CandyHouseContracts.Exceptions;
using CandyHouseContracts.Extensions;
using CandyHouseContracts.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 CandyHouseBusinessLogic.Implementations;
public class StorageBusinessLogicContract(IStorageStorageContract storageStorageContract, ILogger logger) : IStorageBusinessLogicContract
{
private readonly IStorageStorageContract _storageStorageContract = storageStorageContract;
private readonly ILogger _logger = logger;
public List<StorageDataModel> GetAllComponents()
{
_logger.LogInformation("GetAllComponents");
return _storageStorageContract.GetList() ?? throw new NullListException();
return [];
}
public StorageDataModel GetComponentByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ElementNotFoundException(data);
}
return _storageStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return new("", ProductType.None, 0, []);
}
public void InsertComponent(StorageDataModel storageDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(storageDataModel));
ArgumentNullException.ThrowIfNull(storageDataModel);
storageDataModel.Validate();
_storageStorageContract.AddElement(storageDataModel);
}
public void UpdateComponent(StorageDataModel storageDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(storageDataModel));
ArgumentNullException.ThrowIfNull(storageDataModel);
storageDataModel.Validate();
_storageStorageContract.UpdElement(storageDataModel);
}
public void DeleteComponent(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");
}
_storageStorageContract.DelElement(id);
}
}