forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
229 lines
8.9 KiB
C#
229 lines
8.9 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.Resources;
|
|
using MagicCarpetContracts.ViewModels;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
namespace MagicCarpetWebApi.Adapters;
|
|
|
|
internal class TourAdapter : ITourAdapter
|
|
{
|
|
private readonly ITourBusinessLogicContract _tourBusinessLogicContract;
|
|
|
|
private readonly IStringLocalizer<Messages> _localizer;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
|
|
public TourAdapter(ITourBusinessLogicContract tourBusinessLogicContract, IStringLocalizer<Messages> localizer, 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);
|
|
_localizer = localizer;
|
|
}
|
|
|
|
public TourOperationResponse GetList(bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return TourOperationResponse.OK([.. _tourBusinessLogicContract.GetAllTours().Select(_mapper.Map<TourViewModel>)]);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return TourOperationResponse.NotFound(string.Format(_localizer["AdapterMessageElementNotFoundException"], data));
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return TourOperationResponse.NotFound(string.Format(_localizer["AdapterMessageElementDeletedException"], data));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return TourOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return TourOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return TourOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return TourOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], tourModel.Id));
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return TourOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return TourOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], tourModel.Id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return TourOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return TourOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], id));
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return TourOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return TourOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return TourOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|