blob: 03687c350b1115675705af68b9f7ebff9f9fc617 [file] [log] [blame]
wuchimedese5722e32025-04-13 17:38:50 +08001package com.example.g8backend.controller;
2
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08003import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
夜雨声烦368e3562025-04-24 01:49:46 +08004import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
wuchimedes5a842b22025-04-21 22:01:39 +08005import com.example.g8backend.dto.PostCreateDTO;
夜雨声烦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;
15
16import java.util.List;
17
18@RestController
19@RequestMapping("/post")
20public class PostController {
21 @Autowired
22 private IPostService postService;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080023 @Autowired // ✅ 新增注入
24 private PostViewMapper postViewMapper;
wuchimedese5722e32025-04-13 17:38:50 +080025
26 @PostMapping("")
wuchimedes5a842b22025-04-21 22:01:39 +080027 public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) {
wuchimedese5722e32025-04-13 17:38:50 +080028 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
29 long userId = (long) authentication.getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080030 Post post = postCreateDTO.getPost();
31 Long[] tagIds = postCreateDTO.getTagIds();
32
wuchimedese5722e32025-04-13 17:38:50 +080033 post.setUserId(userId);
wuchimedes5a842b22025-04-21 22:01:39 +080034 if (tagIds.length > 0){
35 postService.createPost(post, tagIds);
36 } else {
37 postService.createPost(post);
38 }
wuchimedese5722e32025-04-13 17:38:50 +080039 return ResponseEntity.ok().build();
40 }
41
42 @GetMapping("/{postId}")
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080043 public Post getPost(@PathVariable Long postId) {
44 // 获取当前用户ID
45 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
46 long userId = (long) authentication.getPrincipal();
47
48 // 记录浏览行为
49 postService.recordViewHistory(userId, postId);
50
51 // 返回帖子详情
wuchimedese5722e32025-04-13 17:38:50 +080052 return postService.getById(postId);
53 }
54
55 @DeleteMapping("/{postId}")
56 public ResponseEntity<?> deletePost(@PathVariable("postId") Long postId) {
57 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
58 long userId = (long) authentication.getPrincipal();
59 Post post = postService.getById(postId);
60 if (post == null) {
61 return ResponseEntity.status(500).body("Post not found.");
62 }
63 if (post.getUserId()!= userId) {
64 return ResponseEntity.status(403).body("You are not authorized to delete this post.");
65 }
66 postService.removeById(postId);
67 return ResponseEntity.ok().body("Post deleted successfully.");
68 }
69
70 @GetMapping("/getAll")
71 public List<Post> getAllPosts() {
72 return postService.list();
73 }
74
75 @GetMapping("/getByUserId/{userId}")
76 public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
77 return postService.getPostsByUserId(userId);
78 }
223010711f457dc2025-04-15 17:35:55 +080079
80 @PutMapping("/{postId}")
81 public ResponseEntity<?> updatePost(@PathVariable("postId") Long postId, @RequestBody Post post) {
82 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
83 long userId = (long) authentication.getPrincipal();
84 Post existingPost = postService.getById(postId);
85
86 if (existingPost == null) {
87 return ResponseEntity.status(500).body("Post not found.");
88 }
89 if (existingPost.getUserId() != userId) {
90 return ResponseEntity.status(403).body("You are not authorized to update this post.");
91 }
92
93 post.setPostId(postId);
94 post.setUserId(userId);
95 postService.updateById(post);
96 return ResponseEntity.ok().body("Post updated successfully.");
97 }
98
99 @GetMapping("/type/{postType}")
100 public ResponseEntity<?> getPostsByType(@PathVariable String postType) {
101 List<Post> posts = postService.getPostsByType(postType);
102 return ResponseEntity.ok().body(posts);
103 }
104
105 @PostMapping("/{postId}/like")
106 public ResponseEntity<?> likePost(@PathVariable Long postId) {
107 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
108 long userId = (long) authentication.getPrincipal();
109 postService.likePost(userId, postId);
110 return ResponseEntity.ok().body("Post liked successfully.");
111 }
112
113 @DeleteMapping("/{postId}/like")
114 public ResponseEntity<?> unlikePost(@PathVariable Long postId) {
115 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
116 long userId = (long) authentication.getPrincipal();
117 postService.unlikePost(userId, postId);
118 return ResponseEntity.ok().body("Post unliked successfully.");
119 }
120
121 @GetMapping("/{postId}/likes")
122 public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) {
123 Long likeCount = postService.getPostLikeCount(postId);
124 return ResponseEntity.ok().body(likeCount);
125 }
夜雨声烦4527a722025-04-23 17:04:25 +0800126
127 // 搜索帖子
128 @GetMapping("/search")
129 public List<Post> searchPosts(
130 @RequestParam(required = false) String keyword,
131 @RequestParam(required = false) List<Long> tags, // 修改为接收多个标签
132 @RequestParam(required = false) String author) {
133
134 return postService.searchPosts(keyword, tags, author);
135 }
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800136
137 @GetMapping("/history")
138 public ResponseEntity<List<PostView>> getViewHistory() {
139 // 获取当前用户ID
140 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
141 long userId = (long) authentication.getPrincipal();
142
143 // 查询历史记录(按时间倒序)
144 List<PostView> history = postViewMapper.selectList(
145 new QueryWrapper<PostView>()
146 .eq("user_id", userId)
147 .orderByDesc("view_time")
148 );
149
150 return ResponseEntity.ok(history);
151 }
夜雨声烦368e3562025-04-24 01:49:46 +0800152
153 @GetMapping("/recommended")
154 public ResponseEntity<Page<Post>> getRecommendedPosts(
155 @RequestParam(defaultValue = "1") int page,
156 @RequestParam(defaultValue = "10") int size) {
157
158 // 获取当前用户ID
159 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
160 long userId = (long) authentication.getPrincipal();
161
162 // 调用 Service 层方法
163 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
164
165 return ResponseEntity.ok(pageResult);
166 }
wuchimedese5722e32025-04-13 17:38:50 +0800167}