diff --git a/src/main/java/SuMoC/backend/user/controller/FriendshipController.java b/src/main/java/SuMoC/backend/user/controller/FriendshipController.java index 753df03..4eb4f90 100644 --- a/src/main/java/SuMoC/backend/user/controller/FriendshipController.java +++ b/src/main/java/SuMoC/backend/user/controller/FriendshipController.java @@ -6,13 +6,10 @@ import SuMoC.backend.user.models.User; import SuMoC.backend.user.repository.UserRepository; import SuMoC.backend.user.service.CurrentUserService; import SuMoC.backend.user.service.FriendshipService; -import SuMoC.backend.user.service.PrivacyService; import io.swagger.v3.oas.annotations.Operation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.CacheControl; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -23,18 +20,13 @@ import java.util.concurrent.TimeUnit; public class FriendshipController { private final UserRepository userRepository; private final FriendshipService friendshipService; - private final CurrentUserService currentUser; + private final CurrentUserService currentUserService; + @Autowired - public FriendshipController(UserRepository userRepository, FriendshipService friendshipService, CurrentUserService currentUser) { + public FriendshipController(UserRepository userRepository, FriendshipService friendshipService, CurrentUserService currentUserService) { this.userRepository = userRepository; this.friendshipService = friendshipService; - this.currentUser = currentUser; - } - - // 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(); + this.currentUserService = currentUserService; } @Operation(summary = "Return all friendships of the logged in user.", @@ -51,7 +43,7 @@ public class FriendshipController { """) @GetMapping("/all") public ResponseEntity> getFriendships() { - User user = currentUser.get(); + User user = currentUserService.get(); List friendships = friendshipService.getFriendships(user.getId()); return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) @@ -61,7 +53,7 @@ public class FriendshipController { @Operation(summary = "Return all accepted friends of the logged in user.", description = "Returns all friends with accepted status.") @GetMapping("/accepted") public ResponseEntity> getAcceptedFriendships() { - User user = currentUser.get(); + User user = currentUserService.get(); List friendships = friendshipService.getAcceptedFriendships(user.getId()); return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) @@ -71,7 +63,7 @@ public class FriendshipController { @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 requestFriendship(@PathVariable Long friendId) { - User user = getAuthenticatedUser(); + User user = currentUserService.get(); checkSelfAction(user.getId(), friendId); friendshipService.requestFriendship(user, friendId); return ResponseEntity.ok().build(); @@ -80,7 +72,7 @@ public class FriendshipController { @Operation(summary = "Request a friendship with another user by friend code.", description = "Sends a friendship request to another user identified by their friend code from the logged in user.") @PostMapping("/request/by-friendcode/{friendCode}") public ResponseEntity requestFriendshipByFriendcode(@PathVariable String friendCode) { - User user = getAuthenticatedUser(); + User user = currentUserService.get(); User requestedFriend = userRepository.findByFriendCode(friendCode).orElseThrow(); checkSelfAction(user.getId(), requestedFriend.getId()); friendshipService.requestFriendship(user, requestedFriend.getId()); @@ -90,7 +82,7 @@ public class FriendshipController { @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 acceptFriendship(@PathVariable Long friendId) { - User user = getAuthenticatedUser(); + User user = currentUserService.get(); friendshipService.acceptFriendship(user, friendId); return ResponseEntity.ok().build(); } @@ -98,7 +90,7 @@ public class FriendshipController { @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 rejectFriendship(@PathVariable Long friendId) { - User user = getAuthenticatedUser(); + User user = currentUserService.get(); friendshipService.rejectFriendship(user, friendId); return ResponseEntity.ok().build(); } @@ -106,7 +98,7 @@ public class FriendshipController { @Operation(summary = "Unfriend a user.", description = "Removes an accepted friendship between the logged in user and user with friendId.") @DeleteMapping("/unfriend/{friendId}") public ResponseEntity unfriend(@PathVariable Long friendId) { - User user = getAuthenticatedUser(); + User user = currentUserService.get(); friendshipService.unfriend(user, friendId); return ResponseEntity.ok().build(); } @@ -114,7 +106,7 @@ public class FriendshipController { @Operation(summary = "Block a user.", description = "Blocks the user with friendId for the logged in user.") @PostMapping("/block/{friendId}") public ResponseEntity blockUser(@PathVariable Long friendId) { - User user = getAuthenticatedUser(); + User user = currentUserService.get(); checkSelfAction(user.getId(), friendId); friendshipService.blockUser(user, friendId); return ResponseEntity.ok().build(); @@ -123,7 +115,7 @@ public class FriendshipController { @Operation(summary = "Unblock a user.", description = "Unblocks the user with friendId for the logged in user.") @PostMapping("/unblock/{friendId}") public ResponseEntity unblockUser(@PathVariable Long friendId) { - User user = getAuthenticatedUser(); + User user = currentUserService.get(); friendshipService.unblockUser(user, friendId); return ResponseEntity.ok().build(); } @@ -131,7 +123,7 @@ public class FriendshipController { @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> blockedUsers() { - User user = currentUser.get(); + User user = currentUserService.get(); return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) .body(friendshipService.getBlockedUsers(user.getId()));