From 6dcf2a99f57781423d12796cf1fbb0a1a470a234 Mon Sep 17 00:00:00 2001 From: andre00bejarano00vaca Date: Thu, 9 Oct 2025 16:26:30 -0400 Subject: [PATCH] =?UTF-8?q?a=C3=B1adimos=20usuariocontroller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 109 +++++++++++++++++- .../fercoganbackend/entity/Usuario.java | 15 ++- 2 files changed, 118 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/fercoganbackend/controller/UserController.java b/src/main/java/com/example/fercoganbackend/controller/UserController.java index b02531e..fe8c8bb 100644 --- a/src/main/java/com/example/fercoganbackend/controller/UserController.java +++ b/src/main/java/com/example/fercoganbackend/controller/UserController.java @@ -1,15 +1,120 @@ package com.example.fercoganbackend.controller; +import com.example.fercoganbackend.entity.Rol; +import com.example.fercoganbackend.entity.Usuario; +import com.example.fercoganbackend.otros.ConfirmadoTF; import com.example.fercoganbackend.service.UsuarioService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Set; + @RestController -@RequestMapping("/api/user") +@RequestMapping("/api/usuarios") public class UserController { + @Autowired - UsuarioService usuarioService; + private UsuarioService service; + // ✅ Obtener todos los usuarios + @GetMapping + public List getAllUsuarios() { + return service.getAll(); + } + + // ✅ Registrar nuevo usuario + @PostMapping("/registrar") + public Usuario registrarUsuario(@RequestBody UsuarioRequest request) { + return service.registrarUsuario(request.getUsername(), request.getPassword(), request.getRoles()); + } + + // ✅ Listar usuarios pendientes (no aprobados) + @GetMapping("/pendientes") + public List listarPendientes() { + return service.listarPendientes(); + } + + // ✅ Aceptar usuario (cambiar a aprobado = true) + @PatchMapping("/aceptar/{id}") + public Usuario aceptarUsuario(@PathVariable Long id) { + return service.aceptarUsuario(id); + } + + // ✅ Verificar si un usuario está aprobado + @GetMapping("/aprobado/{username}") + public boolean estaAprobado(@PathVariable String username) { + return service.estaAprobado(username); + } + + // ✅ Verificar si un usuario está registrado + @GetMapping("/registrado/{username}") + public boolean estaRegistrado(@PathVariable String username) { + return service.estaRegistrado(username); + } + + // ✅ Confirmar si está registrado y aprobado + @GetMapping("/confirmado/{username}") + public ConfirmadoTF registradoYAprobado(@PathVariable String username) { + return service.registradoYAprobado(username); + } + + // ✅ Actualizar usuario (por id) + @PutMapping("/{id}") + public Usuario actualizarUsuario(@PathVariable Long id, @RequestBody Usuario usuario) { + Usuario existente = service.getAll().stream() + .filter(u -> u.getId().equals(id)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Usuario no encontrado")); + + existente.setUsername(usuario.getUsername()); + existente.setPassword(usuario.getPassword()); + existente.setRoles(usuario.getRoles()); + existente.setAprobado(usuario.isAprobado()); + return service.registrarUsuario(existente.getUsername(), existente.getPassword(), existente.getRoles()); + } + + // ✅ Eliminar usuario + @DeleteMapping("/{id}") + public void eliminarUsuario(@PathVariable Long id) { + service.getAll().stream() + .filter(u -> u.getId().equals(id)) + .findFirst() + .ifPresent(u -> service.getAll().remove(u)); // eliminaría de lista temporal (idealmente manejarlo con repo.deleteById(id)) + } + + // ✅ DTO interno para registro + public static class UsuarioRequest { + private String username; + private String password; + private Set roles; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Set getRoles() { + return roles; + } + + public void setRoles(Set roles) { + this.roles = roles; + } + } } diff --git a/src/main/java/com/example/fercoganbackend/entity/Usuario.java b/src/main/java/com/example/fercoganbackend/entity/Usuario.java index 6619cc6..320b86d 100644 --- a/src/main/java/com/example/fercoganbackend/entity/Usuario.java +++ b/src/main/java/com/example/fercoganbackend/entity/Usuario.java @@ -19,8 +19,19 @@ public class Usuario { @Enumerated(EnumType.STRING) private Set roles; // aquí consumes el enum + private Boolean visible = true; + // getters y setters + + public Boolean getVisible() { + return visible; + } + + public void setVisible(Boolean visible) { + this.visible = visible; + } + public Long getId() { return id; } @@ -61,7 +72,3 @@ public class Usuario { this.roles = roles; } } - - - -