251 lines
10 KiB
C#
251 lines
10 KiB
C#
using Microsoft.Extensions.Localization;
|
|
using SmallSoftwareContracts.AdapterContracts;
|
|
using SmallSoftwareContracts.AdapterContracts.OperationResponses;
|
|
using SmallSoftwareContracts.BindingModels;
|
|
using SmallSoftwareContracts.BusinessLogicsContracts;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.Mapper;
|
|
using SmallSoftwareContracts.Resources;
|
|
using SmallSoftwareContracts.ViewModels;
|
|
|
|
namespace SmallSoftwareWebApi.Adapters;
|
|
|
|
internal class SoftwareAdapter(ISoftwareBusinessLogicContract softwareBusinessLogicContract, ILogger<SoftwareAdapter> logger, IStringLocalizer<Messages> localizer) : ISoftwareAdapter
|
|
{
|
|
private readonly ISoftwareBusinessLogicContract _softwareBusinessLogicContract = softwareBusinessLogicContract;
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
|
|
public SoftwareOperationResponse GetList(bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return SoftwareOperationResponse.OK([.. _softwareBusinessLogicContract.GetAllSoftwares(!includeDeleted).Select(x => CustomMapper.MapObject<SoftwareViewModel>(x))]);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SoftwareOperationResponse GetManufacturerList(string id, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return SoftwareOperationResponse.OK([.. _softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(id, !includeDeleted).Select(x => CustomMapper.MapObject<SoftwareViewModel>(x))]);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SoftwareOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SoftwareOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SoftwareOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SoftwareOperationResponse GetHistory(string id)
|
|
{
|
|
try
|
|
{
|
|
return SoftwareOperationResponse.OK([.. _softwareBusinessLogicContract.GetSoftwareHistoryBySoftware(id).Select(x => CustomMapper.MapObject<SoftwareHistoryViewModel>(x))]);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SoftwareOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SoftwareOperationResponse GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return SoftwareOperationResponse.OK(CustomMapper.MapObject<SoftwareViewModel>(_softwareBusinessLogicContract.GetSoftwareByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SoftwareOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SoftwareOperationResponse.NotFound(string.Format(_localizer["AdapterMessageElementNotFoundException"], data));
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], data));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SoftwareOperationResponse RegisterSoftware(SoftwareBindingModel softwareModel)
|
|
{
|
|
try
|
|
{
|
|
_softwareBusinessLogicContract.InsertSoftware(CustomMapper.MapObject<SoftwareDataModel>(softwareModel));
|
|
return SoftwareOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SoftwareOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return SoftwareOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SoftwareOperationResponse ChangeSoftwareInfo(SoftwareBindingModel softwareModel)
|
|
{
|
|
try
|
|
{
|
|
_softwareBusinessLogicContract.UpdateSoftware(CustomMapper.MapObject<SoftwareDataModel>(softwareModel));
|
|
return SoftwareOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SoftwareOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], softwareModel));
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return SoftwareOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], softwareModel));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SoftwareOperationResponse RemoveSoftware(string id)
|
|
{
|
|
try
|
|
{
|
|
_softwareBusinessLogicContract.DeleteSoftware(id);
|
|
return SoftwareOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SoftwareOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], id));
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|