Enhance button management and user services; refactor BotonDTO and BotonManager, add Factura and Item repositories and services, and update UserController for product handling
This commit is contained in:
parent
f882fd173b
commit
062fe315e0
|
|
@ -5,10 +5,13 @@ import com.ieslamar.GestionInventario.dto.BotonManager;
|
|||
import com.ieslamar.GestionInventario.entities.*;
|
||||
import com.ieslamar.GestionInventario.services.CategoriaService;
|
||||
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
||||
import com.ieslamar.GestionInventario.services.ProductoService;
|
||||
import com.ieslamar.GestionInventario.services.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.CredentialsContainer;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
|
@ -23,31 +26,46 @@ import java.util.*;
|
|||
public class UserController {
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
//SERVICES----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
private final UserService userService;
|
||||
private final DepartamentoService departamentoService;
|
||||
private final CategoriaService categoriaService;
|
||||
private final ProductoService productoService;
|
||||
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
private final BotonManager botonManager;
|
||||
public UserController(UserService userService, DepartamentoService departamentoService, CategoriaService categoriaService, BotonManager botonManager) {
|
||||
public UserController(UserService userService, DepartamentoService departamentoService, CategoriaService categoriaService, ProductoService productoService, BotonManager botonManager) {
|
||||
this.userService = userService;
|
||||
this.departamentoService = departamentoService;
|
||||
this.categoriaService = categoriaService;
|
||||
this.productoService = productoService;
|
||||
this.botonManager = botonManager;
|
||||
}
|
||||
|
||||
private User getCurrentUser(){
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
org.springframework.security.core.userdetails.User springUser = (org.springframework.security.core.userdetails.User) auth.getPrincipal();
|
||||
auth.getCredentials();
|
||||
return userService.getUserByName(springUser.getUsername());
|
||||
}
|
||||
private List<BotonDTO> filtrarBotones(Authentication auth, List<BotonDTO> botones) {
|
||||
List<BotonDTO> botones_response = new ArrayList<>();
|
||||
List<String> roles = auth.getAuthorities()
|
||||
.stream()
|
||||
.map(GrantedAuthority::getAuthority)
|
||||
.toList();
|
||||
User currentUser = getCurrentUser();
|
||||
if (roles.contains("ROLE_ADMIN")) {
|
||||
return botones;
|
||||
}
|
||||
for (BotonDTO botone : botones) {
|
||||
if (!botone.isRequireAdmin()) {
|
||||
botones_response.add(botone);
|
||||
for (BotonDTO boton : botones) {
|
||||
if (!boton.isRequireAdmin()) {
|
||||
if(boton.getDepartamento()== null){
|
||||
logger.info("test");
|
||||
botones_response.add(boton);
|
||||
}else {
|
||||
if(boton.getDepartamento().getNombre().equals(currentUser.getDepartamento().getNombre()))
|
||||
botones_response.add(boton);
|
||||
}
|
||||
}
|
||||
}
|
||||
return botones_response;
|
||||
|
|
@ -79,7 +97,6 @@ public class UserController {
|
|||
botonManager.getGestionCategorias(),
|
||||
botonManager.getGestionUbicaciones(),
|
||||
botonManager.getGestionInventario(),
|
||||
botonManager.getGestionCategorias(),
|
||||
botonManager.getGestionTiposDatos(),
|
||||
botonManager.getHome()
|
||||
)));
|
||||
|
|
@ -202,7 +219,28 @@ public class UserController {
|
|||
// return "userPage"; // Redirige a la página de registro
|
||||
// }
|
||||
|
||||
|
||||
//PRODUCTOS--------------------------------------------------------------------------------------------------------
|
||||
@GetMapping("/producto_list")
|
||||
public String productoList(Model model) {
|
||||
model.addAttribute("title", "Gestión de Productos de" + getCurrentUser().getDepartamento().getNombre());
|
||||
model.addAttribute("headers", List.of("ID", "Nombre","Categoría","Desc"));
|
||||
model.addAttribute(
|
||||
"items",
|
||||
productoService.getAllProductos().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";
|
||||
}
|
||||
//DEPARTAMENTOS--------------------------------------------------------------------------------------------------------
|
||||
|
||||
@GetMapping("/departamento_list")
|
||||
|
|
|
|||
|
|
@ -1,65 +1,19 @@
|
|||
package com.ieslamar.GestionInventario.dto;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Departamento;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class BotonDTO {
|
||||
private String texto;
|
||||
private String url;
|
||||
private boolean requireAdmin;
|
||||
private String cssClass;
|
||||
|
||||
public BotonDTO(String texto, String url) {
|
||||
this(texto, url, false, null);
|
||||
}
|
||||
|
||||
public BotonDTO(String texto, String url, boolean requireAdmin) {
|
||||
this(texto, url, requireAdmin, null);
|
||||
}
|
||||
|
||||
public BotonDTO(String texto, String url, String cssClass) {
|
||||
this(texto, url, false, cssClass);
|
||||
}
|
||||
|
||||
public BotonDTO(String texto, String url, boolean requireAdmin, String cssClass) {
|
||||
this.texto = texto;
|
||||
this.url = url;
|
||||
this.requireAdmin = requireAdmin;
|
||||
this.cssClass = cssClass;
|
||||
}
|
||||
|
||||
public String getTexto() {
|
||||
return texto;
|
||||
}
|
||||
|
||||
public void setTexto(String texto) {
|
||||
this.texto = texto;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public boolean isRequireAdmin() {
|
||||
return requireAdmin;
|
||||
}
|
||||
|
||||
public void setRequireAdmin(boolean requireAdmin) {
|
||||
this.requireAdmin = requireAdmin;
|
||||
}
|
||||
|
||||
public String getCssClass() {
|
||||
return cssClass;
|
||||
}
|
||||
|
||||
public void setCssClass(String cssClass) {
|
||||
this.cssClass = cssClass;
|
||||
}
|
||||
private Departamento departamento;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
|
@ -68,6 +22,7 @@ public class BotonDTO {
|
|||
", url='" + url + '\'' +
|
||||
", requireAdmin=" + requireAdmin +
|
||||
", cssClass='" + cssClass + '\'' +
|
||||
", departamento='" + departamento.getNombre() + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,64 +1,46 @@
|
|||
package com.ieslamar.GestionInventario.dto;
|
||||
|
||||
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Getter
|
||||
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", false, 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 DepartamentoService departamentoService;
|
||||
private final BotonDTO gestionUsuarios ;
|
||||
private final BotonDTO gestionProductos ;
|
||||
private final BotonDTO gestionDepartamentos;
|
||||
private final BotonDTO gestionCategorias ;
|
||||
private final BotonDTO gestionUbicaciones ;
|
||||
private final BotonDTO gestionInventario ;
|
||||
private final BotonDTO gestionTiposDatos ;
|
||||
|
||||
private final BotonDTO inventario = new BotonDTO("Inventario", "/management", false, null);
|
||||
private final BotonDTO gestion = new BotonDTO("Gestión", "/management", true, null);
|
||||
private final BotonDTO verProductos;
|
||||
|
||||
private final BotonDTO home = new BotonDTO("Salir", "/home", false, "button_salir");
|
||||
public BotonManager() {
|
||||
|
||||
private final BotonDTO inventario;
|
||||
private final BotonDTO gestion = BotonDTO.builder().texto("Gestión").url("/management").requireAdmin(true).build();
|
||||
|
||||
private final BotonDTO home = BotonDTO.builder().texto("Salir").url("/home").requireAdmin(false).cssClass("button_salir").build();
|
||||
public BotonManager(DepartamentoService departamentoService) {
|
||||
this.departamentoService = departamentoService;
|
||||
|
||||
this.verProductos = BotonDTO.builder().build();
|
||||
|
||||
|
||||
|
||||
this.gestionDepartamentos = BotonDTO.builder().texto("Gestión de Departamentos").url("/departamento_list").requireAdmin(false).build();
|
||||
this.inventario = BotonDTO.builder().texto("Inventario").url("/management").requireAdmin(false).build();
|
||||
this.gestionUsuarios= BotonDTO.builder().texto("Gestión de usuarios").url( "/user_list").requireAdmin(true).build();
|
||||
this.gestionProductos= BotonDTO.builder().texto("Gestión de Productos").url("/user_management").requireAdmin(true).build();
|
||||
this.gestionCategorias= BotonDTO.builder().texto("Gestión de Categorías").url("/user_management").requireAdmin(true).build();
|
||||
this.gestionUbicaciones= BotonDTO.builder().texto("Gestión de Ubicaciones").url("/user_management").requireAdmin(true).build();
|
||||
this.gestionInventario= BotonDTO.builder().texto("Gestión de Inventario").url("/inventario").requireAdmin(true).build();
|
||||
this.gestionTiposDatos= BotonDTO.builder().texto("Gestión de Tipos de dato").url("/user_management").requireAdmin(true).build();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.entities;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.UserRepository;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
|
|
@ -12,6 +17,7 @@ import java.util.List;
|
|||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) // ID autoincremental
|
||||
private Long id;
|
||||
|
|
@ -36,7 +42,6 @@ public class User {
|
|||
@JoinColumn(name = "departamentos_iddepartamentos", nullable = true)
|
||||
private Departamento departamento;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -7,5 +7,5 @@ import org.springframework.stereotype.Repository;
|
|||
|
||||
@Repository
|
||||
public interface DepartamentoRepository extends JpaRepository<Departamento, Integer> {
|
||||
|
||||
Departamento getDepartamentoByNombre(String nombre);
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Factura;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface FacturaRepository extends JpaRepository<Factura,Integer> {
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Item;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ItemRepository extends JpaRepository<Item,Integer> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Producto;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ProductoRepository extends JpaRepository<Producto,Integer> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Propiedad;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PropiedadRepository extends JpaRepository<Propiedad,Integer> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Proveedor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ProveedorRepository extends JpaRepository<Proveedor,Integer> {
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.TipoDato;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface TipoDatoRepository extends JpaRepository<TipoDato,Integer> {
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Ubicacion;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UbicacionRepository extends JpaRepository<Ubicacion,Integer> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.ieslamar.GestionInventario.repos;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Valor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ValorRepository extends JpaRepository<Valor,Integer> {
|
||||
}
|
||||
|
|
@ -27,4 +27,7 @@ public class DepartamentoService {
|
|||
}
|
||||
|
||||
|
||||
public Departamento getDepartamentoByName(String nombre) {
|
||||
return departamentoRepository.getDepartamentoByNombre(nombre);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.FacturaRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class FacturaService {
|
||||
private final FacturaRepository facturaRepository;
|
||||
|
||||
public FacturaService(FacturaRepository facturaRepository) {
|
||||
this.facturaRepository = facturaRepository;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.ItemRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ItemService {
|
||||
private final ItemRepository itemRepository;
|
||||
|
||||
public ItemService(ItemRepository itemRepository) {
|
||||
this.itemRepository = itemRepository;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.entities.Departamento;
|
||||
import com.ieslamar.GestionInventario.entities.Producto;
|
||||
import com.ieslamar.GestionInventario.repos.ProductoRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ProductoService {
|
||||
private final ProductoRepository productoRepository;
|
||||
|
||||
public ProductoService(ProductoRepository productoRepository) {
|
||||
this.productoRepository = productoRepository;
|
||||
}
|
||||
public List<Producto> getAllProductos(){
|
||||
return productoRepository.findAll();
|
||||
}
|
||||
public Producto getProductoById(Integer id) {
|
||||
return productoRepository.findById(id).orElse(null);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.PropiedadRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PropiedadService {
|
||||
private final PropiedadRepository propiedadRepository;
|
||||
|
||||
public PropiedadService(PropiedadRepository propiedadRepository) {
|
||||
this.propiedadRepository = propiedadRepository;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.ProveedorRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ProveedorService {
|
||||
private final ProveedorRepository proveedorRepository;
|
||||
|
||||
public ProveedorService(ProveedorRepository proveedorRepository) {
|
||||
this.proveedorRepository = proveedorRepository;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.TipoDatoRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TipoDatoService {
|
||||
private final TipoDatoRepository tipoDatoRepository;
|
||||
|
||||
public TipoDatoService(TipoDatoRepository tipoDatoRepository) {
|
||||
this.tipoDatoRepository = tipoDatoRepository;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.UbicacionRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UbicacionService {
|
||||
private final UbicacionRepository ubicacionRepository;
|
||||
|
||||
public UbicacionService(UbicacionRepository ubicacionRepository) {
|
||||
this.ubicacionRepository = ubicacionRepository;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ import java.util.List;
|
|||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
public class UserService{
|
||||
@Autowired
|
||||
public UserRepository userRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
|
@ -58,4 +58,8 @@ public class UserService {
|
|||
original.setDepartamento(user.getDepartamento());
|
||||
userRepository.save(original);
|
||||
}
|
||||
|
||||
public User getUserByName(String name) {
|
||||
return userRepository.findByNombre(name).orElse(null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ieslamar.GestionInventario.services;
|
||||
|
||||
import com.ieslamar.GestionInventario.repos.ValorRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ValorService {
|
||||
private final ValorRepository valorRepository;
|
||||
|
||||
public ValorService(ValorRepository valorRepository) {
|
||||
this.valorRepository = valorRepository;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in New Issue