friendship service / controller - move interaction with security context to controller

This commit is contained in:
2025-12-17 18:50:29 +01:00
parent 50b27adb94
commit bd26c89326
2 changed files with 27 additions and 34 deletions
@@ -26,6 +26,12 @@ public class FriendshipController {
this.friendshipService = friendshipService;
}
// TODO extend security context with user id to avoid unnecessary db query
private User getAuthenticatedUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return userRepository.findByEmail(authentication.getName()).get();
}
@Operation(summary = "Return all friendships of the logged in user.",
description = """
Returns all friendships of all statuses (pending, accepted, rejected, blocked) for the logged in user.
@@ -40,69 +46,69 @@ public class FriendshipController {
""")
@GetMapping("/all")
public ResponseEntity<List<FriendshipDTO>> getFriendships() {
// TODO extend security context with user id to avoid unnecessary db query
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user = userRepository.findByEmail(authentication.getName()).get();
User user = getAuthenticatedUser();
return ResponseEntity.ok(friendshipService.getFriendships(user.getId()));
}
@Operation(summary = "Return all accepted friends of the logged in user.", description = "Returns all friends with accepted status.")
@GetMapping("/accepted")
public ResponseEntity<List<FriendshipDTO>> getAcceptedFriendships() {
// TODO extend security context with user id to avoid unnecessary db query
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user = userRepository.findByEmail(authentication.getName()).get();
User user = getAuthenticatedUser();
return ResponseEntity.ok(friendshipService.getAcceptedFriendships(user.getId()));
}
@Operation(summary = "Request a friendship with another user.", description = "Sends a friendship request to another user from the logged in user.")
@PostMapping("/request/{friendId}")
public ResponseEntity<Void> requestFriendship(@PathVariable Long friendId) {
friendshipService.requestFriendship(friendId);
User user = getAuthenticatedUser();
friendshipService.requestFriendship(user, friendId);
return ResponseEntity.ok().build();
}
@Operation(summary = "Accept a friendship request.", description = "Accepts a pending friendship request for the logged in user from user with friendId.")
@PostMapping("/accept/{friendId}")
public ResponseEntity<Void> acceptFriendship(@PathVariable Long friendId) {
friendshipService.acceptFriendship(friendId);
User user = getAuthenticatedUser();
friendshipService.acceptFriendship(user, friendId);
return ResponseEntity.ok().build();
}
@Operation(summary = "Reject a friendship request.", description = "Rejects a pending friendship request for the logged in user from user with friendId.")
@PostMapping("/reject/{friendId}")
public ResponseEntity<Void> rejectFriendship(@PathVariable Long friendId) {
friendshipService.rejectFriendship(friendId);
User user = getAuthenticatedUser();
friendshipService.rejectFriendship(user, friendId);
return ResponseEntity.ok().build();
}
@Operation(summary = "Unfriend a user.", description = "Removes an accepted friendship between the logged in user and user with friendId.")
@DeleteMapping("/unfriend/{friendId}")
public ResponseEntity<Void> unfriend(@PathVariable Long friendId) {
friendshipService.unfriend(friendId);
User user = getAuthenticatedUser();
friendshipService.unfriend(user, friendId);
return ResponseEntity.ok().build();
}
@Operation(summary = "Block a user.", description = "Blocks the user with friendId for the logged in user.")
@PostMapping("/block/{friendId}")
public ResponseEntity<Void> blockUser(@PathVariable Long friendId) {
friendshipService.blockUser(friendId);
User user = getAuthenticatedUser();
friendshipService.blockUser(user, friendId);
return ResponseEntity.ok().build();
}
@Operation(summary = "Unblock a user.", description = "Unblocks the user with friendId for the logged in user.")
@PostMapping("/unblock/{friendId}")
public ResponseEntity<Void> unblockUser(@PathVariable Long friendId) {
friendshipService.unblockUser(friendId);
User user = getAuthenticatedUser();
friendshipService.unblockUser(user, friendId);
return ResponseEntity.ok().build();
}
@Operation(summary = "Get all users the logged in user has blocked.", description = "Returns a list of all users that have been blocked by the logged in user.")
@GetMapping("/blocked")
public ResponseEntity<List<MinimalUserDTO>> blockedUsers() {
// TODO extend security context with user id to avoid unnecessary db query
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user = userRepository.findByEmail(authentication.getName()).get();
User user = getAuthenticatedUser();
return ResponseEntity.ok(friendshipService.getBlockedUsers(user.getId()));
}
}