blob: 68e0f95e1a0a90e8cd2ef434399bc5393cd42fce [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;
夜雨声烦ef46ac52025-04-24 20:56:47 +08005import com.example.g8backend.dto.PostHistoryDTO;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08006import com.example.g8backend.entity.PostView;
7import com.example.g8backend.mapper.PostViewMapper;
wuchimedese5722e32025-04-13 17:38:50 +08008import org.springframework.beans.factory.annotation.Autowired;
9import org.springframework.http.ResponseEntity;
10import org.springframework.security.core.Authentication;
11import org.springframework.security.core.context.SecurityContextHolder;
12import org.springframework.web.bind.annotation.*;
13import com.example.g8backend.entity.Post;
14import com.example.g8backend.service.IPostService;
wuchimedese5722e32025-04-13 17:38:50 +080015import java.util.List;
wuchimedese5722e32025-04-13 17:38:50 +080016@RestController
17@RequestMapping("/post")
18public class PostController {
19 @Autowired
20 private IPostService postService;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080021 @Autowired // ✅ 新增注入
22 private PostViewMapper postViewMapper;
wuchimedese5722e32025-04-13 17:38:50 +080023 @PostMapping("")
wuchimedes5a842b22025-04-21 22:01:39 +080024 public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) {
wuchimedese5722e32025-04-13 17:38:50 +080025 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
26 long userId = (long) authentication.getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080027 Post post = postCreateDTO.getPost();
28 Long[] tagIds = postCreateDTO.getTagIds();
wuchimedese5722e32025-04-13 17:38:50 +080029 post.setUserId(userId);
wuchimedes5a842b22025-04-21 22:01:39 +080030 if (tagIds.length > 0){
31 postService.createPost(post, tagIds);
32 } else {
33 postService.createPost(post);
34 }
wuchimedese5722e32025-04-13 17:38:50 +080035 return ResponseEntity.ok().build();
36 }
wuchimedese5722e32025-04-13 17:38:50 +080037 @GetMapping("/{postId}")
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080038 public Post getPost(@PathVariable Long postId) {
39 // 获取当前用户ID
40 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
41 long userId = (long) authentication.getPrincipal();
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080042 // 记录浏览行为
43 postService.recordViewHistory(userId, postId);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080044 // 返回帖子详情
wuchimedese5722e32025-04-13 17:38:50 +080045 return postService.getById(postId);
46 }
wuchimedese5722e32025-04-13 17:38:50 +080047 @DeleteMapping("/{postId}")
48 public ResponseEntity<?> deletePost(@PathVariable("postId") Long postId) {
49 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
50 long userId = (long) authentication.getPrincipal();
51 Post post = postService.getById(postId);
52 if (post == null) {
53 return ResponseEntity.status(500).body("Post not found.");
54 }
55 if (post.getUserId()!= userId) {
56 return ResponseEntity.status(403).body("You are not authorized to delete this post.");
57 }
58 postService.removeById(postId);
59 return ResponseEntity.ok().body("Post deleted successfully.");
60 }
wuchimedese5722e32025-04-13 17:38:50 +080061 @GetMapping("/getAll")
62 public List<Post> getAllPosts() {
63 return postService.list();
64 }
wuchimedese5722e32025-04-13 17:38:50 +080065 @GetMapping("/getByUserId/{userId}")
66 public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
67 return postService.getPostsByUserId(userId);
68 }
223010711f457dc2025-04-15 17:35:55 +080069 @PutMapping("/{postId}")
70 public ResponseEntity<?> updatePost(@PathVariable("postId") Long postId, @RequestBody Post post) {
71 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
72 long userId = (long) authentication.getPrincipal();
73 Post existingPost = postService.getById(postId);
夜雨声烦f77d8132025-04-24 19:31:18 +080074
223010711f457dc2025-04-15 17:35:55 +080075 if (existingPost == null) {
76 return ResponseEntity.status(500).body("Post not found.");
77 }
78 if (existingPost.getUserId() != userId) {
79 return ResponseEntity.status(403).body("You are not authorized to update this post.");
80 }
夜雨声烦f77d8132025-04-24 19:31:18 +080081
223010711f457dc2025-04-15 17:35:55 +080082 post.setPostId(postId);
83 post.setUserId(userId);
84 postService.updateById(post);
85 return ResponseEntity.ok().body("Post updated successfully.");
86 }
223010711f457dc2025-04-15 17:35:55 +080087 @GetMapping("/type/{postType}")
88 public ResponseEntity<?> getPostsByType(@PathVariable String postType) {
89 List<Post> posts = postService.getPostsByType(postType);
90 return ResponseEntity.ok().body(posts);
91 }
223010711f457dc2025-04-15 17:35:55 +080092 @PostMapping("/{postId}/like")
93 public ResponseEntity<?> likePost(@PathVariable Long postId) {
94 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
95 long userId = (long) authentication.getPrincipal();
96 postService.likePost(userId, postId);
97 return ResponseEntity.ok().body("Post liked successfully.");
98 }
223010711f457dc2025-04-15 17:35:55 +080099 @DeleteMapping("/{postId}/like")
100 public ResponseEntity<?> unlikePost(@PathVariable Long postId) {
101 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
102 long userId = (long) authentication.getPrincipal();
103 postService.unlikePost(userId, postId);
104 return ResponseEntity.ok().body("Post unliked successfully.");
105 }
223010711f457dc2025-04-15 17:35:55 +0800106 @GetMapping("/{postId}/likes")
107 public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) {
108 Long likeCount = postService.getPostLikeCount(postId);
109 return ResponseEntity.ok().body(likeCount);
110 }
夜雨声烦4527a722025-04-23 17:04:25 +0800111 // 搜索帖子
112 @GetMapping("/search")
113 public List<Post> searchPosts(
114 @RequestParam(required = false) String keyword,
115 @RequestParam(required = false) List<Long> tags, // 修改为接收多个标签
116 @RequestParam(required = false) String author) {
夜雨声烦4527a722025-04-23 17:04:25 +0800117 return postService.searchPosts(keyword, tags, author);
118 }
夜雨声烦ef46ac52025-04-24 20:56:47 +0800119
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800120 @GetMapping("/history")
夜雨声烦ef46ac52025-04-24 20:56:47 +0800121 public ResponseEntity<List<PostHistoryDTO>> getViewHistory() {
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800122 // 获取当前用户ID
123 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
124 long userId = (long) authentication.getPrincipal();
夜雨声烦ef46ac52025-04-24 20:56:47 +0800125
126 // 调用Service层
127 List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800128 return ResponseEntity.ok(history);
129 }
夜雨声烦368e3562025-04-24 01:49:46 +0800130 @GetMapping("/recommended")
131 public ResponseEntity<Page<Post>> getRecommendedPosts(
132 @RequestParam(defaultValue = "1") int page,
133 @RequestParam(defaultValue = "10") int size) {
夜雨声烦368e3562025-04-24 01:49:46 +0800134 // 获取当前用户ID
135 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
136 long userId = (long) authentication.getPrincipal();
夜雨声烦368e3562025-04-24 01:49:46 +0800137 // 调用 Service 层方法
138 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
夜雨声烦368e3562025-04-24 01:49:46 +0800139 return ResponseEntity.ok(pageResult);
140 }
夜雨声烦f77d8132025-04-24 19:31:18 +0800141 @GetMapping("/recommended-by-tags")
142 public ResponseEntity<Page<Post>> getRecommendedByTags(
143 @RequestParam(defaultValue = "1") int page,
144 @RequestParam(defaultValue = "10") int size) {
145 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
146 long userId = (long) authentication.getPrincipal();
147 // 调用标签推荐方法
148 Page<Post> result = postService.getRecommendedByTags(page, size, userId);
149 return ResponseEntity.ok(result);
150 }
151}