diff --git a/ABP_Geocatcher/src/main/java/ABP_Geocatcher/controller/UsuarioController.java b/ABP_Geocatcher/src/main/java/ABP_Geocatcher/controller/UsuarioController.java index 6312eeffdc362e7b332b71cce9bdebab9ea26dce..6adc1add1e7fd3a32d989d7f5dc494d0f43dc5fc 100644 --- a/ABP_Geocatcher/src/main/java/ABP_Geocatcher/controller/UsuarioController.java +++ b/ABP_Geocatcher/src/main/java/ABP_Geocatcher/controller/UsuarioController.java @@ -1,6 +1,8 @@ package ABP_Geocatcher.controller; +import ABP_Geocatcher.model.Artefacto; import ABP_Geocatcher.model.Usuario; +import ABP_Geocatcher.service.ArtefactoService; import ABP_Geocatcher.service.UsuarioService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -16,8 +18,12 @@ import java.util.List; @RequestMapping("/usuario") public class UsuarioController { + @Autowired private final UsuarioService usuarioService; + @Autowired + private ArtefactoService artefactoService; + public UsuarioController(UsuarioService usuarioService) { this.usuarioService = usuarioService; } @@ -80,5 +86,16 @@ public class UsuarioController { public ResponseEntity<Usuario> addArtefactoToUsuario(@PathVariable int usuarioId, @PathVariable int artefactoId) { return ResponseEntity.ok(usuarioService.addArtefactoToUsuario(usuarioId, artefactoId)); } + + @PutMapping("/{usuarioId}/artefacte/{artefactoId}") + public ResponseEntity<Artefacto> assignUsuarioToArtefacto( + @PathVariable int usuarioId, + @PathVariable int artefactoId) { + Artefacto updatedArtefacto = artefactoService.assignUsuarioToArtefacto(usuarioId, artefactoId); + return ResponseEntity.ok(updatedArtefacto); + } + + + } diff --git a/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/ArtefactoService.java b/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/ArtefactoService.java index 545eb546133b9701f927b98210189dc7b2aecec6..91caa145d844fb0ea62079165f22098033b0e7e2 100644 --- a/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/ArtefactoService.java +++ b/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/ArtefactoService.java @@ -2,8 +2,10 @@ package ABP_Geocatcher.service; import ABP_Geocatcher.dao.ArtefactoRepository; import ABP_Geocatcher.dao.UbicacionRepository; +import ABP_Geocatcher.dao.UsuarioRepository; import ABP_Geocatcher.model.Artefacto; import ABP_Geocatcher.model.Ubicacion; +import ABP_Geocatcher.model.Usuario; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -24,6 +26,9 @@ public class ArtefactoService { @Autowired private UbicacionRepository ubicacionRepository; + @Autowired + private UsuarioRepository usuarioRepository; + public ArtefactoService(ArtefactoRepository artefactoRepository) { this.artefactoRepository = artefactoRepository; } @@ -57,4 +62,23 @@ public class ArtefactoService { // Guardar el artefacto actualizado return artefactoRepository.save(artefacto); } + + public Artefacto assignUsuarioToArtefacto(int usuarioId, int artefactoId) { + // Buscar el usuario + Usuario usuario = usuarioRepository.findById(usuarioId) + .orElseThrow(() -> new RuntimeException("Usuario no encontrado")); + + // Buscar el artefacto + Artefacto artefacto = artefactoRepository.findById(artefactoId) + .orElseThrow(() -> new RuntimeException("Artefacto no encontrado")); + + // Asignar el usuario al artefacto + artefacto.getUsuarios().add(usuario); + + // Guardar y devolver el artefacto actualizado + return artefactoRepository.save(artefacto); + } + + + } diff --git a/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/UsuarioService.java b/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/UsuarioService.java index 4c3bff00bc0262e4e29688246c9edb4f7c33e922..93c26f01720dc3d4716253bb25cafc7d9c70cd14 100644 --- a/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/UsuarioService.java +++ b/ABP_Geocatcher/src/main/java/ABP_Geocatcher/service/UsuarioService.java @@ -54,5 +54,6 @@ public class UsuarioService { return usuarioRepository.save(usuario); } + } diff --git a/Files/Propierties.txt b/Files/Propierties.txt index b1ccc5c9622ec77a9c5c34eec5e042a47bf55e4c..d8fbdb7de8a0c201e6564ec92b3ba445b2641865 100644 --- a/Files/Propierties.txt +++ b/Files/Propierties.txt @@ -7,3 +7,4 @@ spring.datasource.url=jdbc:mysql://localhost:3306/ABP_Geocatcher spring.datasource.username=user spring.datasource.password=jupiter +