267 lines
10 KiB
C#
267 lines
10 KiB
C#
using AutoMapper;
|
|
using SmallSoftwareContracts.AdapterContracts;
|
|
using SmallSoftwareContracts.AdapterContracts.OperationResponses;
|
|
using SmallSoftwareContracts.BindingModels;
|
|
using SmallSoftwareContracts.BusinessLogicsContracts;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.ViewModels;
|
|
|
|
namespace SmallSoftwareWebApi.Adapters;
|
|
|
|
public class SoftwareAdapter : ISoftwareAdapter
|
|
{
|
|
private readonly ISoftwareBusinessLogicContract _softwareBusinessLogicContract;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
|
|
public SoftwareAdapter(ISoftwareBusinessLogicContract softwareBusinessLogicContract, ILogger<SoftwareAdapter> logger)
|
|
{
|
|
_softwareBusinessLogicContract = softwareBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<SoftwareBindingModel, SoftwareDataModel>();
|
|
cfg.CreateMap<SoftwareDataModel, SoftwareViewModel>();
|
|
cfg.CreateMap<SoftwareHistoryDataModel, SoftwareHistoryViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public SoftwareOperationResponse GetList(bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return SoftwareOperationResponse.OK([.. _softwareBusinessLogicContract.GetAllSoftwares(!includeDeleted).Select(x => _mapper.Map<SoftwareViewModel>(x))]);
|
|
}
|
|
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 GetManufacturerList(string id, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return SoftwareOperationResponse.OK([.. _softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(id, !includeDeleted).Select(x => _mapper.Map<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 => _mapper.Map<SoftwareHistoryViewModel>(x))]);
|
|
}
|
|
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 GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return SoftwareOperationResponse.OK(_mapper.Map<SoftwareViewModel>(_softwareBusinessLogicContract.GetSoftwareByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SoftwareOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SoftwareOperationResponse.NotFound($"Not found element by data {data}");
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SoftwareOperationResponse.BadRequest($"Element by data: {data} was deleted");
|
|
}
|
|
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 RegisterSoftware(SoftwareBindingModel softwareModel)
|
|
{
|
|
try
|
|
{
|
|
_softwareBusinessLogicContract.InsertSoftware(_mapper.Map<SoftwareDataModel>(softwareModel));
|
|
return SoftwareOperationResponse.NoContent();
|
|
}
|
|
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 (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return SoftwareOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SoftwareOperationResponse ChangeSoftwareInfo(SoftwareBindingModel softwareModel)
|
|
{
|
|
try
|
|
{
|
|
_softwareBusinessLogicContract.UpdateSoftware(_mapper.Map<SoftwareDataModel>(softwareModel));
|
|
return SoftwareOperationResponse.NoContent();
|
|
}
|
|
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 (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SoftwareOperationResponse.BadRequest($"Not found element by Id {softwareModel.Id}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return SoftwareOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SoftwareOperationResponse.BadRequest($"Element by id: {softwareModel.Id} was deleted");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.BadRequest($"Error while working with data storage: {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("Id is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SoftwareOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SoftwareOperationResponse.BadRequest($"Not found element by id: {id}");
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SoftwareOperationResponse.BadRequest($"Element by id: {id} was deleted");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SoftwareOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SoftwareOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|