forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
234 lines
8.5 KiB
C#
234 lines
8.5 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.AdapterContracts;
|
|
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
|
using MagicCarpetContracts.BindingModels;
|
|
using MagicCarpetContracts.BuisnessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.ViewModels;
|
|
|
|
namespace MagicCarpetWebApi.Adapters;
|
|
|
|
public class TourAdapter : ITourAdapter
|
|
{
|
|
private readonly ITourBusinessLogicContract _tourBusinessLogicContract;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
|
|
public TourAdapter(ITourBusinessLogicContract tourBusinessLogicContract, ILogger<TourAdapter> logger)
|
|
{
|
|
_tourBusinessLogicContract = tourBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<TourBindingModel, TourDataModel>();
|
|
cfg.CreateMap<TourDataModel, TourViewModel>();
|
|
cfg.CreateMap<TourHistoryDataModel, TourHistoryViewModel>();
|
|
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public TourOperationResponse GetList(bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return TourOperationResponse.OK([.. _tourBusinessLogicContract.GetAllTours().Select(_mapper.Map<TourViewModel>)]);
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return TourOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return TourOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public TourOperationResponse GetHistory(string id)
|
|
{
|
|
try
|
|
{
|
|
return TourOperationResponse.OK([.. _tourBusinessLogicContract.GetTourHistoryByTour(id).Select(x => _mapper.Map<TourHistoryViewModel>(x))]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return TourOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return TourOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public TourOperationResponse GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return TourOperationResponse.OK(_mapper.Map<TourViewModel>(_tourBusinessLogicContract.GetTourByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return TourOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return TourOperationResponse.NotFound($"Not found element by data {data}");
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return TourOperationResponse.BadRequest($"Element by data: {data} was deleted");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return TourOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public TourOperationResponse RegisterTour(TourBindingModel tourModel)
|
|
{
|
|
try
|
|
{
|
|
_tourBusinessLogicContract.InsertTour(_mapper.Map<TourDataModel>(tourModel));
|
|
return TourOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return TourOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return TourOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return TourOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public TourOperationResponse ChangeTourInfo(TourBindingModel tourModel)
|
|
{
|
|
try
|
|
{
|
|
_tourBusinessLogicContract.UpdateTour(_mapper.Map<TourDataModel>(tourModel));
|
|
return TourOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return TourOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return TourOperationResponse.BadRequest($"Not found element by Id {tourModel.Id}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return TourOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return TourOperationResponse.BadRequest($"Element by id: {tourModel.Id} was deleted");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return TourOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public TourOperationResponse RemoveTour(string id)
|
|
{
|
|
try
|
|
{
|
|
_tourBusinessLogicContract.DeleteTour(id);
|
|
return TourOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return TourOperationResponse.BadRequest("Id is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return TourOperationResponse.BadRequest($"Not found element by id: {id}");
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return TourOperationResponse.BadRequest($"Element by id: {id} was deleted");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return TourOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|