blob: d53db64f7db56ca8c6c0c97772e6f3918f8eff45 [file] [log] [blame]
wuchimedese5722e32025-04-13 17:38:50 +08001package com.example.g8backend.controller;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08002import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
夜雨声烦368e3562025-04-24 01:49:46 +08003import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
wuchimedes5a842b22025-04-21 22:01:39 +08004import com.example.g8backend.dto.PostCreateDTO;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08005import com.example.g8backend.entity.PostView;
6import com.example.g8backend.mapper.PostViewMapper;
wuchimedese5722e32025-04-13 17:38:50 +08007import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.http.ResponseEntity;
9import org.springframework.security.core.Authentication;
10import org.springframework.security.core.context.SecurityContextHolder;
11import org.springframework.web.bind.annotation.*;
12import com.example.g8backend.entity.Post;
13import com.example.g8backend.service.IPostService;
wuchimedese5722e32025-04-13 17:38:50 +080014import java.util.List;
wuchimedese5722e32025-04-13 17:38:50 +080015@RestController
16@RequestMapping("/post")
17public class PostController {
18 @Autowired
19 private IPostService postService;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080020 @Autowired // ✅ 新增注入
21 private PostViewMapper postViewMapper;
wuchimedese5722e32025-04-13 17:38:50 +080022 @PostMapping("")
wuchimedes5a842b22025-04-21 22:01:39 +080023 public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) {
wuchimedese5722e32025-04-13 17:38:50 +080024 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
25 long userId = (long) authentication.getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080026 Post post = postCreateDTO.getPost();
27 Long[] tagIds = postCreateDTO.getTagIds();
wuchimedese5722e32025-04-13 17:38:50 +080028 post.setUserId(userId);
wuchimedes5a842b22025-04-21 22:01:39 +080029 if (tagIds.length > 0){
30 postService.createPost(post, tagIds);
31 } else {
32 postService.createPost(post);
33 }
wuchimedese5722e32025-04-13 17:38:50 +080034 return ResponseEntity.ok().build();
35 }
wuchimedese5722e32025-04-13 17:38:50 +080036 @GetMapping("/{postId}")
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080037 public Post getPost(@PathVariable Long postId) {
38 // 获取当前用户ID
39 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
40 long userId = (long) authentication.getPrincipal();
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080041 // 记录浏览行为
42 postService.recordViewHistory(userId, postId);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080043 // 返回帖子详情
wuchimedese5722e32025-04-13 17:38:50 +080044 return postService.getById(postId);
45 }
wuchimedese5722e32025-04-13 17:38:50 +080046 @DeleteMapping("/{postId}")
47 public ResponseEntity<?> deletePost(@PathVariable("postId") Long postId) {
48 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
49 long userId = (long) authentication.getPrincipal();
50 Post post = postService.getById(postId);
51 if (post == null) {
52 return ResponseEntity.status(500).body("Post not found.");
53 }
54 if (post.getUserId()!= userId) {
55 return ResponseEntity.status(403).body("You are not authorized to delete this post.");
56 }
57 postService.removeById(postId);
58 return ResponseEntity.ok().body("Post deleted successfully.");
59 }
wuchimedese5722e32025-04-13 17:38:50 +080060 @GetMapping("/getAll")
61 public List<Post> getAllPosts() {
62 return postService.list();
63 }
wuchimedese5722e32025-04-13 17:38:50 +080064 @GetMapping("/getByUserId/{userId}")
65 public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
66 return postService.getPostsByUserId(userId);
67 }
223010711f457dc2025-04-15 17:35:55 +080068 @PutMapping("/{postId}")
69 public ResponseEntity<?> updatePost(@PathVariable("postId") Long postId, @RequestBody Post post) {
70 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
71 long userId = (long) authentication.getPrincipal();
72 Post existingPost = postService.getById(postId);
夜雨声烦f77d8132025-04-24 19:31:18 +080073
223010711f457dc2025-04-15 17:35:55 +080074 if (existingPost == null) {
75 return ResponseEntity.status(500).body("Post not found.");
76 }
77 if (existingPost.getUserId() != userId) {
78 return ResponseEntity.status(403).body("You are not authorized to update this post.");
79 }
夜雨声烦f77d8132025-04-24 19:31:18 +080080
223010711f457dc2025-04-15 17:35:55 +080081 post.setPostId(postId);
82 post.setUserId(userId);
83 postService.updateById(post);
84 return ResponseEntity.ok().body("Post updated successfully.");
85 }
223010711f457dc2025-04-15 17:35:55 +080086 @GetMapping("/type/{postType}")
87 public ResponseEntity<?> getPostsByType(@PathVariable String postType) {
88 List<Post> posts = postService.getPostsByType(postType);
89 return ResponseEntity.ok().body(posts);
90 }
223010711f457dc2025-04-15 17:35:55 +080091 @PostMapping("/{postId}/like")
92 public ResponseEntity<?> likePost(@PathVariable Long postId) {
93 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
94 long userId = (long) authentication.getPrincipal();
95 postService.likePost(userId, postId);
96 return ResponseEntity.ok().body("Post liked successfully.");
97 }
223010711f457dc2025-04-15 17:35:55 +080098 @DeleteMapping("/{postId}/like")
99 public ResponseEntity<?> unlikePost(@PathVariable Long postId) {
100 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
101 long userId = (long) authentication.getPrincipal();
102 postService.unlikePost(userId, postId);
103 return ResponseEntity.ok().body("Post unliked successfully.");
104 }
223010711f457dc2025-04-15 17:35:55 +0800105 @GetMapping("/{postId}/likes")
106 public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) {
107 Long likeCount = postService.getPostLikeCount(postId);
108 return ResponseEntity.ok().body(likeCount);
109 }
夜雨声烦4527a722025-04-23 17:04:25 +0800110 // 搜索帖子
111 @GetMapping("/search")
112 public List<Post> searchPosts(
113 @RequestParam(required = false) String keyword,
114 @RequestParam(required = false) List<Long> tags, // 修改为接收多个标签
115 @RequestParam(required = false) String author) {
夜雨声烦4527a722025-04-23 17:04:25 +0800116 return postService.searchPosts(keyword, tags, author);
117 }
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800118 @GetMapping("/history")
119 public ResponseEntity<List<PostView>> getViewHistory() {
120 // 获取当前用户ID
121 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
122 long userId = (long) authentication.getPrincipal();
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800123 // 查询历史记录(按时间倒序)
124 List<PostView> history = postViewMapper.selectList(
125 new QueryWrapper<PostView>()
126 .eq("user_id", userId)
127 .orderByDesc("view_time")
128 );
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800129 return ResponseEntity.ok(history);
130 }
夜雨声烦368e3562025-04-24 01:49:46 +0800131 @GetMapping("/recommended")
132 public ResponseEntity<Page<Post>> getRecommendedPosts(
133 @RequestParam(defaultValue = "1") int page,
134 @RequestParam(defaultValue = "10") int size) {
夜雨声烦368e3562025-04-24 01:49:46 +0800135 // 获取当前用户ID
136 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
137 long userId = (long) authentication.getPrincipal();
夜雨声烦368e3562025-04-24 01:49:46 +0800138 // 调用 Service 层方法
139 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
夜雨声烦368e3562025-04-24 01:49:46 +0800140 return ResponseEntity.ok(pageResult);
141 }
夜雨声烦f77d8132025-04-24 19:31:18 +0800142 // PostController.java - 新增标签推荐接口
143 @GetMapping("/recommended-by-tags")
144 public ResponseEntity<Page<Post>> getRecommendedByTags(
145 @RequestParam(defaultValue = "1") int page,
146 @RequestParam(defaultValue = "10") int size) {
147 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
148 long userId = (long) authentication.getPrincipal();
149 // 调用标签推荐方法
150 Page<Post> result = postService.getRecommendedByTags(page, size, userId);
151 return ResponseEntity.ok(result);
152 }
153}