69 lines
2.8 KiB
C#
69 lines
2.8 KiB
C#
using Microsoft.Extensions.Localization;
|
|
using Microsoft.Extensions.Logging;
|
|
using CandyHouseContracts.BusinessLogicsContracts;
|
|
using CandyHouseContracts.DataModels;
|
|
using CandyHouseContracts.Exceptions;
|
|
using CandyHouseContracts.Extensions;
|
|
using CandyHouseContracts.Resources;
|
|
using CandyHouseContracts.StoragesContracts;
|
|
using System.Text.Json;
|
|
|
|
namespace CandyHouseBusinessLogic.Implementations;
|
|
|
|
internal class ManufacturerBusinessLogicContract(IManufacturerStorageContract manufacturerStorageContract, IStringLocalizer<Messages> localizer, ILogger logger) : IManufacturerBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly IManufacturerStorageContract _manufacturerStorageContract = manufacturerStorageContract;
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
|
|
public List<ManufacturerDataModel> GetAllManufacturers()
|
|
{
|
|
_logger.LogInformation("GetAllManufacturers");
|
|
return _manufacturerStorageContract.GetList();
|
|
}
|
|
|
|
public ManufacturerDataModel GetManufacturerByData(string data)
|
|
{
|
|
_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, _localizer);
|
|
}
|
|
return _manufacturerStorageContract.GetElementByName(data) ?? _manufacturerStorageContract.GetElementByOldName(data) ??
|
|
throw new ElementNotFoundException(data, _localizer);
|
|
}
|
|
|
|
public void InsertManufacturer(ManufacturerDataModel manufacturerDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(manufacturerDataModel));
|
|
ArgumentNullException.ThrowIfNull(manufacturerDataModel);
|
|
manufacturerDataModel.Validate(_localizer);
|
|
_manufacturerStorageContract.AddElement(manufacturerDataModel);
|
|
}
|
|
|
|
public void UpdateManufacturer(ManufacturerDataModel manufacturerDataModel)
|
|
{
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(manufacturerDataModel));
|
|
ArgumentNullException.ThrowIfNull(manufacturerDataModel);
|
|
manufacturerDataModel.Validate(_localizer);
|
|
_manufacturerStorageContract.UpdElement(manufacturerDataModel);
|
|
}
|
|
|
|
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(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
|
|
}
|
|
_manufacturerStorageContract.DelElement(id);
|
|
}
|
|
} |