package SuMoC.backend.user.controller; import SuMoC.backend.user.dtos.FriendshipDTO; import SuMoC.backend.user.dtos.MinimalUserDTO; import SuMoC.backend.user.models.User; import SuMoC.backend.user.repository.UserRepository; import SuMoC.backend.user.service.FriendshipService; import io.swagger.v3.oas.annotations.Operation; import org.springframework.beans.factory.annotation.Autowired; 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; @RestController @RequestMapping("/friendship") public class FriendshipController { private final UserRepository userRepository; private final FriendshipService friendshipService; @Autowired public FriendshipController(UserRepository userRepository, FriendshipService friendshipService) { this.userRepository = userRepository; 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. A friendship between two users always consists of one or two entries in the database. One for each direction. The user is always the one to whom the status applies. - If A requests friendship from B, B -> A = PENDING, A -> B = REQUESTED - If A accepts friendship from B, B -> A = ACCEPTED, A -> B = ACCEPTED - If A rejects friendship from B, B -> A = REJECTED - If A blocks B, B -> A = BLOCKED Per friendship system logic the logged in user is to whom the statuses apply. Meaning a blocked status indicates that the logged in user has been blocked by the other user. """) @GetMapping("/all") public ResponseEntity> getFriendships() { 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> getAcceptedFriendships() { 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 requestFriendship(@PathVariable Long 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 acceptFriendship(@PathVariable Long 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 rejectFriendship(@PathVariable Long 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 unfriend(@PathVariable Long 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 blockUser(@PathVariable Long 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 unblockUser(@PathVariable Long 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> blockedUsers() { User user = getAuthenticatedUser(); return ResponseEntity.ok(friendshipService.getBlockedUsers(user.getId())); } }