| | | 1 | | using CoWorkingApp.Core.Application.Contracts.Services; |
| | | 2 | | using CoWorkingApp.Core.Domain.DTOs; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | |
| | | 6 | | namespace CoWorkingApp.API.Infrastructure.Presentation.Controllers |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Controlador para las operaciones relacionadas con la entidad Seat. |
| | | 10 | | /// </summary> |
| | | 11 | | [ApiController] |
| | | 12 | | [Route("api/[controller]s")] // Se utiliza el plural "seats" en la ruta para seguir convenciones RESTful |
| | | 13 | | public class SeatController : ControllerGeneric<ISeatService, SeatRequest, SeatResponse> |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Constructor de la clase SeatController. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="service">Instancia del servicio de asientos.</param> |
| | | 19 | | /// <param name="logger">Instancia del logger.</param> |
| | 104 | 20 | | public SeatController(ISeatService service, ILogger<ControllerGeneric<ISeatService, SeatRequest, SeatResponse>> |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Obtiene los asientos disponibles. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <returns>ActionResult con la colección de asientos disponibles o un error interno.</returns> |
| | | 26 | | [HttpGet("availables")] |
| | | 27 | | [AllowAnonymous] // Se permite acceso anónimo para obtener los asientos disponibles |
| | | 28 | | public async Task<ActionResult<IEnumerable<SeatResponse>>> GetAvailables() |
| | 3 | 29 | | { |
| | | 30 | | try |
| | 3 | 31 | | { |
| | | 32 | | // Llama al servicio para obtener los asientos disponibles |
| | 3 | 33 | | var seats = await _service.GetAvailablesAsync(); |
| | | 34 | | |
| | 8 | 35 | | if (seats.Any(e => !e.Success)) |
| | 1 | 36 | | { |
| | | 37 | | // Si hay respuestas no exitosas, se construye una lista de respuestas de error |
| | 5 | 38 | | var errorResponse = seats.Where(e => !e.Success).ToList(); |
| | | 39 | | // Se actualiza el mensaje de cada respuesta de error y se agregan los mensajes de error a la lista |
| | 1 | 40 | | errorResponse.ForEach(response => |
| | 1 | 41 | | { |
| | 1 | 42 | | response.Message = "Error occurred while retrieving seat"; |
| | 1 | 43 | | response.Errors.Add(response.Message); |
| | 2 | 44 | | }); |
| | | 45 | | |
| | | 46 | | // Se devuelve un estado de error con las respuestas de error |
| | 1 | 47 | | return NotFound(errorResponse); |
| | | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | return Ok(seats); |
| | | 51 | | } |
| | 1 | 52 | | catch (Exception) |
| | 1 | 53 | | { |
| | | 54 | | // Maneja cualquier excepción inesperada |
| | 1 | 55 | | var exception = new Exception("An unexpected error occurred while getting available seats."); |
| | 1 | 56 | | _logger.LogError(exception, exception.Message); |
| | 1 | 57 | | return StatusCode(500, HandleException(exception)); |
| | | 58 | | } |
| | 3 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Obtiene asientos por nombre. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="name">Nombre de los asientos a obtener.</param> |
| | | 65 | | /// <returns>ActionResult con la colección de asientos correspondientes al nombre o un error interno.</returns> |
| | | 66 | | [HttpGet("by-name/{name}")] |
| | | 67 | | public async Task<ActionResult<SeatResponse>> GetByName(string name) |
| | 5 | 68 | | { |
| | | 69 | | try |
| | 5 | 70 | | { |
| | | 71 | | // Llama al servicio para obtener los asientos por nombre |
| | 5 | 72 | | var seat = await _service.GetByNameAsync(name); |
| | | 73 | | |
| | 4 | 74 | | if (!seat.Success) |
| | 2 | 75 | | { |
| | | 76 | | // Maneja el caso específico de que la entidad no se encuentre |
| | 2 | 77 | | seat.Message = "Entity not found by name"; |
| | 2 | 78 | | seat.Errors.Add(seat.Message); |
| | | 79 | | |
| | 2 | 80 | | return NotFound(seat); |
| | | 81 | | } |
| | | 82 | | |
| | 2 | 83 | | return Ok(seat); |
| | | 84 | | } |
| | 1 | 85 | | catch (Exception) |
| | 1 | 86 | | { |
| | | 87 | | // Maneja cualquier otra excepción inesperada |
| | 1 | 88 | | var exception = new Exception("An unexpected error occurred while getting seat by name."); |
| | 1 | 89 | | _logger.LogError(exception, exception.Message); |
| | 1 | 90 | | return StatusCode(500, HandleException(exception)); |
| | | 91 | | } |
| | 5 | 92 | | } |
| | | 93 | | |
| | | 94 | | } |
| | | 95 | | } |