wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 3 | import com.example.g8backend.entity.Message; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 4 | import com.example.g8backend.entity.User; |
| 5 | import com.example.g8backend.service.IUserService; |
| 6 | import org.springframework.beans.factory.annotation.Autowired; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 7 | import org.springframework.http.ResponseEntity; |
| 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 |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 24 | public ResponseEntity<?> getUserInfo(){ |
| 25 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 26 | long userId = (long) authentication.getPrincipal(); |
| 27 | User user = userService.getById(userId); |
| 28 | user.setPassword(null); |
| 29 | return ResponseEntity.ok(user); |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 30 | } |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 31 | // ==================== 关注功能 ==================== |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 32 | @PostMapping("/follow/{userId}") |
| 33 | public ResponseEntity<?> followUser(@PathVariable Long userId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 34 | Long currentUserId = getCurrentUserId(); |
| 35 | boolean success = userService.followUser(currentUserId, userId); |
| 36 | return ResponseEntity.ok(Map.of("success", success)); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 37 | } |
| 38 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 39 | @DeleteMapping("/follow/{userId}") |
| 40 | public ResponseEntity<?> unfollowUser(@PathVariable Long userId) { |
| 41 | Long currentUserId = getCurrentUserId(); |
| 42 | boolean success = userService.unfollowUser(currentUserId, userId); |
| 43 | return ResponseEntity.ok(Map.of("success", success)); |
| 44 | } |
| 45 | |
| 46 | @GetMapping("/followings") |
| 47 | public ResponseEntity<?> getFollowings() { |
| 48 | Long currentUserId = getCurrentUserId(); |
| 49 | List<User> followings = userService.getFollowings(currentUserId); |
| 50 | return ResponseEntity.ok(followings); |
| 51 | } |
| 52 | |
| 53 | @GetMapping("/followers") |
| 54 | public ResponseEntity<?> getFollowers() { |
| 55 | Long currentUserId = getCurrentUserId(); |
| 56 | List<User> followers = userService.getFollowers(currentUserId); |
| 57 | return ResponseEntity.ok(followers); |
| 58 | } |
| 59 | |
| 60 | // ==================== 私信功能 ==================== |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 61 | @PostMapping("/message/{receiverId}") |
| 62 | public ResponseEntity<?> sendMessage( |
| 63 | @PathVariable Long receiverId, |
| 64 | @RequestBody String content |
| 65 | ) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 66 | Long senderId = getCurrentUserId(); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 67 | Long messageId = userService.sendMessage(senderId, receiverId, content); |
| 68 | return ResponseEntity.ok(Map.of("messageId", messageId)); |
| 69 | } |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 70 | |
| 71 | @GetMapping("/messages/{otherUserId}") |
| 72 | public ResponseEntity<?> getMessages(@PathVariable Long otherUserId) { |
| 73 | Long currentUserId = getCurrentUserId(); |
| 74 | List<Message> messages = userService.getMessages(currentUserId, otherUserId); |
| 75 | return ResponseEntity.ok(messages); |
| 76 | } |
| 77 | |
| 78 | @GetMapping("/messages/history") |
| 79 | public ResponseEntity<?> getMessageHistory() { |
| 80 | Long currentUserId = getCurrentUserId(); |
| 81 | List<Message> messages = userService.getMessageHistory(currentUserId); |
| 82 | return ResponseEntity.ok(messages); |
| 83 | } |
| 84 | |
| 85 | // ==================== 工具方法 ==================== |
| 86 | private Long getCurrentUserId() { |
| 87 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 88 | return (Long) authentication.getPrincipal(); |
| 89 | } |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 90 | } |