Fix user management routes, enhance user management page with a table, and add CSS for styling

This commit is contained in:
jon ander 2025-04-02 20:32:57 +02:00
parent ccc8be6bb1
commit ee4b48354d
8 changed files with 98 additions and 26 deletions

View File

@ -1,16 +1,20 @@
package com.ieslamar.GestionInventario.controllers;
import com.ieslamar.GestionInventario.entities.User;
import com.ieslamar.GestionInventario.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
@ -42,8 +46,15 @@ public class UserController {
public String managementPage(){
return "management";
}
@GetMapping("/user_managemment")
public String userManagementPage(){
// @GetMapping("/user_management")
// public String userManagementPage(){
// return "user_management";
// }
@GetMapping("/user_management")
public String listUsers(Model model) {
List<User> users = userService.getAllUsers();
model.addAttribute("users", users);
return "user_management";
}

View File

@ -9,6 +9,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@ -23,7 +24,6 @@ public class UserDetailsServiceImpl implements UserDetailsService {
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByNombre(username)
.orElseThrow(() -> new UsernameNotFoundException("Usuario no encontrado"));
return new org.springframework.security.core.userdetails.User(
user.getNombre(),
user.getPassword(),

View File

@ -3,12 +3,14 @@ package com.ieslamar.GestionInventario.services;
import com.ieslamar.GestionInventario.entities.Role;
import com.ieslamar.GestionInventario.entities.User;
import com.ieslamar.GestionInventario.repos.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@ -25,4 +27,7 @@ public class UserService {
user.setMail(mail);
userRepository.save(user);
}
public List<User> getAllUsers() {
return userRepository.findAll();
}
}

Binary file not shown.

View File

@ -0,0 +1,54 @@
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 20px;
}
.container {
width: 80%;
max-width: 800px;
border: 1px solid #ccc;
padding: 10px;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background-color: #f4f4f4;
}
table {
width: 100%;
border-collapse: collapse;
}
thead {
background-color: #ddd;
}
th, td {
padding: 10px;
border: 1px solid #ccc;
text-align: left;
}
.footer {
padding: 10px;
background-color: #f4f4f4;
text-align: right;
}
.id-column {
width: 50px; /* Columna ID más estrecha */
}
.email-column {
width: 250px; /* Columna Email más ancha */
}
.table-container {
height: 400px; /* Altura fija del contenedor */
overflow-y: auto; /* Scroll vertical */
border: 1px solid #ddd; /* Borde opcional */
}
.action-buttons {
display: flex;
justify-content: center;
gap: 10px; /* Espacio entre botones */
}

View File

@ -7,11 +7,11 @@
<body>
<h2 th:text="'Gestión'"></h2>
<div class="container1">
<a href="/Inventario/user_managemment">
<a href="/Inventario/user_management">
<button>Gestión de Usuarios</button>
</a>
<!-- TODO: Cambiar la ruta a la de gestionar inventario -->
<a href="/Inventario/user_managemment">
<a href="/Inventario/user_management">
<button>Gestión de Inventario</button>
</a>
<a href="/Inventario/home">

View File

@ -1,25 +1,27 @@
<!DOCTYPE html>
<html lang="es" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml">
<head>
<title>User management</title>
<link rel="stylesheet" th:href="@{/css/style.css}">
</head>
<body>
<h2 th:text="'Gestión de usuarios'"></h2>
<div class="container1">
<a href="/Inventario/register">
<button>Registrar usuario</button>
</a>
<!-- TODO: modificar ruta-->
<a href="/Inventario/register">
<button>Borrar usuario</button>
</a>
<a href="/Inventario/home">
<button class="button_salir">Volver al inicio</button>
</a>
</div>
<!-- TODO: añadir botón para para editar usuarios -->
</body>
<head>
<title>Gestión de Usuarios</title>
<!-- <link rel="stylesheet" th:href="@{/css/table_style.css}">-->
</head>
<body>
<h2>Gestión de Usuarios</h2>
<hr>
<table border="1px">
<thead>
<tr>ID</tr>
<tr>NOMBRE</tr>
<tr>ROL</tr>
<tr>EMAIL</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:number="${user.id}"></td>
</tr>
</tbody>
</table>
</body>
</html>