Files
PIbd-22_Pyzhov_E.A_TheSquirrel/SquirrelContract/SquirrelWebApi/Adapters/CocktailAdapter.cs
2025-03-22 01:18:27 +04:00

234 lines
8.8 KiB
C#

using AutoMapper;
using SquirrelContract.AdapterContracts;
using SquirrelContract.AdapterContracts.OperationResponses;
using SquirrelContract.BindingModels;
using SquirrelContract.BusinessLogicContracts;
using SquirrelContract.DataModels;
using SquirrelContract.Exceptions;
using SquirrelContract.ViewModels;
namespace SquirrelWebApi.Adapters;
public class CocktailAdapter : ICocktailAdapter
{
private readonly ICocktailBusinessLogicContract _cocktailBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public CocktailAdapter(ICocktailBusinessLogicContract cocktailBusinessLogicContract, ILogger<CocktailAdapter> logger)
{
_cocktailBusinessLogicContract = cocktailBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<CocktailBindingModel, CocktailDataModel>();
cfg.CreateMap<CocktailDataModel, CocktailViewModel>();
cfg.CreateMap<CocktailHistoryDataModel, CocktailHistoryViewModel>();
});
_mapper = new Mapper(config);
}
public CocktailOperationResponse GetList(bool includeDeleted)
{
try
{
return CocktailOperationResponse.OK([.. _cocktailBusinessLogicContract.GetAllCocktails().Select(x => _mapper.Map<CocktailViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return CocktailOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return CocktailOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return CocktailOperationResponse.InternalServerError(ex.Message);
}
}
public CocktailOperationResponse GetHistory(string id)
{
try
{
return CocktailOperationResponse.OK([.. _cocktailBusinessLogicContract.GetCocktailHistoryByCocktail(id).Select(x => _mapper.Map<CocktailHistoryViewModel>(x))]);
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return CocktailOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return CocktailOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return CocktailOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return CocktailOperationResponse.InternalServerError(ex.Message);
}
}
public CocktailOperationResponse GetElement(string data)
{
try
{
return CocktailOperationResponse.OK(_mapper.Map<CocktailViewModel>(_cocktailBusinessLogicContract.GetCocktailByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return CocktailOperationResponse.BadRequest("Data is empty");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return CocktailOperationResponse.NotFound($"Not found element by data {data}");
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return CocktailOperationResponse.BadRequest($"Element by data: {data} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return CocktailOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return CocktailOperationResponse.InternalServerError(ex.Message);
}
}
public CocktailOperationResponse RegisterCocktail(CocktailBindingModel cocktailModel)
{
try
{
_cocktailBusinessLogicContract.InsertCocktail(_mapper.Map<CocktailDataModel>(cocktailModel));
return CocktailOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return CocktailOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return CocktailOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return CocktailOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return CocktailOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return CocktailOperationResponse.InternalServerError(ex.Message);
}
}
public CocktailOperationResponse ChangeCocktailInfo(CocktailBindingModel cocktailModel)
{
try
{
_cocktailBusinessLogicContract.UpdateCocktail(_mapper.Map<CocktailDataModel>(cocktailModel));
return CocktailOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return CocktailOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return CocktailOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return CocktailOperationResponse.BadRequest($"Not found element by Id {cocktailModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return CocktailOperationResponse.BadRequest(ex.Message);
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return CocktailOperationResponse.BadRequest($"Element by id: {cocktailModel.Id} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return CocktailOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return CocktailOperationResponse.InternalServerError(ex.Message);
}
}
public CocktailOperationResponse RemoveCocktail(string id)
{
try
{
_cocktailBusinessLogicContract.DeleteCocktail(id);
return CocktailOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return CocktailOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return CocktailOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return CocktailOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return CocktailOperationResponse.BadRequest($"Element by id: {id} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return CocktailOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return CocktailOperationResponse.InternalServerError(ex.Message);
}
}
}