From 19f8b588fa799d024a9051d70bad6ef211e4a53d Mon Sep 17 00:00:00 2001 From: ruibaby Date: Thu, 16 May 2019 23:45:55 +0800 Subject: [PATCH] Refactor post create and update. --- .../controller/admin/api/PostController.java | 10 +++-- .../run/halo/app/service/PostService.java | 6 ++- .../app/service/impl/PostServiceImpl.java | 43 +++++++------------ 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/java/run/halo/app/controller/admin/api/PostController.java b/src/main/java/run/halo/app/controller/admin/api/PostController.java index 9f3bad41d..d69f1ff25 100644 --- a/src/main/java/run/halo/app/controller/admin/api/PostController.java +++ b/src/main/java/run/halo/app/controller/admin/api/PostController.java @@ -77,22 +77,24 @@ public class PostController { } @PostMapping - public PostDetailVO createBy(@Valid @RequestBody PostParam postParam) { + public PostDetailVO createBy(@Valid @RequestBody PostParam postParam, + @RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave) { // Convert to Post post = postParam.convertTo(); - return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds()); + return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds(), autoSave); } @PutMapping("{postId:\\d+}") public PostDetailVO updateBy(@Valid @RequestBody PostParam postParam, - @PathVariable("postId") Integer postId) { + @PathVariable("postId") Integer postId, + @RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave) { // Get the post info Post postToUpdate = postService.getById(postId); postParam.update(postToUpdate); - return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds()); + return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds(), autoSave); } @PutMapping("{postId:\\d+}/status/{status}") diff --git a/src/main/java/run/halo/app/service/PostService.java b/src/main/java/run/halo/app/service/PostService.java index c309132ac..ff4c62428 100755 --- a/src/main/java/run/halo/app/service/PostService.java +++ b/src/main/java/run/halo/app/service/PostService.java @@ -50,11 +50,12 @@ public interface PostService extends BasePostService { * @param post post must not be null * @param tagIds tag id set * @param categoryIds category id set + * @param autoSave autoSave * @return post created */ @NonNull @Transactional - PostDetailVO createBy(@NonNull Post post, Set tagIds, Set categoryIds); + PostDetailVO createBy(@NonNull Post post, Set tagIds, Set categoryIds, boolean autoSave); /** * Updates post by post, tag id set and category id set. @@ -62,11 +63,12 @@ public interface PostService extends BasePostService { * @param postToUpdate post to update must not be null * @param tagIds tag id set * @param categoryIds category id set + * @param autoSave autoSave * @return updated post */ @NonNull @Transactional - PostDetailVO updateBy(@NonNull Post postToUpdate, Set tagIds, Set categoryIds); + PostDetailVO updateBy(@NonNull Post postToUpdate, Set tagIds, Set categoryIds, boolean autoSave); /** * Gets post by post status and url. diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index 93516b0fb..4d3bfc309 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -144,37 +144,26 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe } @Override - public PostDetailVO createBy(Post postToCreate, Set tagIds, Set categoryIds) { - return createOrUpdate(postToCreate, tagIds, categoryIds); - } - - @Override - public PostDetailVO updateBy(Post postToUpdate, Set tagIds, Set categoryIds) { - // Set edit time - postToUpdate.setEditTime(DateUtils.now()); - - return createOrUpdate(postToUpdate, tagIds, categoryIds); - } - - @Override - public Post create(Post post) { - Post createdPost = super.create(post); - - // Log the creation - LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle()); - eventPublisher.publishEvent(logEvent); - + public PostDetailVO createBy(Post postToCreate, Set tagIds, Set categoryIds, boolean autoSave) { + PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds); + if(!autoSave){ + // Log the creation + LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle()); + eventPublisher.publishEvent(logEvent); + } return createdPost; } @Override - public Post update(Post post) { - Post updatedPost = super.update(post); - - // Log the creation - LogEvent logEvent = new LogEvent(this, updatedPost.getId().toString(), LogType.POST_EDITED, updatedPost.getTitle()); - eventPublisher.publishEvent(logEvent); - + public PostDetailVO updateBy(Post postToUpdate, Set tagIds, Set categoryIds, boolean autoSave) { + // Set edit time + postToUpdate.setEditTime(DateUtils.now()); + PostDetailVO updatedPost = createOrUpdate(postToUpdate, tagIds, categoryIds); + if(!autoSave){ + // Log the creation + LogEvent logEvent = new LogEvent(this, updatedPost.getId().toString(), LogType.POST_EDITED, updatedPost.getTitle()); + eventPublisher.publishEvent(logEvent); + } return updatedPost; }