Skip to content
Snippets Groups Projects

user controller clear reponse message for APIs

Merged Massil Bouzar requested to merge cherry-pick-5068ad85 into main
1 file
+ 32
12
Compare changes
  • Side-by-side
  • Inline
@@ -24,8 +24,12 @@ public class UserController {
// Récupérer tous les utilisateurs
@GetMapping
public List<UserDTO> getAllUsers() {
return userService.getAllUsers();
public ResponseEntity<?> getAllUsers() {
List<UserDTO> users = userService.getAllUsers();
return ResponseEntity.ok(Map.of(
"message", "All users retrieved successfully.",
"users", users
));
}
// Ajouter un utilisateur
@@ -46,10 +50,21 @@ public class UserController {
// Récupérer un utilisateur par ID
@GetMapping("/{id}")
public UserDTO getUserById(@PathVariable Long id) {
return userService.getUserById(id);
public ResponseEntity<?> getUserById(@PathVariable Long id) {
try {
UserDTO user = userService.getUserById(id);
return ResponseEntity.ok(Map.of(
"message", "User retrieved successfully.",
"user", user
));
} catch (ResourceNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of(
"error", e.getMessage()
));
}
}
// Mettre à jour un utilisateur
@PutMapping("/{id}")
public ResponseEntity<?> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
try {
@@ -62,21 +77,26 @@ public class UserController {
return ResponseEntity.status(HttpStatus.CONFLICT).body(Map.of(
"error", e.getMessage()
));
} catch (RuntimeException e) {
} catch (ResourceNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of(
"error", e.getMessage()
));
}
}
// Supprimer un utilisateur
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
@PostMapping("/{userId}/quizzes")
public QuizDTO createQuiz(@PathVariable Long userId, @RequestBody QuizDTO quizDTO) {
return quizService.createQuiz(userId, quizDTO);
public ResponseEntity<?> deleteUser(@PathVariable Long id) {
try {
userService.deleteUser(id);
return ResponseEntity.ok(Map.of(
"message", "User successfully deleted."
));
} catch (ResourceNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of(
"error", e.getMessage()
));
}
}
// Activer ou désactiver un utilisateur
Loading