2025-02-18 01:17:27 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NorthBridgeContract.BusinessLogicsContracts;
|
|
|
|
|
using NorthBridgeContract.DataModels;
|
|
|
|
|
using NorthBridgeContract.Enums;
|
|
|
|
|
using NorthBridgeContract.Exceptions;
|
2025-02-18 02:34:31 +04:00
|
|
|
|
using NorthBridgeContract.Extensions;
|
2025-02-18 01:17:27 +04:00
|
|
|
|
using NorthBridgeContract.StoragesContracts;
|
2025-02-18 02:34:31 +04:00
|
|
|
|
using System.Text.Json;
|
2025-02-18 01:17:27 +04:00
|
|
|
|
|
|
|
|
|
namespace NorthBridgeBusinessLogic.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)
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("GetAllComponents, onlyActive: {onlyActive}", onlyActive);
|
|
|
|
|
return _componentStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 02:34:31 +04:00
|
|
|
|
public ComponentDataModel GetComponentById(string id)
|
2025-02-18 01:17:27 +04:00
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("GetComponentById: {id}", id);
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
|
}
|
|
|
|
|
if (!id.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
|
|
|
|
return _componentStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id);
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 02:34:31 +04:00
|
|
|
|
public ComponentDataModel GetComponentByName(string name)
|
2025-02-18 01:17:27 +04:00
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("GetComponentByName: {name}", name);
|
|
|
|
|
if (name.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(name));
|
|
|
|
|
}
|
|
|
|
|
return _componentStorageContract.GetElementByName(name) ?? throw new ElementNotFoundException(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentDataModel GetComponentByData(string data)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("GetComponentByData: {data}", data);
|
|
|
|
|
if (data.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(data));
|
|
|
|
|
}
|
|
|
|
|
if (data.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
return GetComponentById(data);
|
|
|
|
|
}
|
|
|
|
|
return GetComponentByName(data);
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InsertComponent(ComponentDataModel componentDataModel)
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("InsertComponent: {json}", JsonSerializer.Serialize(componentDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(componentDataModel);
|
2025-02-18 01:17:27 +04:00
|
|
|
|
componentDataModel.Validate();
|
|
|
|
|
_componentStorageContract.AddElement(componentDataModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateComponent(ComponentDataModel componentDataModel)
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("UpdateComponent: {json}", JsonSerializer.Serialize(componentDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(componentDataModel);
|
|
|
|
|
componentDataModel.Validate();
|
|
|
|
|
|
2025-02-18 01:17:27 +04:00
|
|
|
|
var existingComponent = _componentStorageContract.GetElementById(componentDataModel.Id);
|
|
|
|
|
if (existingComponent == null)
|
2025-02-18 02:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ElementNotFoundException(componentDataModel.Id);
|
|
|
|
|
}
|
2025-02-18 01:17:27 +04:00
|
|
|
|
|
|
|
|
|
if (existingComponent.Price != componentDataModel.Price)
|
|
|
|
|
{
|
|
|
|
|
var historyEntry = new ComponentHistoryDataModel(existingComponent.Id, existingComponent.Price);
|
|
|
|
|
historyEntry.Validate();
|
|
|
|
|
_componentStorageContract.AddComponentHistory(historyEntry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_componentStorageContract.UpdElement(componentDataModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteComponent(string id)
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("DeleteComponent by id: {id}", id);
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
|
}
|
|
|
|
|
if (!id.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
2025-02-18 01:17:27 +04:00
|
|
|
|
_componentStorageContract.DelElement(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RestoreComponent(string id)
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("RestoreComponent by id: {id}", id);
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
|
}
|
|
|
|
|
if (!id.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
2025-02-18 01:17:27 +04:00
|
|
|
|
_componentStorageContract.ResElement(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ComponentHistoryDataModel> GetComponentHistory(string componentId)
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("GetComponentHistory for componentId: {componentId}", componentId);
|
|
|
|
|
if (componentId.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(componentId));
|
|
|
|
|
}
|
|
|
|
|
if (!componentId.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("ComponentId is not a unique identifier");
|
|
|
|
|
}
|
|
|
|
|
return _componentStorageContract.GetComponentHistory(componentId) ?? [];
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 02:34:31 +04:00
|
|
|
|
|
|
|
|
|
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|