ym923 | f1d9f45 | 2025-05-27 18:29:44 +0800 | [diff] [blame^] | 1 | package com.pt5.pthouduan.controller; |
| 2 | |
| 3 | import com.pt5.pthouduan.entity.Post; |
| 4 | import com.pt5.pthouduan.service.PostService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.stereotype.Controller; |
| 7 | import org.springframework.web.bind.annotation.*; |
| 8 | import org.springframework.web.multipart.MultipartFile; |
| 9 | |
| 10 | import java.io.File; |
| 11 | import java.io.IOException; |
| 12 | import java.time.LocalDateTime; |
| 13 | import java.util.List; |
| 14 | import java.util.UUID; |
| 15 | |
| 16 | /** |
| 17 | * <p> |
| 18 | * 帖子控制器 |
| 19 | * </p> |
| 20 | * |
| 21 | * 功能:创建帖子(支持上传图片、置顶、范围控制)、点赞、取消点赞、关键词搜索、删除、更新、置顶与取消置顶 |
| 22 | * |
| 23 | * @author |
| 24 | * @since 2025-05-10 |
| 25 | */ |
| 26 | @CrossOrigin(origins = "http://localhost:5173") |
| 27 | @Controller |
| 28 | @RequestMapping("/post") |
| 29 | public class PostController { |
| 30 | |
| 31 | @Autowired |
| 32 | private PostService postService; |
| 33 | |
| 34 | // 创建帖子(支持图片上传) |
| 35 | @PostMapping("/create") |
| 36 | @ResponseBody |
| 37 | public boolean createPost( |
| 38 | @RequestParam("userid") Long userid, |
| 39 | @RequestParam("post_title") String post_title, |
| 40 | @RequestParam("post_content") String post_content, |
| 41 | @RequestParam(value = "tags", required = false) String tags, |
| 42 | @RequestParam(value = "rannge", required = false) String rannge, |
| 43 | @RequestParam(value = "is_pinned", required = false) Boolean is_pinned, |
| 44 | @RequestParam(value = "photo", required = false) MultipartFile photoFile |
| 45 | ) { |
| 46 | Post post = new Post(); |
| 47 | post.setUserid(userid); |
| 48 | post.setPostTitle(post_title); |
| 49 | post.setPostContent(post_content); |
| 50 | post.setTags(tags); |
| 51 | post.setRannge(rannge); |
| 52 | post.setIsSticky(is_pinned != null && is_pinned); |
| 53 | post.setPostCreatedTime(LocalDateTime.now()); |
| 54 | post.setUpdatedTime(LocalDateTime.now()); |
| 55 | post.setLikes(0); |
| 56 | |
| 57 | |
| 58 | // 保存图片 |
| 59 | if (photoFile != null && !photoFile.isEmpty()) { |
| 60 | String uploadDir = "D:/postuploads/"; |
| 61 | File dir = new File(uploadDir); |
| 62 | if (!dir.exists()) dir.mkdirs(); |
| 63 | |
| 64 | String fileName = UUID.randomUUID() + "_" + photoFile.getOriginalFilename(); |
| 65 | File dest = new File(uploadDir + fileName); |
| 66 | try { |
| 67 | photoFile.transferTo(dest); |
| 68 | post.setPhoto("/images/" + fileName); |
| 69 | } catch (IOException e) { |
| 70 | e.printStackTrace(); |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return postService.createPost(post) != null; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | // 删除帖子 |
| 81 | @DeleteMapping("/delete/{postid}") |
| 82 | @ResponseBody |
| 83 | public boolean deletePost(@PathVariable Integer postid) { |
| 84 | return postService.deletePost(postid); |
| 85 | } |
| 86 | |
| 87 | // 更新帖子(包括置顶、范围、照片等) |
| 88 | @PutMapping("/update") |
| 89 | @ResponseBody |
| 90 | public boolean updatePost(@RequestBody Post post) { |
| 91 | return postService.updatePost(post); |
| 92 | } |
| 93 | |
| 94 | // 关键词搜索 |
| 95 | @GetMapping("/search") |
| 96 | @ResponseBody |
| 97 | public List<Post> searchPosts(@RequestParam String keyword) { |
| 98 | return postService.searchPostsByKeyword(keyword); |
| 99 | } |
| 100 | |
| 101 | // 点赞 |
| 102 | @PutMapping("/like/{postid}") |
| 103 | @ResponseBody |
| 104 | public boolean likePost(@PathVariable Integer postid) { |
| 105 | return postService.incrementLikes(postid); |
| 106 | } |
| 107 | |
| 108 | @GetMapping("/all") |
| 109 | @ResponseBody |
| 110 | public List<Post> getAllPostsSorted() { |
| 111 | return postService.getAllPostsSorted(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | // 取消点赞 |
| 116 | @PutMapping("/unlike/{postid}") |
| 117 | @ResponseBody |
| 118 | public boolean unlikePost(@PathVariable Integer postid) { |
| 119 | return postService.decrementLikes(postid); |
| 120 | } |
| 121 | |
| 122 | // 置顶帖子 |
| 123 | @PutMapping("/pin/{postid}") |
| 124 | @ResponseBody |
| 125 | public boolean pinPost(@PathVariable Integer postid) { |
| 126 | return postService.setPinnedStatus(postid, true); |
| 127 | } |
| 128 | |
| 129 | // 取消置顶 |
| 130 | @PutMapping("/unpin/{postid}") |
| 131 | @ResponseBody |
| 132 | public boolean unpinPost(@PathVariable Integer postid) { |
| 133 | return postService.setPinnedStatus(postid, false); |
| 134 | } |
| 135 | |
| 136 | // 根据用户ID获取该用户所有帖子 |
| 137 | @GetMapping("/findByUserid") |
| 138 | @ResponseBody |
| 139 | public List<Post> findByUserid(@RequestParam Long userid) { |
| 140 | return postService.findByUserid(userid); |
| 141 | } |
| 142 | |
| 143 | // 根据是否置顶查找帖子 |
| 144 | @GetMapping("/findPinned") |
| 145 | @ResponseBody |
| 146 | public List<Post> findPinnedPosts() { |
| 147 | return postService.findPinnedPosts(); |
| 148 | } |
| 149 | } |