implemented friendship system

This commit is contained in:
2025-12-16 17:47:13 +01:00
parent c05420099a
commit 38ce235522
19 changed files with 542 additions and 48 deletions
@@ -0,0 +1,108 @@
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;
}
@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<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();
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();
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);
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);
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);
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);
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);
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);
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();
return ResponseEntity.ok(friendshipService.getBlockedUsers(user.getId()));
}
}