controller_adjust
Change-Id: Ie136f68887cd547576239ad0ce0c2eaccde730b3
diff --git a/src/main/java/com/example/g8backend/controller/UserController.java b/src/main/java/com/example/g8backend/controller/UserController.java
index 0473025..121bc1a 100644
--- a/src/main/java/com/example/g8backend/controller/UserController.java
+++ b/src/main/java/com/example/g8backend/controller/UserController.java
@@ -1,10 +1,10 @@
package com.example.g8backend.controller;
+import com.example.g8backend.dto.ApiResponse;
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;
-import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
@@ -21,65 +21,68 @@
// 获取已登录的用户信息
@GetMapping
- public ResponseEntity<?> getUserInfo(){
+ public ApiResponse<User> getUserInfo(){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
long userId = (long) authentication.getPrincipal();
User user = userService.getById(userId);
- user.setPassword(null);
- return ResponseEntity.ok(user);
+ user.setPassword(null); // 不返回密码
+ return ApiResponse.success(user);
}
+
// ==================== 关注功能 ====================
+
@PostMapping("/follow/{userId}")
- public ResponseEntity<?> followUser(@PathVariable Long userId) {
+ public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) {
Long currentUserId = getCurrentUserId();
boolean success = userService.followUser(currentUserId, userId);
- return ResponseEntity.ok(Map.of("success", success));
+ return ApiResponse.success(Map.of("success", success));
}
@DeleteMapping("/follow/{userId}")
- public ResponseEntity<?> unfollowUser(@PathVariable Long userId) {
+ public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) {
Long currentUserId = getCurrentUserId();
boolean success = userService.unfollowUser(currentUserId, userId);
- return ResponseEntity.ok(Map.of("success", success));
+ return ApiResponse.success(Map.of("success", success));
}
@GetMapping("/followings")
- public ResponseEntity<?> getFollowings() {
+ public ApiResponse<List<User>> getFollowings() {
Long currentUserId = getCurrentUserId();
List<User> followings = userService.getFollowings(currentUserId);
- return ResponseEntity.ok(followings);
+ return ApiResponse.success(followings);
}
@GetMapping("/followers")
- public ResponseEntity<?> getFollowers() {
+ public ApiResponse<List<User>> getFollowers() {
Long currentUserId = getCurrentUserId();
List<User> followers = userService.getFollowers(currentUserId);
- return ResponseEntity.ok(followers);
+ return ApiResponse.success(followers);
}
// ==================== 私信功能 ====================
+
@PostMapping("/message/{receiverId}")
- public ResponseEntity<?> sendMessage(
+ public ApiResponse<Map<String, Long>> sendMessage(
@PathVariable Long receiverId,
@RequestBody String content
) {
Long senderId = getCurrentUserId();
Long messageId = userService.sendMessage(senderId, receiverId, content);
- return ResponseEntity.ok(Map.of("messageId", messageId));
+ return ApiResponse.success(Map.of("messageId", messageId));
}
@GetMapping("/messages/{otherUserId}")
- public ResponseEntity<?> getMessages(@PathVariable Long otherUserId) {
+ public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) {
Long currentUserId = getCurrentUserId();
List<Message> messages = userService.getMessages(currentUserId, otherUserId);
- return ResponseEntity.ok(messages);
+ return ApiResponse.success(messages);
}
@GetMapping("/messages/history")
- public ResponseEntity<?> getMessageHistory() {
+ public ApiResponse<List<Message>> getMessageHistory() {
Long currentUserId = getCurrentUserId();
List<Message> messages = userService.getMessageHistory(currentUserId);
- return ResponseEntity.ok(messages);
+ return ApiResponse.success(messages);
}
// ==================== 工具方法 ====================