blob: 04730254943618318fc48d4690c147f41eee1c07 [file] [log] [blame]
wuchimedesa1bf2782025-03-27 15:08:54 +08001package com.example.g8backend.controller;
2
夜雨声烦19976162025-04-22 01:29:11 +08003import com.example.g8backend.entity.Message;
wuchimedesa1bf2782025-03-27 15:08:54 +08004import com.example.g8backend.entity.User;
5import com.example.g8backend.service.IUserService;
6import org.springframework.beans.factory.annotation.Autowired;
wuchimedes223bfab2025-04-04 17:16:05 +08007import org.springframework.http.ResponseEntity;
8import 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
wuchimedes223bfab2025-04-04 17:16:05 +080024 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);
wuchimedesa1bf2782025-03-27 15:08:54 +080030 }
夜雨声烦19976162025-04-22 01:29:11 +080031 // ==================== 关注功能 ====================
夜雨声烦7e6eb382025-04-22 01:18:00 +080032 @PostMapping("/follow/{userId}")
33 public ResponseEntity<?> followUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080034 Long currentUserId = getCurrentUserId();
35 boolean success = userService.followUser(currentUserId, userId);
36 return ResponseEntity.ok(Map.of("success", success));
夜雨声烦7e6eb382025-04-22 01:18:00 +080037 }
38
夜雨声烦19976162025-04-22 01:29:11 +080039 @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 // ==================== 私信功能 ====================
夜雨声烦7e6eb382025-04-22 01:18:00 +080061 @PostMapping("/message/{receiverId}")
62 public ResponseEntity<?> sendMessage(
63 @PathVariable Long receiverId,
64 @RequestBody String content
65 ) {
夜雨声烦19976162025-04-22 01:29:11 +080066 Long senderId = getCurrentUserId();
夜雨声烦7e6eb382025-04-22 01:18:00 +080067 Long messageId = userService.sendMessage(senderId, receiverId, content);
68 return ResponseEntity.ok(Map.of("messageId", messageId));
69 }
夜雨声烦19976162025-04-22 01:29:11 +080070
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 }
wuchimedesa1bf2782025-03-27 15:08:54 +080090}