Skip to content

Commit ecfce63

Browse files
committed
Find user by id
1 parent c7c1460 commit ecfce63

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/main/java/dev/mikablondo/hibernate_reactive_test/controller/UserController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,17 @@ public Uni<ResponseEntity<Void>> deleteUser(@PathVariable String id) {
4646
return Uni.createFrom().item(ResponseEntity.status(HttpStatus.BAD_REQUEST).build());
4747
}
4848
}
49+
50+
@GetMapping("/{id}")
51+
public Uni<ResponseEntity<User>> getUserById(@PathVariable String id) {
52+
try {
53+
UUID uuid = UUID.fromString(id);
54+
return userService.getUserById(uuid)
55+
.map(user -> user != null
56+
? ResponseEntity.ok(user)
57+
: ResponseEntity.status(HttpStatus.NOT_FOUND).build());
58+
} catch (IllegalArgumentException e) {
59+
return Uni.createFrom().item(ResponseEntity.status(HttpStatus.BAD_REQUEST).build());
60+
}
61+
}
4962
}

src/main/java/dev/mikablondo/hibernate_reactive_test/services/UserService.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,27 @@ public Uni<Void> createUser(User user) {
6262
public Uni<Boolean> deleteUser(UUID id) {
6363
return userRepository.deleteUser(id);
6464
}
65+
66+
/**
67+
* This method retrieves a user by its UUID from the database.
68+
*
69+
* @param uuid the UUID of the user to be retrieved
70+
* @return a Uni<User> containing the user DTO if found, or null if not found
71+
*/
72+
public Uni<User> getUserById(UUID uuid) {
73+
return userRepository.findById(uuid)
74+
.onItem().transform(userEntity -> {
75+
if (userEntity != null) {
76+
return User.builder()
77+
.id(userEntity.getId())
78+
.nom(userEntity.getNom())
79+
.prenom(userEntity.getPrenom())
80+
.age(userEntity.getAge())
81+
.metier(userEntity.getMetier())
82+
.build();
83+
} else {
84+
return null;
85+
}
86+
});
87+
}
6588
}

0 commit comments

Comments
 (0)