wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 2 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 4 | import com.example.g8backend.dto.PostCreateDTO; |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 5 | import com.example.g8backend.entity.PostView; |
| 6 | import com.example.g8backend.mapper.PostViewMapper; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 7 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | import org.springframework.http.ResponseEntity; |
| 9 | import org.springframework.security.core.Authentication; |
| 10 | import org.springframework.security.core.context.SecurityContextHolder; |
| 11 | import org.springframework.web.bind.annotation.*; |
| 12 | import com.example.g8backend.entity.Post; |
| 13 | import com.example.g8backend.service.IPostService; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 14 | import java.util.List; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 15 | @RestController |
| 16 | @RequestMapping("/post") |
| 17 | public class PostController { |
| 18 | @Autowired |
| 19 | private IPostService postService; |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 20 | @Autowired // ✅ 新增注入 |
| 21 | private PostViewMapper postViewMapper; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 22 | @PostMapping("") |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 23 | public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) { |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 24 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 25 | long userId = (long) authentication.getPrincipal(); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 26 | Post post = postCreateDTO.getPost(); |
| 27 | Long[] tagIds = postCreateDTO.getTagIds(); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 28 | post.setUserId(userId); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 29 | if (tagIds.length > 0){ |
| 30 | postService.createPost(post, tagIds); |
| 31 | } else { |
| 32 | postService.createPost(post); |
| 33 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 34 | return ResponseEntity.ok().build(); |
| 35 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 36 | @GetMapping("/{postId}") |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 37 | public Post getPost(@PathVariable Long postId) { |
| 38 | // 获取当前用户ID |
| 39 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 40 | long userId = (long) authentication.getPrincipal(); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 41 | // 记录浏览行为 |
| 42 | postService.recordViewHistory(userId, postId); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 43 | // 返回帖子详情 |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 44 | return postService.getById(postId); |
| 45 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 46 | @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 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 60 | @GetMapping("/getAll") |
| 61 | public List<Post> getAllPosts() { |
| 62 | return postService.list(); |
| 63 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 64 | @GetMapping("/getByUserId/{userId}") |
| 65 | public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) { |
| 66 | return postService.getPostsByUserId(userId); |
| 67 | } |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 68 | @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); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame^] | 73 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 74 | 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 | } |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame^] | 80 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 81 | post.setPostId(postId); |
| 82 | post.setUserId(userId); |
| 83 | postService.updateById(post); |
| 84 | return ResponseEntity.ok().body("Post updated successfully."); |
| 85 | } |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 86 | @GetMapping("/type/{postType}") |
| 87 | public ResponseEntity<?> getPostsByType(@PathVariable String postType) { |
| 88 | List<Post> posts = postService.getPostsByType(postType); |
| 89 | return ResponseEntity.ok().body(posts); |
| 90 | } |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 91 | @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 | } |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 98 | @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 | } |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 105 | @GetMapping("/{postId}/likes") |
| 106 | public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) { |
| 107 | Long likeCount = postService.getPostLikeCount(postId); |
| 108 | return ResponseEntity.ok().body(likeCount); |
| 109 | } |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 110 | // 搜索帖子 |
| 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) { |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 116 | return postService.searchPosts(keyword, tags, author); |
| 117 | } |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 118 | @GetMapping("/history") |
| 119 | public ResponseEntity<List<PostView>> getViewHistory() { |
| 120 | // 获取当前用户ID |
| 121 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 122 | long userId = (long) authentication.getPrincipal(); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 123 | // 查询历史记录(按时间倒序) |
| 124 | List<PostView> history = postViewMapper.selectList( |
| 125 | new QueryWrapper<PostView>() |
| 126 | .eq("user_id", userId) |
| 127 | .orderByDesc("view_time") |
| 128 | ); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 129 | return ResponseEntity.ok(history); |
| 130 | } |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 131 | @GetMapping("/recommended") |
| 132 | public ResponseEntity<Page<Post>> getRecommendedPosts( |
| 133 | @RequestParam(defaultValue = "1") int page, |
| 134 | @RequestParam(defaultValue = "10") int size) { |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 135 | // 获取当前用户ID |
| 136 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 137 | long userId = (long) authentication.getPrincipal(); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 138 | // 调用 Service 层方法 |
| 139 | Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 140 | return ResponseEntity.ok(pageResult); |
| 141 | } |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame^] | 142 | // 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 | } |