blob: 51aa519670a5172d2614fb67d99e51820e850b88 [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;
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;
14
15import java.util.List;
16
17@RestController
18@RequestMapping("/post")
19public class PostController {
20 @Autowired
21 private IPostService postService;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080022 @Autowired // ✅ 新增注入
23 private PostViewMapper postViewMapper;
wuchimedese5722e32025-04-13 17:38:50 +080024
25 @PostMapping("")
wuchimedes5a842b22025-04-21 22:01:39 +080026 public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) {
wuchimedese5722e32025-04-13 17:38:50 +080027 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
28 long userId = (long) authentication.getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080029 Post post = postCreateDTO.getPost();
30 Long[] tagIds = postCreateDTO.getTagIds();
31
wuchimedese5722e32025-04-13 17:38:50 +080032 post.setUserId(userId);
wuchimedes5a842b22025-04-21 22:01:39 +080033 if (tagIds.length > 0){
34 postService.createPost(post, tagIds);
35 } else {
36 postService.createPost(post);
37 }
wuchimedese5722e32025-04-13 17:38:50 +080038 return ResponseEntity.ok().build();
39 }
40
41 @GetMapping("/{postId}")
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080042 public Post getPost(@PathVariable Long postId) {
43 // 获取当前用户ID
44 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
45 long userId = (long) authentication.getPrincipal();
46
47 // 记录浏览行为
48 postService.recordViewHistory(userId, postId);
49
50 // 返回帖子详情
wuchimedese5722e32025-04-13 17:38:50 +080051 return postService.getById(postId);
52 }
53
54 @DeleteMapping("/{postId}")
55 public ResponseEntity<?> deletePost(@PathVariable("postId") Long postId) {
56 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
57 long userId = (long) authentication.getPrincipal();
58 Post post = postService.getById(postId);
59 if (post == null) {
60 return ResponseEntity.status(500).body("Post not found.");
61 }
62 if (post.getUserId()!= userId) {
63 return ResponseEntity.status(403).body("You are not authorized to delete this post.");
64 }
65 postService.removeById(postId);
66 return ResponseEntity.ok().body("Post deleted successfully.");
67 }
68
69 @GetMapping("/getAll")
70 public List<Post> getAllPosts() {
71 return postService.list();
72 }
73
74 @GetMapping("/getByUserId/{userId}")
75 public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
76 return postService.getPostsByUserId(userId);
77 }
223010711f457dc2025-04-15 17:35:55 +080078
79 @PutMapping("/{postId}")
80 public ResponseEntity<?> updatePost(@PathVariable("postId") Long postId, @RequestBody Post post) {
81 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
82 long userId = (long) authentication.getPrincipal();
83 Post existingPost = postService.getById(postId);
84
85 if (existingPost == null) {
86 return ResponseEntity.status(500).body("Post not found.");
87 }
88 if (existingPost.getUserId() != userId) {
89 return ResponseEntity.status(403).body("You are not authorized to update this post.");
90 }
91
92 post.setPostId(postId);
93 post.setUserId(userId);
94 postService.updateById(post);
95 return ResponseEntity.ok().body("Post updated successfully.");
96 }
97
98 @GetMapping("/type/{postType}")
99 public ResponseEntity<?> getPostsByType(@PathVariable String postType) {
100 List<Post> posts = postService.getPostsByType(postType);
101 return ResponseEntity.ok().body(posts);
102 }
103
104 @PostMapping("/{postId}/like")
105 public ResponseEntity<?> likePost(@PathVariable Long postId) {
106 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
107 long userId = (long) authentication.getPrincipal();
108 postService.likePost(userId, postId);
109 return ResponseEntity.ok().body("Post liked successfully.");
110 }
111
112 @DeleteMapping("/{postId}/like")
113 public ResponseEntity<?> unlikePost(@PathVariable Long postId) {
114 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
115 long userId = (long) authentication.getPrincipal();
116 postService.unlikePost(userId, postId);
117 return ResponseEntity.ok().body("Post unliked successfully.");
118 }
119
120 @GetMapping("/{postId}/likes")
121 public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) {
122 Long likeCount = postService.getPostLikeCount(postId);
123 return ResponseEntity.ok().body(likeCount);
124 }
夜雨声烦4527a722025-04-23 17:04:25 +0800125
126 // 搜索帖子
127 @GetMapping("/search")
128 public List<Post> searchPosts(
129 @RequestParam(required = false) String keyword,
130 @RequestParam(required = false) List<Long> tags, // 修改为接收多个标签
131 @RequestParam(required = false) String author) {
132
133 return postService.searchPosts(keyword, tags, author);
134 }
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800135
136 @GetMapping("/history")
137 public ResponseEntity<List<PostView>> getViewHistory() {
138 // 获取当前用户ID
139 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
140 long userId = (long) authentication.getPrincipal();
141
142 // 查询历史记录(按时间倒序)
143 List<PostView> history = postViewMapper.selectList(
144 new QueryWrapper<PostView>()
145 .eq("user_id", userId)
146 .orderByDesc("view_time")
147 );
148
149 return ResponseEntity.ok(history);
150 }
wuchimedese5722e32025-04-13 17:38:50 +0800151}