This commit is contained in:
whz 2024-05-30 11:45:54 +08:00
parent 0f99455d6b
commit ae4386c1db
3 changed files with 35 additions and 29 deletions

View File

@ -312,17 +312,27 @@ public interface TagMapper {
创建PostService类实现createPost方法。
```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){

View File

@ -35,7 +35,7 @@ public class PostController {
}
@PostMapping("/post")
public DataResult<PostDto> createPost(@RequestBody PostRequest postRequest) {
return ResultUtil.ok(postService.createPost(postRequest));
return ResultUtil.ok(postService.handlePostRequest(postRequest));
}
@GetMapping("/post/comments")

View File

@ -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)->{