Compare commits
3 Commits
f80add65dd
...
8890b72f19
| Author | SHA1 | Date | |
|---|---|---|---|
| 8890b72f19 | |||
| a6a3b5a971 | |||
| e5b699945c |
@@ -43,6 +43,7 @@ public class SecurityConfig {
|
|||||||
.requestMatchers("/ws/**").permitAll() // WebSocket
|
.requestMatchers("/ws/**").permitAll() // WebSocket
|
||||||
.requestMatchers("/auth/**").permitAll()
|
.requestMatchers("/auth/**").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());
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class AppController {
|
|||||||
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")
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -31,7 +32,7 @@ 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() );
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Listar usuarios pendientes (no aprobados)
|
// ✅ Listar usuarios pendientes (no aprobados)
|
||||||
@@ -85,11 +86,21 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ✅ 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;
|
||||||
|
|
||||||
|
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,23 @@ 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 Boolean visible = true;
|
private Boolean visible = true;
|
||||||
|
|
||||||
// getters y setters
|
// getters y setters
|
||||||
|
|
||||||
|
|
||||||
|
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> {
|
||||||
|
}
|
||||||
@@ -44,6 +44,10 @@ public class PujaService {
|
|||||||
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){
|
||||||
return pujaRepository.findByLote_IdAndLote_Remate_IdAndFechaAfter(loteId, remateId, fecha);
|
return pujaRepository.findByLote_IdAndLote_Remate_IdAndFechaAfter(loteId, remateId, fecha);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.example.fercoganbackend.service;
|
package com.example.fercoganbackend.service;
|
||||||
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,20 +14,25 @@ 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) {
|
||||||
|
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
|
||||||
return repo.save(u);
|
return repo.save(u);
|
||||||
}
|
}
|
||||||
@@ -82,6 +89,12 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user