wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame^] | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
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; |
| 14 | |
| 15 | import java.util.List; |
| 16 | |
| 17 | @RestController |
| 18 | @RequestMapping("/post") |
| 19 | public class PostController { |
| 20 | @Autowired |
| 21 | private IPostService postService; |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame^] | 22 | @Autowired // ✅ 新增注入 |
| 23 | private PostViewMapper postViewMapper; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 24 | |
| 25 | @PostMapping("") |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 26 | public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) { |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 27 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 28 | long userId = (long) authentication.getPrincipal(); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 29 | Post post = postCreateDTO.getPost(); |
| 30 | Long[] tagIds = postCreateDTO.getTagIds(); |
| 31 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 32 | post.setUserId(userId); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 33 | if (tagIds.length > 0){ |
| 34 | postService.createPost(post, tagIds); |
| 35 | } else { |
| 36 | postService.createPost(post); |
| 37 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 38 | return ResponseEntity.ok().build(); |
| 39 | } |
| 40 | |
| 41 | @GetMapping("/{postId}") |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame^] | 42 | 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 | // 返回帖子详情 |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 51 | 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 | } |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 78 | |
| 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 | } |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 125 | |
| 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 | } |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame^] | 135 | |
| 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 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 151 | } |