diff --git a/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java b/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java index 4898481..31f7ad8 100644 --- a/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java +++ b/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java @@ -1,6 +1,7 @@ package com.ieslamar.GestionInventario.controllers; import com.ieslamar.GestionInventario.dto.BotonDTO; +import com.ieslamar.GestionInventario.dto.BotonManager; import com.ieslamar.GestionInventario.entities.*; import com.ieslamar.GestionInventario.repos.GenericEntityService; import com.ieslamar.GestionInventario.services.CategoriaService; @@ -31,27 +32,14 @@ public class UserController { private final DepartamentoService departamentoService; private final CategoriaService categoriaService; private static final Logger logger = LoggerFactory.getLogger(UserController.class); - - public UserController(UserService userService, DepartamentoService departamentoService, CategoriaService categoriaService) { + private final BotonManager botonManager; + public UserController(UserService userService, DepartamentoService departamentoService, CategoriaService categoriaService, BotonManager botonManager) { this.userService = userService; this.departamentoService = departamentoService; this.categoriaService = categoriaService; + this.botonManager = botonManager; } - private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_list", true, null); -// private final BotonDTO gestionDepartamentosTest = new BotonDTO("Gestión de departamentos generica", "/list/" + Categoria.class.getSimpleName(), true, null); - private final BotonDTO gestionProductos = new BotonDTO("Gestión de Productos", "/user_management", true, null); - private final BotonDTO gestionDepartamentos = new BotonDTO("Gestión de Departamentos", "/departamento_list", true, null); - private final BotonDTO gestionCategorias = new BotonDTO("Gestión de Categorías", "/user_management", true, null); - private final BotonDTO gestionUbicaciones = new BotonDTO("Gestión de Ubicaciones", "/user_management", true, null); - private final BotonDTO gestionInventario = new BotonDTO("Gestión de Inventario", "/inventario", true, null); - private final BotonDTO gestionTiposDatos = new BotonDTO("Gestión de Tipos de dato", "/user_management", true, null); - - private final BotonDTO inventario = new BotonDTO("Inventario", "/management", false, null); - private final BotonDTO gestion = new BotonDTO("Gestión", "/management", true, null); - - private final BotonDTO home = new BotonDTO("Salir", "/home", false, "button_salir"); - private List filtrarBotones(Authentication auth, List botones) { List botones_response = new ArrayList<>(); List roles = auth.getAuthorities() @@ -78,8 +66,8 @@ public class UserController { public String homePage(Model model) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); List botones = filtrarBotones(auth, new ArrayList<>(List.of( - inventario, - gestion + botonManager.getInventario(), + botonManager.getGestion() ))); model.addAttribute("buttons", botones); return "home"; @@ -89,16 +77,15 @@ public class UserController { public String managementPage(Model model) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); List botones = filtrarBotones(auth, new ArrayList<>(List.of( - gestionUsuarios, - gestionProductos, - gestionDepartamentos, - gestionCategorias, - gestionUbicaciones, - gestionInventario, - gestionCategorias, - gestionTiposDatos, -// gestionDepartamentosTest, - home + botonManager.getGestionUsuarios(), + botonManager.getGestionProductos(), + botonManager.getGestionDepartamentos(), + botonManager.getGestionCategorias(), + botonManager.getGestionUbicaciones(), + botonManager.getGestionInventario(), + botonManager.getGestionCategorias(), + botonManager.getGestionTiposDatos(), + botonManager.getHome() ))); model.addAttribute("buttons", botones); return "management"; @@ -108,37 +95,53 @@ public class UserController { //USERS-------------------------------------------------------------------------------------------------------- @GetMapping("/userPage") - public String userPage(Model model) { + public String userPage(@RequestParam(required = false) Long id, Model model) { model.addAttribute("departamentos", departamentoService.getAllDepartamentos()); - return "userPage"; // Retorna la vista register.html - } -@PostMapping("/userPage") -public String userPage(@RequestParam(required = false) Long id, - @RequestParam String username, - @RequestParam String password, - @RequestParam String role, - @RequestParam String mail, - @RequestParam(required = false) Integer iddepartamento) { - Departamento departamento = iddepartamento != null ? departamentoService.getDepartamentoById(iddepartamento) : null; - if (id != null) { - // Editar usuario existente - User user = userService.getUserById(id); - if (user != null) { - user.setNombre(username); - if (!password.isEmpty()) { - user.setPassword(password); + if (id != null) { + User usuario = userService.getUserById(id); + if (usuario != null) { + model.addAttribute("user", usuario); + } else { + model.addAttribute("error", "Usuario no encontrado con ID: " + id); } - user.setRole(Role.valueOf(role)); - user.setMail(mail); - user.setDepartamento(departamento); - userService.saveUser(user); + } else { + model.addAttribute("user", new User()); } - } else { - // Crear nuevo usuario - userService.registerUser(username, password, role, mail, departamento); + return "userPage"; + } + + // POST: Registrar nuevo usuario + @PostMapping("/register") + public String registerUser(@RequestParam String username, + @RequestParam String password, + @RequestParam String role, + @RequestParam String mail, + @RequestParam(required = false) Integer iddepartamento, + @RequestParam(required = false) Long id) { + Departamento departamento = null; + if (iddepartamento != null) { + departamento = departamentoService.getDepartamentoById(iddepartamento); + } + Role userRole; + try { + userRole = Role.valueOf(role.toUpperCase()); + } catch (IllegalArgumentException e) { + throw new RuntimeException("Rol no válido: " + role); + } + if (id != null) { + User Old = userService.getUserById(id); + Old.setNombre(username); + if(password != null) + Old.setPassword(password); + Old.setRole(Role.valueOf(role)); + Old.setMail(mail); + Old.setDepartamento(departamento); + userService.saveUser(Old); + } else { + userService.registerUser(username, password, userRole.toString(), mail, departamento); + } + return "redirect:/user_list"; // redirige a lista o página deseada } - return "redirect:/user_list"; -} @PostMapping("/delete_user") public String deleteUser(@RequestParam("id") Long id) { User user = userService.getUserById(id); diff --git a/src/main/java/com/ieslamar/GestionInventario/dto/BotonDTO.java b/src/main/java/com/ieslamar/GestionInventario/dto/BotonDTO.java index bf64d88..e0da3e5 100644 --- a/src/main/java/com/ieslamar/GestionInventario/dto/BotonDTO.java +++ b/src/main/java/com/ieslamar/GestionInventario/dto/BotonDTO.java @@ -2,6 +2,7 @@ package com.ieslamar.GestionInventario.dto; import lombok.Getter; import lombok.Setter; +import org.springframework.stereotype.Component; public class BotonDTO { private String texto; diff --git a/src/main/java/com/ieslamar/GestionInventario/dto/BotonManager.java b/src/main/java/com/ieslamar/GestionInventario/dto/BotonManager.java new file mode 100644 index 0000000..2eddb07 --- /dev/null +++ b/src/main/java/com/ieslamar/GestionInventario/dto/BotonManager.java @@ -0,0 +1,64 @@ +package com.ieslamar.GestionInventario.dto; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class BotonManager { + private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_list", true, null); + private final BotonDTO gestionProductos = new BotonDTO("Gestión de Productos", "/user_management", true, null); + private final BotonDTO gestionDepartamentos = new BotonDTO("Gestión de Departamentos", "/departamento_list", true, null); + private final BotonDTO gestionCategorias = new BotonDTO("Gestión de Categorías", "/user_management", true, null); + private final BotonDTO gestionUbicaciones = new BotonDTO("Gestión de Ubicaciones", "/user_management", true, null); + private final BotonDTO gestionInventario = new BotonDTO("Gestión de Inventario", "/inventario", true, null); + private final BotonDTO gestionTiposDatos = new BotonDTO("Gestión de Tipos de dato", "/user_management", true, null); + + private final BotonDTO inventario = new BotonDTO("Inventario", "/management", false, null); + private final BotonDTO gestion = new BotonDTO("Gestión", "/management", true, null); + + private final BotonDTO home = new BotonDTO("Salir", "/home", false, "button_salir"); + public BotonManager() { + } + + public BotonDTO getGestionUsuarios() { + return gestionUsuarios; + } + + public BotonDTO getGestionProductos() { + return gestionProductos; + } + + public BotonDTO getGestionDepartamentos() { + return gestionDepartamentos; + } + + public BotonDTO getGestionCategorias() { + return gestionCategorias; + } + + public BotonDTO getGestionUbicaciones() { + return gestionUbicaciones; + } + + public BotonDTO getGestionInventario() { + return gestionInventario; + } + + public BotonDTO getGestionTiposDatos() { + return gestionTiposDatos; + } + + public BotonDTO getInventario() { + return inventario; + } + + public BotonDTO getGestion() { + return gestion; + } + + public BotonDTO getHome() { + return home; + } +} diff --git a/src/main/resources/templates/list.html b/src/main/resources/templates/list.html index 1b1b25e..c8ed830 100644 --- a/src/main/resources/templates/list.html +++ b/src/main/resources/templates/list.html @@ -35,7 +35,9 @@
- + + +
- +
@@ -45,3 +47,9 @@
+ + + + + +