añadimos usuariocontroller
Some checks failed
Deploy Spring Boot App / build-and-deploy (push) Has been cancelled
Some checks failed
Deploy Spring Boot App / build-and-deploy (push) Has been cancelled
This commit is contained in:
@@ -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<Usuario> 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<Usuario> 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<Rol> 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<Rol> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(Set<Rol> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,19 @@ public class Usuario {
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Set<Rol> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user