diff --git a/docs/tasks/任务10-Sprint-2- 发布动态.md b/docs/tasks/任务10-Sprint-2- 发布动态.md index 7b6b2ce..de884f9 100644 --- a/docs/tasks/任务10-Sprint-2- 发布动态.md +++ b/docs/tasks/任务10-Sprint-2- 发布动态.md @@ -311,18 +311,28 @@ public interface TagMapper { 创建PostService类,实现createPost方法。 -```java - - /** - * 创建文章 +```java + /** + * 处理文章发布请求 * @param postRequest 创建文章的请求参数 * @return 创建成功后的文章DTO */ - public PostDto createPost(PostRequest postRequest){ + public PostDto handlePostRequest(PostRequest postRequest){ // 获取当前用户 User user = authService.getCurrentUser(); + // 保存文章 + Post post = savePost(postRequest, user); + // 处理post中的标签 + handleTags(postRequest, user); + // 发送消息通知post中@的用户 + handleAtUser(postRequest, user); + + return postMapper.toDto(post); + } + + private Post savePost(PostRequest postRequest, User currentUser){ Post post = new Post(); - post.setUser(user); + post.setUser(currentUser); // 设置附件价格,默认为0 post.setAttachmentPrice(postRequest.getAttachmentPrice()!=null?postRequest.getAttachmentPrice():0); // 设置文章可见性 @@ -335,19 +345,12 @@ public interface TagMapper { content.setSort(contentReq.getSort()); // 设置排序 content.setType(contentReq.getType()); // 设置类型 content.setPost(post); // 关联文章 - content.setUser(user); // 关联用户 + content.setUser(currentUser); // 关联用户 contents.add(content); // 添加到列表 }); post.setContents(contents); // 保存文章 - Post savedPost = postRepository.save(post); - - // 处理post中的标签 - handleTags(postRequest, user); - // 发送消息通知post中@的用户 - handleAtUser(postRequest, user); - - return postMapper.toDto(savedPost); + return postRepository.save(post); } private void handleTags(PostRequest postRequest, User currentUser){ diff --git a/src/main/java/com/lk/paopao/controller/PostController.java b/src/main/java/com/lk/paopao/controller/PostController.java index 54c3782..8c9cfe8 100644 --- a/src/main/java/com/lk/paopao/controller/PostController.java +++ b/src/main/java/com/lk/paopao/controller/PostController.java @@ -35,7 +35,7 @@ public class PostController { } @PostMapping("/post") public DataResult createPost(@RequestBody PostRequest postRequest) { - return ResultUtil.ok(postService.createPost(postRequest)); + return ResultUtil.ok(postService.handlePostRequest(postRequest)); } @GetMapping("/post/comments") diff --git a/src/main/java/com/lk/paopao/service/PostService.java b/src/main/java/com/lk/paopao/service/PostService.java index e965852..02e9ba4 100644 --- a/src/main/java/com/lk/paopao/service/PostService.java +++ b/src/main/java/com/lk/paopao/service/PostService.java @@ -84,15 +84,26 @@ public class PostService { } /** - * 创建文章 + * 处理文章发布请求 * @param postRequest 创建文章的请求参数 * @return 创建成功后的文章DTO */ - public PostDto createPost(PostRequest postRequest){ + public PostDto handlePostRequest(PostRequest postRequest){ // 获取当前用户 User user = authService.getCurrentUser(); + // 保存文章 + Post post = savePost(postRequest, user); + // 处理post中的标签 + handleTags(postRequest, user); + // 发送消息通知post中@的用户 + handleAtUser(postRequest, user); + + return postMapper.toDto(post); + } + + private Post savePost(PostRequest postRequest, User currentUser){ Post post = new Post(); - post.setUser(user); + post.setUser(currentUser); // 设置附件价格,默认为0 post.setAttachmentPrice(postRequest.getAttachmentPrice()!=null?postRequest.getAttachmentPrice():0); // 设置文章可见性 @@ -105,21 +116,13 @@ public class PostService { content.setSort(contentReq.getSort()); // 设置排序 content.setType(contentReq.getType()); // 设置类型 content.setPost(post); // 关联文章 - content.setUser(user); // 关联用户 + content.setUser(currentUser); // 关联用户 contents.add(content); // 添加到列表 }); post.setContents(contents); // 保存文章 - Post savedPost = postRepository.save(post); - - // 处理post中的标签 - handleTags(postRequest, user); - // 发送消息通知post中@的用户 - handleAtUser(postRequest, user); - - return postMapper.toDto(savedPost); + return postRepository.save(post); } - private void handleTags(PostRequest postRequest, User currentUser){ // 处理post中的标签 postRequest.getTags().forEach((tag)->{