Files
Pibd-21_Semin_D.A._SmallSof…/SmallSoftwareProject/SmallSoftwareBusinessLogic/Implementations/SoftwareBusinessLogicContract.cs

101 lines
3.7 KiB
C#

using Microsoft.Extensions.Logging;
using SmallSoftwareContracts.BusinessLogicsContracts;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Enums;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.Extensions;
using SmallSoftwareContracts.StoragesContracts;
using System.Text.Json;
namespace SmallSoftwareBusinessLogic.Implementations;
internal class SoftwareBusinessLogicContract(ISoftwareStorageContract
softwareStorageContract, ILogger logger) : ISoftwareBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ISoftwareStorageContract _softwareStorageContract =
softwareStorageContract;
public List<SoftwareDataModel> GetAllSoftwares(bool onlyActive)
{
_logger.LogInformation("GetAllSoftwares params: {onlyActive}", onlyActive);
return _softwareStorageContract.GetList(onlyActive) ?? throw new
NullListException();
}
public List<SoftwareDataModel> GetAllSoftwaresByManufacturer(string
manufacturerId, bool onlyActive = true)
{
if (manufacturerId.IsEmpty())
{
throw new ArgumentNullException(nameof(manufacturerId));
}
if (!manufacturerId.IsGuid())
{
throw new ValidationException("The value in the field manufacturerId is not a unique identifier.");
}
_logger.LogInformation("GetAllSoftwares params: {manufacturerId}, { onlyActive} ", manufacturerId, onlyActive);
return _softwareStorageContract.GetList(onlyActive, manufacturerId) ??
throw new NullListException();
}
public List<SoftwareHistoryDataModel> GetSoftwareHistoryBySoftware(string softwareId)
{
_logger.LogInformation("GetSoftwareHistoryBySoftware for {softwareId}", softwareId);
if (softwareId.IsEmpty())
{
throw new ArgumentNullException(nameof(softwareId));
}
if (!softwareId.IsGuid())
{
throw new ValidationException("The value in the field softwareId is not a unique identifier.");
}
return _softwareStorageContract.GetHistoryBySoftwareId(softwareId) ??
throw new NullListException();
}
public SoftwareDataModel GetSoftwareByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _softwareStorageContract.GetElementById(data) ?? throw
new ElementNotFoundException(data);
}
return _softwareStorageContract.GetElementByName(data) ?? throw new
ElementNotFoundException(data);
}
public void InsertSoftware(SoftwareDataModel softwareDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(softwareDataModel));
ArgumentNullException.ThrowIfNull(softwareDataModel);
softwareDataModel.Validate();
_softwareStorageContract.AddElement(softwareDataModel);
}
public void UpdateSoftware(SoftwareDataModel softwareDataModel)
{
_logger.LogInformation("Update data: {json}",
JsonSerializer.Serialize(softwareDataModel));
ArgumentNullException.ThrowIfNull(softwareDataModel);
softwareDataModel.Validate();
_softwareStorageContract.UpdElement(softwareDataModel);
}
public void DeleteSoftware(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");
}
_softwareStorageContract.DelElement(id);
}
}