Files
PIBD-23_Coursach_YouAreProg…/YouAreProgrammerShop/YAPBusinessLogic/Implementations/ComponentBusinessLogicContract.cs

72 lines
2.5 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 YAPContracts.BusinessLogicContracts;
using YAPContracts.DataModels;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.StorageContracts;
namespace YAPBusinessLogic.Implementations;
internal class ComponentBusinessLogicContract(IComponentStorageContract componentStorageContract, ILogger logger) : IComponentBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IComponentStorageContract _componentStorageContract = componentStorageContract;
public List<ComponentDataModel> GetAllComponents(bool onlyActual)
{
_logger.LogInformation("GetAllComponents params: {onlyActual}", onlyActual);
return _componentStorageContract.GetList(onlyActual) ?? throw new NullListException();
}
public ComponentDataModel GetComponentByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _componentStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _componentStorageContract.GetElementByName(data) ??
throw new ElementNotFoundException(data);
}
public void InsertComponent(ComponentDataModel componentDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate();
_componentStorageContract.AddElement(componentDataModel);
}
public void UpdateComponent(ComponentDataModel componentDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate();
_componentStorageContract.UpdElement(componentDataModel);
}
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");
}
_componentStorageContract.DelElement(id);
}
}