2025-02-18 01:17:27 +04:00
|
|
|
|
using NorthBridgeContract.BusinessLogicsContracts;
|
|
|
|
|
using NorthBridgeContract.DataModels;
|
|
|
|
|
using NorthBridgeContract.Exceptions;
|
|
|
|
|
using NorthBridgeContract.Extensions;
|
|
|
|
|
using NorthBridgeContract.StoragesContracts;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace NorthBridgeBusinessLogic.Implementations;
|
|
|
|
|
|
|
|
|
|
internal class ManufacturerBusinessLogicContract(IManufacturerStorageContract manufacturerStorageContract, ILogger logger) : IManufacturerBusinessLogicContract
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
|
private readonly IManufacturerStorageContract _manufacturerStorageContract = manufacturerStorageContract;
|
2025-02-18 01:17:27 +04:00
|
|
|
|
|
|
|
|
|
public List<ManufacturerDataModel> GetAllManufacturers()
|
2025-02-18 02:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("GetAllManufacturers");
|
|
|
|
|
return _manufacturerStorageContract.GetList() ?? throw new NullListException();
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ManufacturerDataModel GetManufacturerByData(string data)
|
|
|
|
|
{
|
2025-02-18 02:34:31 +04:00
|
|
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
|
|
|
if (data.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(data));
|
|
|
|
|
}
|
|
|
|
|
if (data.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
return _manufacturerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
|
|
|
}
|
|
|
|
|
return _manufacturerStorageContract.GetElementByName(data) ?? _manufacturerStorageContract.GetElementByOldName(data) ??
|
|
|
|
|
throw new ElementNotFoundException(data);
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InsertManufacturer(ManufacturerDataModel manufacturerDataModel)
|
2025-02-18 02:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(manufacturerDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(manufacturerDataModel);
|
|
|
|
|
manufacturerDataModel.Validate();
|
|
|
|
|
_manufacturerStorageContract.AddElement(manufacturerDataModel);
|
|
|
|
|
}
|
2025-02-18 01:17:27 +04:00
|
|
|
|
|
2025-02-18 02:34:31 +04:00
|
|
|
|
public void UpdateManufacturer(ManufacturerDataModel manufacturerDataModel)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(manufacturerDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(manufacturerDataModel);
|
|
|
|
|
manufacturerDataModel.Validate();
|
|
|
|
|
_manufacturerStorageContract.UpdElement(manufacturerDataModel);
|
|
|
|
|
}
|
2025-02-18 01:17:27 +04:00
|
|
|
|
|
2025-02-18 02:34:31 +04:00
|
|
|
|
public void DeleteManufacturer(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");
|
|
|
|
|
}
|
|
|
|
|
_manufacturerStorageContract.DelElement(id);
|
|
|
|
|
}
|
2025-02-18 01:17:27 +04:00
|
|
|
|
}
|