Update search form in list.html; modify UserController for user list handling and enhance application properties

This commit is contained in:
jon ander 2025-04-30 19:57:10 +02:00
parent e7eeb45db1
commit ebbb373cf6
3 changed files with 33 additions and 18 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
@ -119,6 +120,8 @@ public class UserController {
return "redirect:/user_list"; // Redirige al login tras registrarse return "redirect:/user_list"; // Redirige al login tras registrarse
} }
@PostMapping("/delete_user") @PostMapping("/delete_user")
public String deleteUser(@RequestParam("id") Long id) { public String deleteUser(@RequestParam("id") Long id) {
User user = userService.getUserById(id); User user = userService.getUserById(id);
@ -126,15 +129,24 @@ public class UserController {
return "redirect:/user_list"; // Redirige a la página de gestión de usuarios return "redirect:/user_list"; // Redirige a la página de gestión de usuarios
} }
@GetMapping("/user_list") @GetMapping("/user_list")
public String genericPage(Model model) { public String userList(Model model) {
return buildUserList(model, userService.getAllUsers());
}
private String buildUserList(Model model, List<User> users) {
model.addAttribute("title", "Gestión de usuarios"); model.addAttribute("title", "Gestión de usuarios");
model.addAttribute("headers", List.of("ID", "Nombre", "Rol", "Email","Departamento", "Acciones")); model.addAttribute("headers", List.of("ID", "Nombre", "Rol", "Email","Departamento"));
model.addAttribute("items", userService.getAllUsers().stream() model.addAttribute("items", users.stream()
.map(user -> Map.of( .map(user -> Map.of(
"id", user.getId(), "id", user.getId(),
"values", List.of(user.getId(), user.getNombre(), user.getRole(), user.getMail(), user.getDepartamento() != null ? user.getDepartamento().getNombre() : " ") "values", List.of(
user.getId(),
user.getNombre(),
user.getRole(),
user.getMail(),
user.getDepartamento() != null ? user.getDepartamento().getNombre() : " "
)
)) ))
.toList()); .toList());
model.addAttribute("actionsUrl", Map.of( model.addAttribute("actionsUrl", Map.of(
@ -143,21 +155,24 @@ public class UserController {
)); ));
model.addAttribute("backUrl", "/Inventario/home"); model.addAttribute("backUrl", "/Inventario/home");
model.addAttribute("addUrl", "/Inventario/userPage"); model.addAttribute("addUrl", "/Inventario/userPage");
return "list"; return "list";
} }
@GetMapping("/departamento_list") @GetMapping("/departamento_list")
public String genericPageDepartamento(Model model) { public String genericPageDepartamento(Model model) {
model.addAttribute("title", "Gestión de Departamentos"); model.addAttribute("title", "Gestión de Departamentos");
model.addAttribute("headers", List.of("ID", "Nombre","Acciones")); model.addAttribute("headers", List.of("ID", "Nombre"));
model.addAttribute( model.addAttribute(
"items", "items",
departamentoService.getAllDepartamentos().stream().map(departamento -> Map.of("id", departamento.getId(), departamentoService.getAllDepartamentos().stream().map(departamento -> Map.of("id", departamento.getId(),
"values", List.of(departamento.getId(), departamento.getNombre()))).toList() "values", List.of(departamento.getId(), departamento.getNombre()))).toList()
); );
model.addAttribute("actionsUrl", Map.of( model.addAttribute("actionsUrl", Map.of(
"edit", "", "edit", "",
"delete", "" "delete", ""

View File

@ -11,7 +11,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/Inventario?useSSL=false&server
spring.datasource.username=InventarioSpring spring.datasource.username=InventarioSpring
spring.datasource.password=1234567891 spring.datasource.password=1234567891
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Configuración de JPA e Hibernate # Configuración de JPA e Hibernate
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=validate spring.jpa.hibernate.ddl-auto=validate

View File

@ -9,24 +9,25 @@
<h2 th:text="${title}"></h2> <h2 th:text="${title}"></h2>
<!-- Formulario de búsqueda --> <!-- Formulario de búsqueda -->
<form th:action="@{/search}" method="get" class="search-form"> <form th:action="@{/filtered_list}" method="get" class="search-form">
<label for="headers">Filtro:</label>
<select id="headers" name="filter">
<option value="">-- Sin filtro --</option>
<option th:each="header : ${headers}"
th:value="${header}"
th:text="${header}">
</option>
</select>
<input type="text" name="query" placeholder="Buscar..." required> <input type="text" name="query" placeholder="Buscar..." required>
<button type="submit">Buscar</button> <button type="submit">Buscar</button>
</form> </form>
<label for="headers">Filtro:</label>
<select id="headers" name="">
<option value="">-- Sin filtro --</option>
<option th:each="headers : ${headers}"
th:value="${headers}"
th:text="${headers}">
</option>
</select>
<div class="table-container"> <div class="table-container">
<table class="table1"> <table class="table1">
<thead> <thead>
<tr> <tr>
<th th:each="header : ${headers}" th:text="${header}"></th> <th th:each="header : ${headers}" th:text="${header}"></th>
<th>Acciones</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>