67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Text.Json;
|
|
using TheCyclopsContracts.BusinessLogicContracts;
|
|
using TheCyclopsContracts.DataModels;
|
|
using TheCyclopsContracts.Enums;
|
|
using TheCyclopsContracts.Exceptions;
|
|
using TheCyclopsContracts.Extensions;
|
|
using TheCyclopsContracts.StoragesContracts;
|
|
|
|
namespace TheCyclopsBusinessLogic.Implementations;
|
|
|
|
internal class ComponentBusinessLogicContract(IComponentStorageContract componentStorageContract, ILogger logger) : IComponentBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly IComponentStorageContract _componentStorageContract = componentStorageContract;
|
|
|
|
public List<ComponentDataModel> GetAllComponents(bool onlyActive)
|
|
{
|
|
_logger.LogInformation("GetAllComponents params: {onlyActive}", onlyActive);
|
|
return _componentStorageContract.GetList(onlyActive) ?? 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);
|
|
}
|
|
}
|