Add Categoria and Departamento services and repositories; refactor UserService to implement GenericEntityService
This commit is contained in:
parent
ebbb373cf6
commit
8b14a236a2
|
|
@ -2,6 +2,7 @@
|
||||||
package com.ieslamar.GestionInventario;
|
package com.ieslamar.GestionInventario;
|
||||||
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
||||||
import com.ieslamar.GestionInventario.entities.Role;
|
import com.ieslamar.GestionInventario.entities.Role;
|
||||||
|
import com.ieslamar.GestionInventario.services.CategoriaService;
|
||||||
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
||||||
import com.ieslamar.GestionInventario.services.UserService;
|
import com.ieslamar.GestionInventario.services.UserService;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
|
@ -11,14 +12,14 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class DataInitializer {
|
public class DataInitializer {
|
||||||
|
|
||||||
@Bean
|
// @Bean
|
||||||
public CommandLineRunner initData(UserService userService,DepartamentoService departamentoService) {
|
// public CommandLineRunner initData(UserService userService,DepartamentoService departamentoService) {
|
||||||
return args -> {
|
// return args -> {
|
||||||
// userService.registerUser("Test","test123",Role.USER.name(),"test4@gmail.com",departamentoService.getDepartamentoById(1));
|
// userService.registerUser("Test","test123",Role.USER.name(),"test4@gmail.com",departamentoService.getDepartamentoById(1));
|
||||||
// userService.registerUser("admin", "admin123", Role.ADMIN.name(), "admin123@ejemplo.com",null);
|
// userService.registerUser("admin", "admin123", Role.ADMIN.name(), "admin123@ejemplo.com",null);
|
||||||
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null);
|
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null);
|
||||||
};
|
// };
|
||||||
}
|
// }
|
||||||
|
|
||||||
// @Bean
|
// @Bean
|
||||||
// public CommandLineRunner initData(DepartamentoService departamentoService) {
|
// public CommandLineRunner initData(DepartamentoService departamentoService) {
|
||||||
|
|
@ -27,6 +28,12 @@ public class DataInitializer {
|
||||||
// departamentoService.registerDepartamento("Ciencias");
|
// departamentoService.registerDepartamento("Ciencias");
|
||||||
// };
|
// };
|
||||||
// }
|
// }
|
||||||
|
// @Bean
|
||||||
|
// public CommandLineRunner initData(CategoriaService categoriaService) {
|
||||||
|
// return args -> {
|
||||||
|
// categoriaService.registerCategoria("Mueble");
|
||||||
|
// };
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,46 +1,50 @@
|
||||||
package com.ieslamar.GestionInventario.controllers;
|
package com.ieslamar.GestionInventario.controllers;
|
||||||
|
|
||||||
|
|
||||||
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
||||||
import com.ieslamar.GestionInventario.entities.Departamento;
|
import com.ieslamar.GestionInventario.entities.*;
|
||||||
import com.ieslamar.GestionInventario.entities.User;
|
import com.ieslamar.GestionInventario.repos.GenericEntityService;
|
||||||
|
import com.ieslamar.GestionInventario.services.CategoriaService;
|
||||||
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
||||||
import com.ieslamar.GestionInventario.services.UserService;
|
import com.ieslamar.GestionInventario.services.UserService;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
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.*;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext context;
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final DepartamentoService departamentoService;
|
private final DepartamentoService departamentoService;
|
||||||
|
private final CategoriaService categoriaService;
|
||||||
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||||
|
|
||||||
public UserController(UserService userService, DepartamentoService departamentoService) {
|
public UserController(UserService userService, DepartamentoService departamentoService, CategoriaService categoriaService) {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.departamentoService = departamentoService;
|
this.departamentoService = departamentoService;
|
||||||
|
this.categoriaService = categoriaService;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_list", true, null);
|
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 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 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 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 gestionUbicaciones = new BotonDTO("Gestión de Ubicaciones", "/user_management", true, null);
|
||||||
private final BotonDTO gestionInventario = new BotonDTO("Gestión de Inventario", "/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 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 inventario = new BotonDTO("Inventario", "/management", false, null);
|
||||||
|
|
@ -48,12 +52,12 @@ public class UserController {
|
||||||
|
|
||||||
private final BotonDTO home = new BotonDTO("Salir", "/home", false, "button_salir");
|
private final BotonDTO home = new BotonDTO("Salir", "/home", false, "button_salir");
|
||||||
|
|
||||||
private List<BotonDTO> filtrarBotones(Authentication auth,List<BotonDTO> botones){
|
private List<BotonDTO> filtrarBotones(Authentication auth, List<BotonDTO> botones) {
|
||||||
List<BotonDTO> botones_response = new ArrayList<>();
|
List<BotonDTO> botones_response = new ArrayList<>();
|
||||||
List<String> roles = auth.getAuthorities()
|
List<String> roles = auth.getAuthorities()
|
||||||
.stream()
|
.stream()
|
||||||
.map(GrantedAuthority::getAuthority)
|
.map(GrantedAuthority::getAuthority)
|
||||||
.toList();
|
.toList();
|
||||||
if (roles.contains("ROLE_ADMIN")) {
|
if (roles.contains("ROLE_ADMIN")) {
|
||||||
return botones;
|
return botones;
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +77,7 @@ public class UserController {
|
||||||
@GetMapping("/home")
|
@GetMapping("/home")
|
||||||
public String homePage(Model model) {
|
public String homePage(Model model) {
|
||||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||||
List<BotonDTO> botones = filtrarBotones(auth,new ArrayList<>(List.of(
|
List<BotonDTO> botones = filtrarBotones(auth, new ArrayList<>(List.of(
|
||||||
inventario,
|
inventario,
|
||||||
gestion
|
gestion
|
||||||
)));
|
)));
|
||||||
|
|
@ -82,9 +86,9 @@ public class UserController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/management")
|
@GetMapping("/management")
|
||||||
public String managementPage(Model model){
|
public String managementPage(Model model) {
|
||||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||||
List<BotonDTO>botones = filtrarBotones(auth,new ArrayList<>(List.of(
|
List<BotonDTO> botones = filtrarBotones(auth, new ArrayList<>(List.of(
|
||||||
gestionUsuarios,
|
gestionUsuarios,
|
||||||
gestionProductos,
|
gestionProductos,
|
||||||
gestionDepartamentos,
|
gestionDepartamentos,
|
||||||
|
|
@ -93,6 +97,7 @@ public class UserController {
|
||||||
gestionInventario,
|
gestionInventario,
|
||||||
gestionCategorias,
|
gestionCategorias,
|
||||||
gestionTiposDatos,
|
gestionTiposDatos,
|
||||||
|
// gestionDepartamentosTest,
|
||||||
home
|
home
|
||||||
)));
|
)));
|
||||||
model.addAttribute("buttons", botones);
|
model.addAttribute("buttons", botones);
|
||||||
|
|
@ -100,58 +105,122 @@ public class UserController {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//USERS--------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
@GetMapping("/userPage")
|
@GetMapping("/userPage")
|
||||||
public String userPage(Model model) {
|
public String userPage(Model model) {
|
||||||
model.addAttribute("departamentos",departamentoService.getAllDepartamentos());
|
model.addAttribute("departamentos", departamentoService.getAllDepartamentos());
|
||||||
return "userPage"; // Retorna la vista register.html
|
return "userPage"; // Retorna la vista register.html
|
||||||
}
|
}
|
||||||
|
@PostMapping("/userPage")
|
||||||
@PostMapping("/userPage")
|
public String userPage(@RequestParam(required = false) Long id,
|
||||||
public String userPage(@RequestParam String username,
|
@RequestParam String username,
|
||||||
@RequestParam String password,
|
@RequestParam String password,
|
||||||
@RequestParam String role,
|
@RequestParam String role,
|
||||||
@RequestParam String mail,
|
@RequestParam String mail,
|
||||||
@RequestParam(required = false) Integer iddepartamento
|
@RequestParam(required = false) Integer iddepartamento) {
|
||||||
) {
|
Departamento departamento = iddepartamento != null ? departamentoService.getDepartamentoById(iddepartamento) : null;
|
||||||
Departamento departamento = null;
|
if (id != null) {
|
||||||
if(iddepartamento != null)
|
// Editar usuario existente
|
||||||
departamento = departamentoService.getDepartamentoById(iddepartamento);
|
User user = userService.getUserById(id);
|
||||||
|
if (user != null) {
|
||||||
|
user.setNombre(username);
|
||||||
|
if (!password.isEmpty()) {
|
||||||
|
user.setPassword(password);
|
||||||
|
}
|
||||||
|
user.setRole(Role.valueOf(role));
|
||||||
|
user.setMail(mail);
|
||||||
|
user.setDepartamento(departamento);
|
||||||
|
userService.saveUser(user);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Crear nuevo usuario
|
||||||
userService.registerUser(username, password, role, mail, departamento);
|
userService.registerUser(username, password, role, mail, departamento);
|
||||||
return "redirect:/user_list"; // Redirige al login tras registrarse
|
|
||||||
}
|
}
|
||||||
|
return "redirect:/user_list";
|
||||||
|
}
|
||||||
|
|
||||||
@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);
|
||||||
userService.deleteUser(user); // Llama al servicio para eliminar el usuario
|
userService.deleteUser(user); // Llama al servicio para eliminar el usuario
|
||||||
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 userList(Model model) {
|
public String userList(Model model) {
|
||||||
return buildUserList(model, userService.getAllUsers());
|
return buildUserList(model, userService.getAllUsers());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String buildUserList(Model model, List<User> users) {
|
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"));
|
model.addAttribute("headers", List.of("ID", "Nombre", "Rol", "Email", "Departamento"));
|
||||||
model.addAttribute("items", users.stream()
|
model.addAttribute("items", users.stream()
|
||||||
.map(user -> Map.of(
|
.map(user -> Map.of(
|
||||||
"id", user.getId(),
|
"id", user.getId(),
|
||||||
"values", List.of(
|
"values", List.of(
|
||||||
user.getId(),
|
user.getId(),
|
||||||
user.getNombre(),
|
user.getNombre(),
|
||||||
user.getRole(),
|
user.getRole(),
|
||||||
user.getMail(),
|
user.getMail(),
|
||||||
user.getDepartamento() != null ? user.getDepartamento().getNombre() : " "
|
user.getDepartamento() != null ? user.getDepartamento().getNombre() : " "
|
||||||
)
|
)
|
||||||
))
|
))
|
||||||
.toList());
|
.toList());
|
||||||
model.addAttribute("actionsUrl", Map.of(
|
model.addAttribute("actionsUrl", Map.of(
|
||||||
"edit", "",
|
"edit", "/edit_user",
|
||||||
"delete", "/delete_user"
|
"delete", "/delete_user"
|
||||||
|
));
|
||||||
|
model.addAttribute("backUrl", "/Inventario/home");
|
||||||
|
model.addAttribute("addUrl", "/Inventario/userPage");
|
||||||
|
|
||||||
|
return "list";
|
||||||
|
}
|
||||||
|
@GetMapping("/edit_user/{id}")
|
||||||
|
public String editUser(@PathVariable Long id, Model model) {
|
||||||
|
User user = userService.getUserById(id);
|
||||||
|
if (user != null) {
|
||||||
|
model.addAttribute("user", user);
|
||||||
|
model.addAttribute("departamentos", departamentoService.getAllDepartamentos());
|
||||||
|
}
|
||||||
|
return "userPage"; // Redirige a la página de registro
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//DEPARTAMENTOS--------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@GetMapping("/departamento_list")
|
||||||
|
public String genericPageDepartamento(Model model) {
|
||||||
|
model.addAttribute("title", "Gestión de Departamentos");
|
||||||
|
model.addAttribute("headers", List.of("ID", "Nombre"));
|
||||||
|
model.addAttribute(
|
||||||
|
"items",
|
||||||
|
departamentoService.getAllDepartamentos().stream().map(departamento -> Map.of("id", departamento.getId(),
|
||||||
|
"values", List.of(departamento.getId(), departamento.getNombre()))).toList()
|
||||||
|
);
|
||||||
|
model.addAttribute("actionsUrl", Map.of(
|
||||||
|
"edit", "",
|
||||||
|
"delete", ""
|
||||||
|
));
|
||||||
|
model.addAttribute("backUrl", "/Inventario/home");
|
||||||
|
model.addAttribute("addUrl", "/Inventario/");
|
||||||
|
return "list";
|
||||||
|
}
|
||||||
|
|
||||||
|
//CATEGORIAS--------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private String buildCategoriaList(Model model, List<Categoria> categorias) {
|
||||||
|
model.addAttribute("title", "Gestión de categorías");
|
||||||
|
model.addAttribute("headers", List.of("ID", "Nombre"));
|
||||||
|
model.addAttribute("items", categorias.stream()
|
||||||
|
.map(categoria -> Map.of(
|
||||||
|
"id", categoria.getId(),
|
||||||
|
"values", List.of(
|
||||||
|
categoria.getId(),
|
||||||
|
categoria.getNombre()
|
||||||
|
)
|
||||||
|
))
|
||||||
|
.toList());
|
||||||
|
model.addAttribute("actionsUrl", Map.of(
|
||||||
|
"edit", "",
|
||||||
|
"delete", "/delete_user"
|
||||||
));
|
));
|
||||||
model.addAttribute("backUrl", "/Inventario/home");
|
model.addAttribute("backUrl", "/Inventario/home");
|
||||||
model.addAttribute("addUrl", "/Inventario/userPage");
|
model.addAttribute("addUrl", "/Inventario/userPage");
|
||||||
|
|
@ -160,27 +229,76 @@ public class UserController {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//GENERICO--------------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
@GetMapping("/list/{tabla}")
|
||||||
|
public String tablaList(Model model, @PathVariable String entity) {
|
||||||
|
String serviceName = entity.toLowerCase() + "Service";
|
||||||
|
Object serviceBean = context.getBean(serviceName);
|
||||||
|
|
||||||
|
return "";
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/departamento_list")
|
|
||||||
public String genericPageDepartamento(Model model) {
|
|
||||||
model.addAttribute("title", "Gestión de Departamentos");
|
|
||||||
model.addAttribute("headers", List.of("ID", "Nombre"));
|
|
||||||
model.addAttribute(
|
|
||||||
"items",
|
|
||||||
departamentoService.getAllDepartamentos().stream().map(departamento -> Map.of("id", departamento.getId(),
|
|
||||||
"values", List.of(departamento.getId(), departamento.getNombre()))).toList()
|
|
||||||
);
|
|
||||||
model.addAttribute("actionsUrl", Map.of(
|
|
||||||
"edit", "",
|
|
||||||
"delete", ""
|
|
||||||
));
|
|
||||||
model.addAttribute("backUrl", "/Inventario/home");
|
|
||||||
model.addAttribute("addUrl", "/Inventario/");
|
|
||||||
return "list";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/list/{entity}")
|
||||||
|
public String entityList(Model model, @PathVariable String entity) {
|
||||||
|
//construye el nombre del servicio de la entidad
|
||||||
|
String serviceName = entity.toLowerCase() + "Service";
|
||||||
|
try {
|
||||||
|
//busca el servicio de la entidad
|
||||||
|
Object serviceBean = context.getBean(serviceName);
|
||||||
|
//si el Bean es una instancia de GenericEntityService
|
||||||
|
if (serviceBean instanceof GenericEntityService<?> genericService) {
|
||||||
|
//genericService es un Servicio de los que hay, eso trae todas las instancias, entities sería por ejemplo user,categoria,etc
|
||||||
|
List<?> entities = genericService.findAll();
|
||||||
|
|
||||||
|
List<Map<String, Object>> items = entities.stream().map(e ->
|
||||||
|
Map.of(
|
||||||
|
"id", getId(e),
|
||||||
|
"values", genericService.getRowValues(e)
|
||||||
|
)
|
||||||
|
).toList();
|
||||||
|
|
||||||
|
model.addAttribute("title", "Gestión de " + entity);
|
||||||
|
model.addAttribute("headers", genericService.getHeaders());
|
||||||
|
model.addAttribute("items", items);
|
||||||
|
model.addAttribute("actionsUrl", Map.of(
|
||||||
|
"edit", "",
|
||||||
|
"delete", "" // puedes personalizar por entidad
|
||||||
|
));
|
||||||
|
model.addAttribute("backUrl", "/Inventario/home");
|
||||||
|
model.addAttribute("addUrl", "/Inventario/");
|
||||||
|
|
||||||
|
return "list";
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("No es un GenericEntityService válido");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
model.addAttribute("error", "No se pudo cargar la entidad: " + entity);
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getId(Object entity) {
|
||||||
|
try {
|
||||||
|
Method method = entity.getClass().getMethod("getId");
|
||||||
|
return method.invoke(entity);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
**/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
package com.ieslamar.GestionInventario.entities;
|
package com.ieslamar.GestionInventario.entities;
|
||||||
|
|
||||||
|
import com.ieslamar.GestionInventario.controllers.UserController;
|
||||||
|
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.springframework.orm.hibernate5.SpringSessionContext;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -28,4 +32,5 @@ public class Departamento {
|
||||||
|
|
||||||
@OneToMany(mappedBy = "departamento")
|
@OneToMany(mappedBy = "departamento")
|
||||||
private List<User> usuarios;
|
private List<User> usuarios;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.ieslamar.GestionInventario.entities;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class Entidad {
|
||||||
|
|
||||||
|
public abstract String getEntityName();
|
||||||
|
public abstract List<String> getEntityAttributes();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.ieslamar.GestionInventario.repos;
|
||||||
|
|
||||||
|
import com.ieslamar.GestionInventario.entities.Categoria;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CategoriaRepository extends JpaRepository<Categoria, Integer> {
|
||||||
|
Categoria findByNombre(String nombre);
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
package com.ieslamar.GestionInventario.repos;
|
package com.ieslamar.GestionInventario.repos;
|
||||||
|
|
||||||
import com.ieslamar.GestionInventario.entities.Departamento;
|
import com.ieslamar.GestionInventario.entities.Departamento;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
public interface DepartamentoRepository extends JpaRepository<Departamento, Integer> {
|
public interface DepartamentoRepository extends JpaRepository<Departamento, Integer> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.ieslamar.GestionInventario.repos;
|
||||||
|
|
||||||
|
import com.ieslamar.GestionInventario.entities.User;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface GenericEntityService<T> {
|
||||||
|
List<T> findAll();
|
||||||
|
List<String> getHeaders();
|
||||||
|
List<Object> getRowValues(T entity);
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
package com.ieslamar.GestionInventario.repos;
|
package com.ieslamar.GestionInventario.repos;
|
||||||
|
|
||||||
import com.ieslamar.GestionInventario.entities.User;
|
import com.ieslamar.GestionInventario.entities.User;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import java.util.Optional;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
@Repository
|
||||||
public interface UserRepository extends JpaRepository<User, Long> {
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
Optional<User> findByNombre(String username);
|
Optional<User> findByNombre(String username);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.ieslamar.GestionInventario.services;
|
||||||
|
|
||||||
|
import com.ieslamar.GestionInventario.entities.Categoria;
|
||||||
|
import com.ieslamar.GestionInventario.repos.CategoriaRepository;
|
||||||
|
import com.ieslamar.GestionInventario.repos.GenericEntityService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CategoriaService implements GenericEntityService<Categoria> {
|
||||||
|
private final CategoriaRepository categoriaRepository;
|
||||||
|
|
||||||
|
public CategoriaService(CategoriaRepository categoriaRepository) {
|
||||||
|
this.categoriaRepository = categoriaRepository;
|
||||||
|
}
|
||||||
|
public List<Categoria> getAllCategorias() {
|
||||||
|
return categoriaRepository.findAll();
|
||||||
|
}
|
||||||
|
public Categoria getCategoriaById(Integer id) {
|
||||||
|
return categoriaRepository.findById(id).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerCategoria(String nombre) {
|
||||||
|
Categoria categoria = new Categoria();
|
||||||
|
categoria.setNombre(nombre);
|
||||||
|
categoriaRepository.save(categoria);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Categoria> findAll() {
|
||||||
|
return categoriaRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getHeaders() {
|
||||||
|
return List.of("ID","Nombre");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Object> getRowValues(Categoria categoria) {
|
||||||
|
return List.of(
|
||||||
|
categoria.getId(),
|
||||||
|
categoria.getNombre()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,12 +3,12 @@ package com.ieslamar.GestionInventario.services;
|
||||||
import com.ieslamar.GestionInventario.entities.Departamento;
|
import com.ieslamar.GestionInventario.entities.Departamento;
|
||||||
import com.ieslamar.GestionInventario.entities.User;
|
import com.ieslamar.GestionInventario.entities.User;
|
||||||
import com.ieslamar.GestionInventario.repos.DepartamentoRepository;
|
import com.ieslamar.GestionInventario.repos.DepartamentoRepository;
|
||||||
import com.ieslamar.GestionInventario.repos.UserRepository;
|
import com.ieslamar.GestionInventario.repos.GenericEntityService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DepartamentoService {
|
public class DepartamentoService implements GenericEntityService<Departamento> {
|
||||||
private final DepartamentoRepository departamentoRepository;
|
private final DepartamentoRepository departamentoRepository;
|
||||||
|
|
||||||
public DepartamentoService(DepartamentoRepository departamentoRepository) {
|
public DepartamentoService(DepartamentoRepository departamentoRepository) {
|
||||||
|
|
@ -26,4 +26,22 @@ public class DepartamentoService {
|
||||||
departamento.setNombre(nombre);
|
departamento.setNombre(nombre);
|
||||||
departamentoRepository.save(departamento);
|
departamentoRepository.save(departamento);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List findAll() {
|
||||||
|
return departamentoRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getHeaders() {
|
||||||
|
return List.of("ID", "Nombre");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Object> getRowValues(Departamento departamento) {
|
||||||
|
return List.of(
|
||||||
|
departamento.getId(),
|
||||||
|
departamento.getNombre()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,22 @@ package com.ieslamar.GestionInventario.services;
|
||||||
import com.ieslamar.GestionInventario.entities.Departamento;
|
import com.ieslamar.GestionInventario.entities.Departamento;
|
||||||
import com.ieslamar.GestionInventario.entities.Role;
|
import com.ieslamar.GestionInventario.entities.Role;
|
||||||
import com.ieslamar.GestionInventario.entities.User;
|
import com.ieslamar.GestionInventario.entities.User;
|
||||||
|
import com.ieslamar.GestionInventario.repos.GenericEntityService;
|
||||||
import com.ieslamar.GestionInventario.repos.UserRepository;
|
import com.ieslamar.GestionInventario.repos.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService implements GenericEntityService<User> {
|
||||||
private final UserRepository userRepository;
|
@Autowired
|
||||||
|
public UserRepository userRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
||||||
this.userRepository = userRepository;
|
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,4 +40,32 @@ public class UserService {
|
||||||
public User getUserById(Long id) {
|
public User getUserById(Long id) {
|
||||||
return userRepository.findById(id).orElse(null);
|
return userRepository.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
public List<User> getAll() {
|
||||||
|
return userRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<User> findAll() {
|
||||||
|
return userRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getHeaders() {
|
||||||
|
return List.of("ID", "Nombre", "Rol", "Email", "Departamento");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Object> getRowValues(User user) {
|
||||||
|
return List.of(
|
||||||
|
user.getId(),
|
||||||
|
user.getNombre(),
|
||||||
|
user.getRole(),
|
||||||
|
user.getMail(),
|
||||||
|
user.getDepartamento() != null ? user.getDepartamento().getNombre() : " "
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveUser(User user) {
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ body {
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
tr{
|
tr{
|
||||||
text-align: left;
|
text-align: center;
|
||||||
}
|
}
|
||||||
tbody tr:hover {
|
tbody tr:hover {
|
||||||
background-color: rgba(255, 255, 255, 0.3);
|
background-color: rgba(255, 255, 255, 0.3);
|
||||||
|
|
|
||||||
|
|
@ -12,21 +12,17 @@
|
||||||
|
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<label>Usuario:</label>
|
<label>Usuario:</label>
|
||||||
<input type="text" name="username" required>
|
<input type="text" name="username" th:value="${user != null ? user.nombre : ''}" required>
|
||||||
</div>
|
|
||||||
<div class="input-group">
|
|
||||||
<label>Contraseña:</label>
|
|
||||||
<input type="password" name="password" required>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<label>Mail:</label>
|
<label>Mail:</label>
|
||||||
<input type="text" name="mail" required>
|
<input type="text" name="mail" th:value="${user != null ? user.mail : ''}" required>
|
||||||
</div>
|
</div>
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<label>Rol:</label>
|
<label>Rol:</label>
|
||||||
<select name="role">
|
<select name="role">
|
||||||
<option value="USER">Usuario</option>
|
<option value="USER" th:selected="${user != null and user.role.name() == 'USER'}">Usuario</option>
|
||||||
<option value="ADMIN">Administrador</option>
|
<option value="ADMIN" th:selected="${user != null and user.role.name() == 'ADMIN'}">Administrador</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
|
|
@ -35,7 +31,8 @@
|
||||||
<option value="">-- Sin departamento --</option>
|
<option value="">-- Sin departamento --</option>
|
||||||
<option th:each="departamento : ${departamentos}"
|
<option th:each="departamento : ${departamentos}"
|
||||||
th:value="${departamento.id}"
|
th:value="${departamento.id}"
|
||||||
th:text="${departamento.nombre}">
|
th:text="${departamento.nombre}"
|
||||||
|
th:selected="${user != null and user.departamento != null and user.departamento.id == departamento.id}">
|
||||||
</option>
|
</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue