22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame^] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.entity.Post; |
| 4 | import com.example.myproject.service.PostService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.web.bind.annotation.*; |
| 7 | import org.springframework.web.multipart.MultipartFile; |
| 8 | |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | import java.util.Optional; |
| 12 | |
| 13 | @RestController |
| 14 | @RequestMapping("/echo/forum/posts") |
| 15 | public class PostController { |
| 16 | |
| 17 | @Autowired |
| 18 | private PostService postService; |
| 19 | |
| 20 | //创建新帖子(已完成) |
| 21 | @PostMapping("/{user_id}/createPost") |
| 22 | public Map<String, Object> createPost( |
| 23 | @PathVariable("user_id") Long userId, |
| 24 | @RequestParam(value = "postContent") String postContent, |
| 25 | @RequestParam(value = "title", required = false) String title, |
| 26 | @RequestParam(value = "imageUrl") MultipartFile[] imageFiles) { |
| 27 | return postService.createPost(userId, postContent, title, imageFiles); |
| 28 | } |
| 29 | |
| 30 | //编辑帖子(已完成) |
| 31 | @PutMapping("/{post_id}/editPost") |
| 32 | public String updatePost(@PathVariable("post_id") Long postId, @RequestBody Post post) { |
| 33 | postService.updatePost(postId, post); |
| 34 | return "Post updated successfully!"; |
| 35 | } |
| 36 | |
| 37 | //删除帖子(已完成) |
| 38 | @DeleteMapping("/{post_id}/deletePost") |
| 39 | public String deletePost(@PathVariable("post_id") Long postId) { |
| 40 | postService.deletePost(postId); |
| 41 | return "Post deleted successfully!"; |
| 42 | } |
| 43 | |
| 44 | //点赞帖子(已完成) |
| 45 | @PostMapping("/{post_id}/like") |
| 46 | public String likePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) { |
| 47 | |
| 48 | Long userId = requestBody.get("user_id"); |
| 49 | postService.likePost(postId, userId); |
| 50 | |
| 51 | return "Post liked successfully!"; |
| 52 | } |
| 53 | |
| 54 | //取消点赞(已完成) |
| 55 | @PostMapping("/{post_id}/unlike") |
| 56 | public String unlikePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) { |
| 57 | Long userId = requestBody.get("user_id"); |
| 58 | postService.unlikePost(postId, userId); |
| 59 | |
| 60 | return "Post unliked successfully!"; |
| 61 | } |
| 62 | |
| 63 | //获取帖子列表(已完成) |
| 64 | @GetMapping("/getAllPosts") |
| 65 | public Map<String, Object> getAllPosts() { |
| 66 | return postService.getAllPosts(); // 调用服务层的 getAllPosts 方法 |
| 67 | } |
| 68 | |
| 69 | @GetMapping("/{postId}/getPost") |
| 70 | public Map<String, Object> getPostById(@PathVariable Long postId) { |
| 71 | return postService.getPostById(postId); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | //收藏帖子(已完成) |
| 76 | @PostMapping("/{post_id}/collect") |
| 77 | public String collectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) { |
| 78 | Long userId = requestBody.get("user_id"); |
| 79 | postService.collectPost(postId, userId); |
| 80 | |
| 81 | return "Post collected successfully!"; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | //取消收藏(已完成) |
| 86 | @DeleteMapping("/{post_id}/uncollect") |
| 87 | public String uncollectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) { |
| 88 | Long userId = requestBody.get("user_id"); |
| 89 | postService.uncollectPost(postId, userId); |
| 90 | return "Post uncollected successfully!"; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | } |