wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 3 | import com.example.g8backend.dto.ApiResponse; |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 4 | import com.example.g8backend.entity.Message; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 5 | import com.example.g8backend.entity.User; |
| 6 | import com.example.g8backend.service.IUserService; |
| 7 | import org.springframework.beans.factory.annotation.Autowired; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 8 | import org.springframework.security.core.Authentication; |
| 9 | import org.springframework.security.core.context.SecurityContextHolder; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 10 | import org.springframework.web.bind.annotation.*; |
| 11 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 12 | import java.util.List; |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 13 | import java.util.Map; |
| 14 | |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 15 | @RestController |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 16 | @RequestMapping("/user") |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 17 | public class UserController { |
| 18 | |
| 19 | @Autowired |
| 20 | private IUserService userService; |
| 21 | |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 22 | // 获取已登录的用户信息 |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 23 | @GetMapping |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 24 | public ApiResponse<User> getUserInfo(){ |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 25 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 26 | long userId = (long) authentication.getPrincipal(); |
| 27 | User user = userService.getById(userId); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 28 | user.setPassword(null); // 不返回密码 |
| 29 | return ApiResponse.success(user); |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 30 | } |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 31 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 32 | // ==================== 关注功能 ==================== |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 33 | |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 34 | @PostMapping("/follow/{userId}") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 35 | public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 36 | Long currentUserId = getCurrentUserId(); |
| 37 | boolean success = userService.followUser(currentUserId, userId); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 38 | return ApiResponse.success(Map.of("success", success)); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 39 | } |
| 40 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 41 | @DeleteMapping("/follow/{userId}") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 42 | public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 43 | Long currentUserId = getCurrentUserId(); |
| 44 | boolean success = userService.unfollowUser(currentUserId, userId); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 45 | return ApiResponse.success(Map.of("success", success)); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @GetMapping("/followings") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 49 | public ApiResponse<List<User>> getFollowings() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 50 | Long currentUserId = getCurrentUserId(); |
| 51 | List<User> followings = userService.getFollowings(currentUserId); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 52 | return ApiResponse.success(followings); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @GetMapping("/followers") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 56 | public ApiResponse<List<User>> getFollowers() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 57 | Long currentUserId = getCurrentUserId(); |
| 58 | List<User> followers = userService.getFollowers(currentUserId); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 59 | return ApiResponse.success(followers); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // ==================== 私信功能 ==================== |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 63 | |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 64 | @PostMapping("/message/{receiverId}") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 65 | public ApiResponse<Map<String, Long>> sendMessage( |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 66 | @PathVariable Long receiverId, |
| 67 | @RequestBody String content |
| 68 | ) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 69 | Long senderId = getCurrentUserId(); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 70 | Long messageId = userService.sendMessage(senderId, receiverId, content); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 71 | return ApiResponse.success(Map.of("messageId", messageId)); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 72 | } |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 73 | |
| 74 | @GetMapping("/messages/{otherUserId}") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 75 | public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 76 | Long currentUserId = getCurrentUserId(); |
| 77 | List<Message> messages = userService.getMessages(currentUserId, otherUserId); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 78 | return ApiResponse.success(messages); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @GetMapping("/messages/history") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 82 | public ApiResponse<List<Message>> getMessageHistory() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 83 | Long currentUserId = getCurrentUserId(); |
| 84 | List<Message> messages = userService.getMessageHistory(currentUserId); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame^] | 85 | return ApiResponse.success(messages); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // ==================== 工具方法 ==================== |
| 89 | private Long getCurrentUserId() { |
| 90 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 91 | return (Long) authentication.getPrincipal(); |
| 92 | } |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 93 | } |