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