blob: 121bc1a4b76b4bc943aea65f24090940e885d466 [file] [log] [blame]
wuchimedesa1bf2782025-03-27 15:08:54 +08001package com.example.g8backend.controller;
2
夜雨声烦c2e30e52025-05-13 18:49:03 +08003import com.example.g8backend.dto.ApiResponse;
夜雨声烦19976162025-04-22 01:29:11 +08004import com.example.g8backend.entity.Message;
wuchimedesa1bf2782025-03-27 15:08:54 +08005import com.example.g8backend.entity.User;
6import com.example.g8backend.service.IUserService;
7import org.springframework.beans.factory.annotation.Autowired;
wuchimedes223bfab2025-04-04 17:16:05 +08008import org.springframework.security.core.Authentication;
9import org.springframework.security.core.context.SecurityContextHolder;
wuchimedesa1bf2782025-03-27 15:08:54 +080010import org.springframework.web.bind.annotation.*;
11
夜雨声烦19976162025-04-22 01:29:11 +080012import java.util.List;
夜雨声烦7e6eb382025-04-22 01:18:00 +080013import java.util.Map;
14
wuchimedesa1bf2782025-03-27 15:08:54 +080015@RestController
wuchimedes223bfab2025-04-04 17:16:05 +080016@RequestMapping("/user")
wuchimedesa1bf2782025-03-27 15:08:54 +080017public class UserController {
18
19 @Autowired
20 private IUserService userService;
21
wuchimedes223bfab2025-04-04 17:16:05 +080022 // 获取已登录的用户信息
wuchimedesa1bf2782025-03-27 15:08:54 +080023 @GetMapping
夜雨声烦c2e30e52025-05-13 18:49:03 +080024 public ApiResponse<User> getUserInfo(){
wuchimedes223bfab2025-04-04 17:16:05 +080025 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
26 long userId = (long) authentication.getPrincipal();
27 User user = userService.getById(userId);
夜雨声烦c2e30e52025-05-13 18:49:03 +080028 user.setPassword(null); // 不返回密码
29 return ApiResponse.success(user);
wuchimedesa1bf2782025-03-27 15:08:54 +080030 }
夜雨声烦c2e30e52025-05-13 18:49:03 +080031
夜雨声烦19976162025-04-22 01:29:11 +080032 // ==================== 关注功能 ====================
夜雨声烦c2e30e52025-05-13 18:49:03 +080033
夜雨声烦7e6eb382025-04-22 01:18:00 +080034 @PostMapping("/follow/{userId}")
夜雨声烦c2e30e52025-05-13 18:49:03 +080035 public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080036 Long currentUserId = getCurrentUserId();
37 boolean success = userService.followUser(currentUserId, userId);
夜雨声烦c2e30e52025-05-13 18:49:03 +080038 return ApiResponse.success(Map.of("success", success));
夜雨声烦7e6eb382025-04-22 01:18:00 +080039 }
40
夜雨声烦19976162025-04-22 01:29:11 +080041 @DeleteMapping("/follow/{userId}")
夜雨声烦c2e30e52025-05-13 18:49:03 +080042 public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080043 Long currentUserId = getCurrentUserId();
44 boolean success = userService.unfollowUser(currentUserId, userId);
夜雨声烦c2e30e52025-05-13 18:49:03 +080045 return ApiResponse.success(Map.of("success", success));
夜雨声烦19976162025-04-22 01:29:11 +080046 }
47
48 @GetMapping("/followings")
夜雨声烦c2e30e52025-05-13 18:49:03 +080049 public ApiResponse<List<User>> getFollowings() {
夜雨声烦19976162025-04-22 01:29:11 +080050 Long currentUserId = getCurrentUserId();
51 List<User> followings = userService.getFollowings(currentUserId);
夜雨声烦c2e30e52025-05-13 18:49:03 +080052 return ApiResponse.success(followings);
夜雨声烦19976162025-04-22 01:29:11 +080053 }
54
55 @GetMapping("/followers")
夜雨声烦c2e30e52025-05-13 18:49:03 +080056 public ApiResponse<List<User>> getFollowers() {
夜雨声烦19976162025-04-22 01:29:11 +080057 Long currentUserId = getCurrentUserId();
58 List<User> followers = userService.getFollowers(currentUserId);
夜雨声烦c2e30e52025-05-13 18:49:03 +080059 return ApiResponse.success(followers);
夜雨声烦19976162025-04-22 01:29:11 +080060 }
61
62 // ==================== 私信功能 ====================
夜雨声烦c2e30e52025-05-13 18:49:03 +080063
夜雨声烦7e6eb382025-04-22 01:18:00 +080064 @PostMapping("/message/{receiverId}")
夜雨声烦c2e30e52025-05-13 18:49:03 +080065 public ApiResponse<Map<String, Long>> sendMessage(
夜雨声烦7e6eb382025-04-22 01:18:00 +080066 @PathVariable Long receiverId,
67 @RequestBody String content
68 ) {
夜雨声烦19976162025-04-22 01:29:11 +080069 Long senderId = getCurrentUserId();
夜雨声烦7e6eb382025-04-22 01:18:00 +080070 Long messageId = userService.sendMessage(senderId, receiverId, content);
夜雨声烦c2e30e52025-05-13 18:49:03 +080071 return ApiResponse.success(Map.of("messageId", messageId));
夜雨声烦7e6eb382025-04-22 01:18:00 +080072 }
夜雨声烦19976162025-04-22 01:29:11 +080073
74 @GetMapping("/messages/{otherUserId}")
夜雨声烦c2e30e52025-05-13 18:49:03 +080075 public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) {
夜雨声烦19976162025-04-22 01:29:11 +080076 Long currentUserId = getCurrentUserId();
77 List<Message> messages = userService.getMessages(currentUserId, otherUserId);
夜雨声烦c2e30e52025-05-13 18:49:03 +080078 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +080079 }
80
81 @GetMapping("/messages/history")
夜雨声烦c2e30e52025-05-13 18:49:03 +080082 public ApiResponse<List<Message>> getMessageHistory() {
夜雨声烦19976162025-04-22 01:29:11 +080083 Long currentUserId = getCurrentUserId();
84 List<Message> messages = userService.getMessageHistory(currentUserId);
夜雨声烦c2e30e52025-05-13 18:49:03 +080085 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +080086 }
87
88 // ==================== 工具方法 ====================
89 private Long getCurrentUserId() {
90 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
91 return (Long) authentication.getPrincipal();
92 }
wuchimedesa1bf2782025-03-27 15:08:54 +080093}