| | 1 | | using CoWorkingApp.Core.Application.Contracts.Services; |
| | 2 | | using CoWorkingApp.Core.Domain.DTOs; |
| | 3 | | using Microsoft.AspNetCore.Mvc; |
| | 4 | |
|
| | 5 | | namespace CoWorkingApp.API.Infrastructure.Presentation.Controllers |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Controlador para las operaciones relacionadas con la entidad User. |
| | 9 | | /// </summary> |
| | 10 | | [ApiController] |
| | 11 | | [Route("api/[controller]s")] // Se utiliza el plural "users" en la ruta para seguir convenciones RESTful |
| | 12 | | public class UserController : ControllerGeneric<IUserService, UserRequest, UserResponse> |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Constructor de la clase UserController. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="service">Instancia del servicio de usuarios.</param> |
| | 18 | | /// <param name="logger">Instancia del logger.</param> |
| 80 | 19 | | public UserController(IUserService service, ILogger<ControllerGeneric<IUserService, UserRequest, UserResponse>> |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Obtiene un usuario por su dirección de correo electrónico. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="email">Dirección de correo electrónico del usuario.</param> |
| | 25 | | /// <returns>ActionResult con el usuario correspondiente al correo electrónico o NotFound si no existe.</returns |
| | 26 | | [HttpGet("email/{email}")] |
| | 27 | | [ResponseCache(Duration = 60)] // Se agrega caché HTTP para mejorar el rendimiento |
| | 28 | | public async Task<ActionResult<UserResponse>> GetByEmail(string email) |
| 3 | 29 | | { |
| | 30 | | try |
| 3 | 31 | | { |
| | 32 | | // Llama al servicio para obtener un usuario por su dirección de correo electrónico |
| 3 | 33 | | var response = await _service.GetByEmailAsync(email); |
| | 34 | |
|
| 2 | 35 | | if (!response.Success) |
| 1 | 36 | | { |
| 1 | 37 | | response.Message = "Error occurred while retrieving the user by email."; |
| 1 | 38 | | response.Errors.Add(response.Message); |
| 1 | 39 | | return BadRequest(response); |
| | 40 | | } |
| | 41 | |
|
| 1 | 42 | | return Ok(response); |
| | 43 | | } |
| 1 | 44 | | catch (Exception) |
| 1 | 45 | | { |
| | 46 | | // Maneja cualquier otra excepción inesperada |
| 1 | 47 | | var exception = new Exception("An unexpected error occurred while getting the user by email."); |
| 1 | 48 | | _logger.LogError(exception, exception.Message); |
| 1 | 49 | | return StatusCode(500, HandleException(exception)); |
| | 50 | | } |
| 3 | 51 | | } |
| | 52 | | } |
| | 53 | | } |