| 1 | package com.tecmilenio.mapsconect.controller; |
| 2 | |
| 3 | import com.tecmilenio.mapsconect.dto.ApiResponse; |
| 4 | import com.tecmilenio.mapsconect.dto.EstudianteDTO; |
| 5 | import com.tecmilenio.mapsconect.dto.EstudianteRequestDTO; |
| 6 | import com.tecmilenio.mapsconect.service.EstudianteService; |
| 7 | import jakarta.validation.Valid; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | import org.springframework.http.HttpStatus; |
| 10 | import org.springframework.http.ResponseEntity; |
| 11 | import org.springframework.security.core.Authentication; |
| 12 | import org.springframework.web.bind.annotation.*; |
| 13 | |
| 14 | import java.util.List; |
| 15 | |
| 16 | @RestController |
| 17 | @RequestMapping("/estudiantes") |
| 18 | public class EstudianteController { |
| 19 | |
| 20 | @Autowired |
| 21 | private EstudianteService estudianteService; |
| 22 | |
| 23 | @GetMapping |
| 24 | public ResponseEntity<ApiResponse<List<EstudianteDTO>>> listar() { |
| 25 | return ResponseEntity.ok(ApiResponse.success(estudianteService.listar())); |
| 26 | } |
| 27 | |
| 28 | @GetMapping("/me") |
| 29 | public ResponseEntity<ApiResponse<EstudianteDTO>> obtenerMiPerfil(Authentication authentication) { |
| 30 | EstudianteDTO estudiante = estudianteService.obtenerPerfilActual(authentication.getName()); |
| 31 | return ResponseEntity.ok(ApiResponse.success(estudiante)); |
| 32 | } |
| 33 | |
| 34 | @GetMapping("/{id}") |
| 35 | public ResponseEntity<ApiResponse<EstudianteDTO>> obtenerPorId(@PathVariable Integer id) { |
| 36 | return ResponseEntity.ok(ApiResponse.success(estudianteService.obtenerPorId(id))); |
| 37 | } |
| 38 | |
| 39 | @PostMapping |
| 40 | public ResponseEntity<ApiResponse<EstudianteDTO>> crear( |
| 41 | Authentication authentication, |
| 42 | @Valid @RequestBody EstudianteRequestDTO dto) { |
| 43 | EstudianteDTO estudiante = estudianteService.crearPerfil(authentication.getName(), dto); |
| 44 | return ResponseEntity.status(HttpStatus.CREATED) |
| 45 | .body(ApiResponse.success(estudiante, "Perfil de estudiante creado correctamente")); |
| 46 | } |
| 47 | |
| 48 | @PutMapping("/me") |
| 49 | public ResponseEntity<ApiResponse<EstudianteDTO>> actualizar( |
| 50 | Authentication authentication, |
| 51 | @Valid @RequestBody EstudianteRequestDTO dto) { |
| 52 | EstudianteDTO estudiante = estudianteService.actualizarPerfil(authentication.getName(), dto); |
| 53 | return ResponseEntity.ok(ApiResponse.success(estudiante, "Perfil actualizado correctamente")); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 |