controller_adjust
Change-Id: I33aa2968ffa37f18f1010c2a2385fea5954c887d
diff --git a/src/main/java/com/example/g8backend/controller/UserController.java b/src/main/java/com/example/g8backend/controller/UserController.java
index 3b9ec22..0473025 100644
--- a/src/main/java/com/example/g8backend/controller/UserController.java
+++ b/src/main/java/com/example/g8backend/controller/UserController.java
@@ -1,5 +1,6 @@
package com.example.g8backend.controller;
+import com.example.g8backend.entity.Message;
import com.example.g8backend.entity.User;
import com.example.g8backend.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -8,6 +9,7 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
+import java.util.List;
import java.util.Map;
@RestController
@@ -26,21 +28,63 @@
user.setPassword(null);
return ResponseEntity.ok(user);
}
+ // ==================== 关注功能 ====================
@PostMapping("/follow/{userId}")
public ResponseEntity<?> followUser(@PathVariable Long userId) {
- Authentication auth = SecurityContextHolder.getContext().getAuthentication();
- Long followerId = (Long) auth.getPrincipal(); // 确保Security返回Long
- return ResponseEntity.ok(userService.followUser(followerId, userId));
+ Long currentUserId = getCurrentUserId();
+ boolean success = userService.followUser(currentUserId, userId);
+ return ResponseEntity.ok(Map.of("success", success));
}
+ @DeleteMapping("/follow/{userId}")
+ public ResponseEntity<?> unfollowUser(@PathVariable Long userId) {
+ Long currentUserId = getCurrentUserId();
+ boolean success = userService.unfollowUser(currentUserId, userId);
+ return ResponseEntity.ok(Map.of("success", success));
+ }
+
+ @GetMapping("/followings")
+ public ResponseEntity<?> getFollowings() {
+ Long currentUserId = getCurrentUserId();
+ List<User> followings = userService.getFollowings(currentUserId);
+ return ResponseEntity.ok(followings);
+ }
+
+ @GetMapping("/followers")
+ public ResponseEntity<?> getFollowers() {
+ Long currentUserId = getCurrentUserId();
+ List<User> followers = userService.getFollowers(currentUserId);
+ return ResponseEntity.ok(followers);
+ }
+
+ // ==================== 私信功能 ====================
@PostMapping("/message/{receiverId}")
public ResponseEntity<?> sendMessage(
@PathVariable Long receiverId,
@RequestBody String content
) {
- Authentication auth = SecurityContextHolder.getContext().getAuthentication();
- Long senderId = (Long) auth.getPrincipal();
+ Long senderId = getCurrentUserId();
Long messageId = userService.sendMessage(senderId, receiverId, content);
return ResponseEntity.ok(Map.of("messageId", messageId));
}
+
+ @GetMapping("/messages/{otherUserId}")
+ public ResponseEntity<?> getMessages(@PathVariable Long otherUserId) {
+ Long currentUserId = getCurrentUserId();
+ List<Message> messages = userService.getMessages(currentUserId, otherUserId);
+ return ResponseEntity.ok(messages);
+ }
+
+ @GetMapping("/messages/history")
+ public ResponseEntity<?> getMessageHistory() {
+ Long currentUserId = getCurrentUserId();
+ List<Message> messages = userService.getMessageHistory(currentUserId);
+ return ResponseEntity.ok(messages);
+ }
+
+ // ==================== 工具方法 ====================
+ private Long getCurrentUserId() {
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+ return (Long) authentication.getPrincipal();
+ }
}