Compare commits
12 Commits
f80add65dd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| f8d022d007 | |||
| 5f8ed1e7b8 | |||
| 38e573e5fc | |||
| f3e99fa830 | |||
| 6927c6b7e6 | |||
| 090d6c9a64 | |||
| 95c2e57ab1 | |||
| aa9b7d2056 | |||
| 5f1c8f93fd | |||
| 8890b72f19 | |||
| a6a3b5a971 | |||
| e5b699945c |
@@ -42,7 +42,9 @@ public class SecurityConfig {
|
|||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("/ws/**").permitAll() // WebSocket
|
.requestMatchers("/ws/**").permitAll() // WebSocket
|
||||||
.requestMatchers("/auth/**").permitAll()
|
.requestMatchers("/auth/**").permitAll()
|
||||||
|
.requestMatchers("/api/usuarios/confirmado/**").permitAll()
|
||||||
.requestMatchers("/favicon.ico", "/error", "/static/**", "/contador/**", "/api/**").permitAll()
|
.requestMatchers("/favicon.ico", "/error", "/static/**", "/contador/**", "/api/**").permitAll()
|
||||||
|
.requestMatchers("/admin/**").hasRole("ADMIN")
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.httpBasic(Customizer.withDefaults());
|
.httpBasic(Customizer.withDefaults());
|
||||||
|
|||||||
@@ -26,16 +26,16 @@ public class AppController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Registro
|
// Registro
|
||||||
@PostMapping("/auth/registrar")
|
// @PostMapping("/auth/registrar")
|
||||||
public ResponseEntity<String> registrar(@RequestParam String username,
|
// public ResponseEntity<String> registrar(@RequestParam String username,
|
||||||
@RequestParam String password) {
|
// @RequestParam String password) {
|
||||||
if (usuarioService.estaRegistrado(username)) {
|
// if (usuarioService.estaRegistrado(username)) {
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).body("Usuario ya existe ❌");
|
// return ResponseEntity.status(HttpStatus.CONFLICT).body("Usuario ya existe ❌");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
usuarioService.registrarUsuario(username, password, Set.of(Rol.CLIENTE));
|
// usuarioService.registrarUsuario(username, password, Set.of(Rol.CLIENTE), 1L);
|
||||||
return ResponseEntity.ok("Usuario registrado, pendiente de aprobación ✅");
|
// return ResponseEntity.ok("Usuario registrado, pendiente de aprobación ✅");
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,19 +2,28 @@ package com.example.fercoganbackend.controller;
|
|||||||
|
|
||||||
import com.example.fercoganbackend.component.ContadorWebSocketHandler;
|
import com.example.fercoganbackend.component.ContadorWebSocketHandler;
|
||||||
import com.example.fercoganbackend.dto.CorregirRequest;
|
import com.example.fercoganbackend.dto.CorregirRequest;
|
||||||
|
import com.example.fercoganbackend.entity.Puja;
|
||||||
import com.example.fercoganbackend.service.ContadorService;
|
import com.example.fercoganbackend.service.ContadorService;
|
||||||
|
import com.example.fercoganbackend.service.PujaService;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/contador")
|
@RequestMapping("/contador")
|
||||||
public class ContadorController {
|
public class ContadorController {
|
||||||
private final ContadorService contadorService;
|
private final ContadorService contadorService;
|
||||||
private final ContadorWebSocketHandler webSocketHandler;
|
private final ContadorWebSocketHandler webSocketHandler;
|
||||||
|
private final PujaService pujaService;
|
||||||
|
|
||||||
public ContadorController(ContadorService contadorService,
|
public ContadorController(
|
||||||
ContadorWebSocketHandler webSocketHandler) {
|
ContadorService contadorService,
|
||||||
|
ContadorWebSocketHandler webSocketHandler,
|
||||||
|
PujaService pujaService) {
|
||||||
this.contadorService = contadorService;
|
this.contadorService = contadorService;
|
||||||
this.webSocketHandler = webSocketHandler;
|
this.webSocketHandler = webSocketHandler;
|
||||||
|
this.pujaService = pujaService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/incrementar/{remate}/{lote}")
|
@PostMapping("/incrementar/{remate}/{lote}")
|
||||||
@@ -46,20 +55,41 @@ public class ContadorController {
|
|||||||
return valor;
|
return valor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/corregir/{remate}/{lote}")
|
@PutMapping("/corregir/pujaid/{id}/remateid/{remate}/loteid/{lote}")
|
||||||
public int corregirValor(
|
public int corregirValor(
|
||||||
|
@PathVariable Long id,
|
||||||
@PathVariable String remate,
|
@PathVariable String remate,
|
||||||
@PathVariable String lote,
|
@PathVariable String lote,
|
||||||
@RequestBody CorregirRequest request) {
|
@RequestBody CorregirRequest request) {
|
||||||
|
|
||||||
|
// Convertir IDs de String a Long
|
||||||
|
Long remateId = Long.valueOf(remate);
|
||||||
|
Long loteId = Long.valueOf(lote);
|
||||||
|
|
||||||
|
// Buscar la puja correspondiente por lote y remate
|
||||||
|
Puja puja = pujaService.findForIdAndLoteAndRemate(id, loteId, remateId)
|
||||||
|
.orElseThrow(() ->
|
||||||
|
new EntityNotFoundException(
|
||||||
|
"No existe puja para remate " + remate + " y lote " + lote
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Actualizar el monto de la puja y persistir
|
||||||
|
puja.setMonto((double) request.getNuevoValor());
|
||||||
|
pujaService.save(puja);
|
||||||
|
|
||||||
|
// Actualizar contador en memoria
|
||||||
String roomKey = remate + "-" + lote;
|
String roomKey = remate + "-" + lote;
|
||||||
int nuevoValor = request.getNuevoValor();
|
int nuevoValor = request.getNuevoValor();
|
||||||
|
|
||||||
contadorService.setContador(roomKey, nuevoValor);
|
contadorService.setContador(roomKey, nuevoValor);
|
||||||
|
|
||||||
|
// Notificar a clientes vía WebSocket
|
||||||
webSocketHandler.broadcastToRoom(remate, lote, String.valueOf(nuevoValor));
|
webSocketHandler.broadcastToRoom(remate, lote, String.valueOf(nuevoValor));
|
||||||
|
|
||||||
|
// Retornar el nuevo valor
|
||||||
return nuevoValor;
|
return nuevoValor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/pujas")
|
@RequestMapping("/api/pujas")
|
||||||
@@ -38,7 +39,7 @@ public class PujaController {
|
|||||||
|
|
||||||
//get coincidir con lote y remate
|
//get coincidir con lote y remate
|
||||||
@GetMapping("/loteyremate/{loteId}/{remateId}")
|
@GetMapping("/loteyremate/{loteId}/{remateId}")
|
||||||
public List<Puja> getForLoteAndRemate(@PathVariable Long loteId,@PathVariable Long remateId){
|
public List<Puja> getForLoteAndRemate(@PathVariable Long loteId, @PathVariable Long remateId){
|
||||||
return pujaService.findForLoteAndRemate(loteId,remateId);
|
return pujaService.findForLoteAndRemate(loteId,remateId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.example.fercoganbackend.controller;
|
||||||
|
|
||||||
|
import com.example.fercoganbackend.entity.Roles;
|
||||||
|
import com.example.fercoganbackend.repository.RolesRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/roles")
|
||||||
|
public class RolesController {
|
||||||
|
|
||||||
|
final RolesRepository rolesRepository;
|
||||||
|
|
||||||
|
RolesController(RolesRepository rolesRepository){
|
||||||
|
this.rolesRepository=rolesRepository;
|
||||||
|
}
|
||||||
|
@GetMapping
|
||||||
|
public List<Roles> getRoles(){
|
||||||
|
return rolesRepository.findAll();
|
||||||
|
}
|
||||||
|
@PostMapping
|
||||||
|
public Roles setRoles(@RequestBody Roles roles){
|
||||||
|
return rolesRepository.save(roles);
|
||||||
|
}
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public Roles updateRoles(@PathVariable Long id, @RequestBody Roles updatedRole) {
|
||||||
|
|
||||||
|
Roles role = rolesRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Rol no encontrado"));
|
||||||
|
|
||||||
|
role.setName(updatedRole.getName());
|
||||||
|
|
||||||
|
return rolesRepository.save(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.example.fercoganbackend.controller;
|
package com.example.fercoganbackend.controller;
|
||||||
|
|
||||||
import com.example.fercoganbackend.entity.Rol;
|
import com.example.fercoganbackend.entity.Rol;
|
||||||
|
import com.example.fercoganbackend.entity.Roles;
|
||||||
import com.example.fercoganbackend.entity.Usuario;
|
import com.example.fercoganbackend.entity.Usuario;
|
||||||
import com.example.fercoganbackend.otros.ConfirmadoTF;
|
import com.example.fercoganbackend.otros.ConfirmadoTF;
|
||||||
import com.example.fercoganbackend.service.UsuarioService;
|
import com.example.fercoganbackend.service.UsuarioService;
|
||||||
@@ -11,7 +12,9 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -31,7 +34,14 @@ public class UserController {
|
|||||||
// ✅ Registrar nuevo usuario
|
// ✅ Registrar nuevo usuario
|
||||||
@PostMapping("/registrar")
|
@PostMapping("/registrar")
|
||||||
public Usuario registrarUsuario(@RequestBody UsuarioRequest request) {
|
public Usuario registrarUsuario(@RequestBody UsuarioRequest request) {
|
||||||
return service.registrarUsuario(request.getUsername(), request.getPassword(), request.getRoles());
|
return service.registrarUsuario(
|
||||||
|
request.getUsername(),
|
||||||
|
request.getPassword(),
|
||||||
|
request.getRoles(),
|
||||||
|
request.getRol(),
|
||||||
|
request.getCi(),
|
||||||
|
request.getCelular(),
|
||||||
|
request.getNombre());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Listar usuarios pendientes (no aprobados)
|
// ✅ Listar usuarios pendientes (no aprobados)
|
||||||
@@ -65,10 +75,10 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Actualizar usuario (por id)
|
// ✅ Actualizar usuario (por id)
|
||||||
@PutMapping("/{id}")
|
//@PutMapping("/{id}")
|
||||||
public Usuario actualizarUsuario(@PathVariable Long id, @RequestBody Usuario usuario) {
|
//public Usuario actualizarUsuario(@PathVariable Long id, @RequestBody Usuario usuario) {
|
||||||
return service.actualizarUsuario(id, usuario);
|
// return service.actualizarUsuario(id, usuario);
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
// ✅ Eliminar usuario
|
// ✅ Eliminar usuario
|
||||||
@@ -84,12 +94,92 @@ public class UserController {
|
|||||||
return ResponseEntity.ok(id);
|
return ResponseEntity.ok(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/rol/{username}")
|
||||||
|
public ResponseEntity<Map<String, Long>> obtenerRolUsuario(@PathVariable String username) {
|
||||||
|
Long rolId = service.obtenerIdRolPorUsername(username);
|
||||||
|
|
||||||
|
Map<String, Long> response = new HashMap<>();
|
||||||
|
response.put("rolId", rolId);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public Usuario actualizarUsuario(@PathVariable Long id, @RequestBody UsuarioRequest request) {
|
||||||
|
return service.actualizarParcial(id, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ✅ DTO interno para registro
|
// ✅ DTO interno para registro
|
||||||
public static class UsuarioRequest {
|
public static class UsuarioRequest {
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private Set<Rol> roles;
|
private Set<Rol> roles; //ignora esto
|
||||||
|
private Long rolId;
|
||||||
|
private Long ci;
|
||||||
|
private Long celular;
|
||||||
|
private String nombre;
|
||||||
|
private boolean aprobado;
|
||||||
|
private boolean visible;
|
||||||
|
|
||||||
|
public boolean isAprobado() {
|
||||||
|
return aprobado;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAprobado(boolean aprobado) {
|
||||||
|
this.aprobado = aprobado;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVisible() {
|
||||||
|
return visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVisible(boolean visible) {
|
||||||
|
this.visible = visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getRolId() {
|
||||||
|
return rolId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRolId(Long rolId) {
|
||||||
|
this.rolId = rolId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCi() {
|
||||||
|
return ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCi(Long ci) {
|
||||||
|
this.ci = ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCelular() {
|
||||||
|
return celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCelular(Long celular) {
|
||||||
|
this.celular = celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getRol() {
|
||||||
|
return rolId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRol(Long rol) {
|
||||||
|
this.rolId = rol;
|
||||||
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
return username;
|
||||||
|
|||||||
32
src/main/java/com/example/fercoganbackend/entity/Roles.java
Normal file
32
src/main/java/com/example/fercoganbackend/entity/Roles.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package com.example.fercoganbackend.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Roles {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long Id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
Id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,11 +19,54 @@ public class Usuario {
|
|||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private Set<Rol> roles; // aquí consumes el enum
|
private Set<Rol> roles; // aquí consumes el enum
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "RolId")
|
||||||
|
private Roles rol;
|
||||||
|
|
||||||
|
private Long ci;
|
||||||
|
|
||||||
|
private Long celular;
|
||||||
|
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
private Boolean visible = true;
|
private Boolean visible = true;
|
||||||
|
|
||||||
// getters y setters
|
// getters y setters
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Long getCi() {
|
||||||
|
return ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCi(Long ci) {
|
||||||
|
this.ci = ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCelular() {
|
||||||
|
return celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCelular(Long celular) {
|
||||||
|
this.celular = celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Roles getRol() {
|
||||||
|
return rol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRol(Roles rol) {
|
||||||
|
this.rol = rol;
|
||||||
|
}
|
||||||
|
|
||||||
public Boolean getVisible() {
|
public Boolean getVisible() {
|
||||||
return visible;
|
return visible;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.Query;
|
|||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface PujaRepository extends JpaRepository<Puja, Long> {
|
public interface PujaRepository extends JpaRepository<Puja, Long> {
|
||||||
|
|
||||||
@@ -14,6 +15,8 @@ public interface PujaRepository extends JpaRepository<Puja, Long> {
|
|||||||
|
|
||||||
List<Puja> findByLote_IdAndLote_Remate_Id(Long loteId, Long remateId);
|
List<Puja> findByLote_IdAndLote_Remate_Id(Long loteId, Long remateId);
|
||||||
|
|
||||||
|
Optional<Puja> findByIdAndLote_IdAndLote_Remate_Id(Long id, Long loteId, Long remateId);
|
||||||
|
|
||||||
List<Puja> findByLote_IdAndLote_Remate_IdAndFechaAfter(
|
List<Puja> findByLote_IdAndLote_Remate_IdAndFechaAfter(
|
||||||
Long loteId,
|
Long loteId,
|
||||||
Long remateId,
|
Long remateId,
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.fercoganbackend.repository;
|
||||||
|
|
||||||
|
import com.example.fercoganbackend.entity.Roles;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface RolesRepository extends JpaRepository<Roles, Long> {
|
||||||
|
}
|
||||||
@@ -40,8 +40,12 @@ public class PujaService {
|
|||||||
return pujaRepository.findByLote_Remate_Id(id);
|
return pujaRepository.findByLote_Remate_Id(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Puja> findForLoteAndRemate(Long loteId, Long remateId){
|
public List<Puja> findForLoteAndRemate( Long loteId, Long remateId){
|
||||||
return pujaRepository.findByLote_IdAndLote_Remate_Id(loteId,remateId);
|
return pujaRepository.findByLote_IdAndLote_Remate_Id( loteId,remateId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Puja> findForIdAndLoteAndRemate(Long Id, Long loteId, Long remateId){
|
||||||
|
return pujaRepository.findByIdAndLote_IdAndLote_Remate_Id( Id, loteId,remateId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Puja> findForLoteAndRemateAndDate(Long loteId, Long remateId, LocalDateTime fecha){
|
public List<Puja> findForLoteAndRemateAndDate(Long loteId, Long remateId, LocalDateTime fecha){
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package com.example.fercoganbackend.service;
|
package com.example.fercoganbackend.service;
|
||||||
|
import com.example.fercoganbackend.controller.UserController;
|
||||||
import com.example.fercoganbackend.entity.Rol;
|
import com.example.fercoganbackend.entity.Rol;
|
||||||
|
import com.example.fercoganbackend.entity.Roles;
|
||||||
import com.example.fercoganbackend.entity.Usuario;
|
import com.example.fercoganbackend.entity.Usuario;
|
||||||
import com.example.fercoganbackend.otros.ConfirmadoTF;
|
import com.example.fercoganbackend.otros.ConfirmadoTF;
|
||||||
|
import com.example.fercoganbackend.repository.RolesRepository;
|
||||||
import com.example.fercoganbackend.repository.UsuarioRepository;
|
import com.example.fercoganbackend.repository.UsuarioRepository;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -12,21 +15,29 @@ import java.util.Set;
|
|||||||
public class UsuarioService {
|
public class UsuarioService {
|
||||||
private final UsuarioRepository repo;
|
private final UsuarioRepository repo;
|
||||||
private final PasswordEncoder encoder;
|
private final PasswordEncoder encoder;
|
||||||
|
private final RolesRepository rolesRepository;
|
||||||
|
|
||||||
public List<Usuario> getAll(){
|
public List<Usuario> getAll(){
|
||||||
return repo.findAll();
|
return repo.findAll();
|
||||||
}
|
}
|
||||||
public UsuarioService(UsuarioRepository repo, PasswordEncoder encoder) {
|
public UsuarioService(UsuarioRepository repo, PasswordEncoder encoder, RolesRepository rolesRepository) {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
this.encoder = encoder;
|
this.encoder = encoder;
|
||||||
|
this.rolesRepository = rolesRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Usuario registrarUsuario(String username, String password, Set<Rol> roles) {
|
public Usuario registrarUsuario(String username, String password, Set<Rol> roles, Long rolId, Long ci, Long celular, String nombre) {
|
||||||
|
Roles rol = rolesRepository.findById(rolId)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Rol no encontrado"));
|
||||||
Usuario u = new Usuario();
|
Usuario u = new Usuario();
|
||||||
u.setUsername(username);
|
u.setUsername(username);
|
||||||
u.setPassword(encoder.encode(password));
|
u.setPassword(encoder.encode(password));
|
||||||
u.setRoles(roles);
|
u.setRoles(roles);
|
||||||
|
u.setRol(rol);
|
||||||
u.setAprobado(false); // no aprobado hasta aceptación
|
u.setAprobado(false); // no aprobado hasta aceptación
|
||||||
|
u.setCi(ci);
|
||||||
|
u.setCelular(celular);
|
||||||
|
u.setNombre(nombre);
|
||||||
return repo.save(u);
|
return repo.save(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +93,58 @@ public class UsuarioService {
|
|||||||
return usuario.getId();
|
return usuario.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long obtenerIdRolPorUsername(String username) {
|
||||||
|
return repo.findByUsername(username)
|
||||||
|
.map(usuario -> usuario.getRol().getId()) // Navega: Usuario -> Rol -> Id
|
||||||
|
.orElse(null); // O podrías lanzar una excepción si el usuario no existe
|
||||||
|
}
|
||||||
|
|
||||||
|
public Usuario actualizarParcial(Long id, UserController.UsuarioRequest request) {
|
||||||
|
// 1. Buscamos el usuario actual o lanzamos error si no existe
|
||||||
|
Usuario usuarioExistente = repo.findById(id)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Usuario no encontrado"));
|
||||||
|
|
||||||
|
// 2. Actualizamos solo si el campo no es nulo
|
||||||
|
if (request.getUsername() != null) {
|
||||||
|
usuarioExistente.setUsername(request.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.getCi()!= null){
|
||||||
|
usuarioExistente.setCi(request.getCi());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.getCelular()!= null){
|
||||||
|
usuarioExistente.setCelular(request.getCelular());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.getNombre()!= null){
|
||||||
|
usuarioExistente.setNombre(request.getNombre());
|
||||||
|
}
|
||||||
|
if(request.isVisible()){
|
||||||
|
usuarioExistente.setVisible(request.isVisible());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.isAprobado()){
|
||||||
|
usuarioExistente.setAprobado(request.isAprobado());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getPassword() != null) {
|
||||||
|
// ¡No olvides encriptarla si cambias el password!
|
||||||
|
usuarioExistente.setPassword(encoder.encode(request.getPassword()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getRol() != null) {
|
||||||
|
// Buscamos el objeto Rol por su ID (necesitas el RolesRepository)
|
||||||
|
Roles nuevoRol = rolesRepository.findById(request.getRol())
|
||||||
|
.orElseThrow(() -> new RuntimeException("El Rol especificado no existe"));
|
||||||
|
|
||||||
|
usuarioExistente.setRol(nuevoRol); // Asignamos el objeto completo
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Guardamos los cambios
|
||||||
|
return repo.save(usuarioExistente);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user