Kaynağa Gözat

feat: add admin post update/delete endpoints

yangyi 3 gün önce
ebeveyn
işleme
f8cfa808fa

+ 22 - 0
src/main/java/space/anyi/serve/controller/PostController.java

@@ -103,6 +103,28 @@ public class PostController {
         return Response.ok();
     }
 
+    @Operation(summary = "更新帖子(管理员)")
+    @PreAuthorize("hasRole('ROLE_admin')")
+    @PutMapping("{id}")
+    public Response<Void> updatePost(@PathVariable Long id, @Valid @RequestBody PostDto dto) {
+        Post post = new Post();
+        post.setTitle(dto.getTitle());
+        post.setContentIntro(dto.getContentIntro());
+        post.setContentPaid(dto.getContentPaid());
+        post.setPrice(dto.getPrice());
+        post.setExpireTime(dto.getExpireTime());
+        postService.updatePost(id, post);
+        return Response.ok();
+    }
+
+    @Operation(summary = "删除帖子(管理员,逻辑删除)")
+    @PreAuthorize("hasRole('ROLE_admin')")
+    @DeleteMapping("{id}")
+    public Response<Void> deletePost(@PathVariable Long id) {
+        postService.deletePost(id);
+        return Response.ok();
+    }
+
     @Operation(summary = "获取专家往期帖子")
     @GetMapping("expert/{expertId}/previous")
     public Response<PageVo<List<PostVo>>> listPreviousPosts(

+ 2 - 0
src/main/java/space/anyi/serve/service/PostService.java

@@ -11,6 +11,8 @@ public interface PostService extends IService<Post> {
     Long createPost(Post post);
     void updateHitStatus(Long id, String hitStatus);
     void updateViewCount(Long id, Integer viewCount);
+    void updatePost(Long id, Post post);
+    void deletePost(Long id);
     boolean hasUserPaid(Long postId, Long userId);
     Page<Post> listExpertPreviousPosts(Long expertId, int pageNum, int pageSize);
 }

+ 20 - 0
src/main/java/space/anyi/serve/service/impl/PostServiceImpl.java

@@ -130,6 +130,26 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
         updateById(post);
     }
 
+    @Override
+    @Transactional
+    public void updatePost(Long id, Post post) {
+        Post existing = getById(id);
+        if (existing == null) throw new IllegalArgumentException("帖子不存在");
+        post.setId(id);
+        post.setUpdatedAt(LocalDateTime.now());
+        updateById(post);
+    }
+
+    @Override
+    @Transactional
+    public void deletePost(Long id) {
+        Post post = getById(id);
+        if (post == null) throw new IllegalArgumentException("帖子不存在");
+        post.setDeleteFlag(1);
+        post.setUpdatedAt(LocalDateTime.now());
+        updateById(post);
+    }
+
     @Override
     public boolean hasUserPaid(Long postId, Long userId) {
         if (userId == null) return false;