fix some api
Change-Id: I7179a115c9ed3e7d3473c1761df1bb4f2bf3711e
diff --git a/src/main/java/com/example/g8backend/controller/UserController.java b/src/main/java/com/example/g8backend/controller/UserController.java
index f9d0de4..a41e4d7 100644
--- a/src/main/java/com/example/g8backend/controller/UserController.java
+++ b/src/main/java/com/example/g8backend/controller/UserController.java
@@ -9,6 +9,7 @@
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.example.g8backend.service.ISigningService;
@@ -23,14 +24,42 @@
@Autowired
private IUserService userService;
- // 获取已登录的用户信息
@GetMapping
- public ApiResponse<User> getUserInfo(){
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
- long userId = (long) authentication.getPrincipal();
+ public ApiResponse<Map<String, Object>> getCurrentUserInfo() {
+ Long userId = getCurrentUserId();
+ return getUserInfo(userId);
+ }
+
+ @GetMapping("/{userId}")
+ public ApiResponse<Map<String, Object>> getOtherUserInfo(@PathVariable(required = false) Long userId){
+ return getUserInfo(userId);
+ }
+
+ @GetMapping("/{userId}/follow-status")
+ public ApiResponse<Map<String, Boolean>> getFollowStatus(@PathVariable Long userId) {
+ Long currentUserId = getCurrentUserId();
+ boolean isFollowing = userService.isFollowing(currentUserId, userId);
+ return ApiResponse.success(Map.of("isFollowing", isFollowing));
+ }
+
+ private ApiResponse<Map<String, Object>> getUserInfo(Long userId) {
User user = userService.getById(userId);
- user.setPassword(null); // 不返回密码
- return ApiResponse.success(user);
+ if (user == null) {
+ return ApiResponse.error(404, "用户不存在");
+ }
+ user.setPassword(null);
+ user.setPasskey(null);
+
+ int followingCount = userService.getFollowingsCount(userId);
+ int followersCount = userService.getFollowersCount(userId);
+
+ return ApiResponse.success(Map.of(
+ "userInfo", user,
+ "statistics", Map.of(
+ "followingCount", followingCount,
+ "followersCount", followersCount
+ )
+ ));
}
// ==================== 关注功能 ====================