Refactor post create and update.

pull/167/head^2
ruibaby 2019-05-16 23:45:55 +08:00 committed by John Niang
parent 6febabc289
commit 19f8b588fa
3 changed files with 26 additions and 33 deletions

View File

@ -77,22 +77,24 @@ public class PostController {
} }
@PostMapping @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 // Convert to
Post post = postParam.convertTo(); Post post = postParam.convertTo();
return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds()); return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds(), autoSave);
} }
@PutMapping("{postId:\\d+}") @PutMapping("{postId:\\d+}")
public PostDetailVO updateBy(@Valid @RequestBody PostParam postParam, 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 // Get the post info
Post postToUpdate = postService.getById(postId); Post postToUpdate = postService.getById(postId);
postParam.update(postToUpdate); 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}") @PutMapping("{postId:\\d+}/status/{status}")

View File

@ -50,11 +50,12 @@ public interface PostService extends BasePostService<Post> {
* @param post post must not be null * @param post post must not be null
* @param tagIds tag id set * @param tagIds tag id set
* @param categoryIds category id set * @param categoryIds category id set
* @param autoSave autoSave
* @return post created * @return post created
*/ */
@NonNull @NonNull
@Transactional @Transactional
PostDetailVO createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds); PostDetailVO createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave);
/** /**
* Updates post by post, tag id set and category id set. * Updates post by post, tag id set and category id set.
@ -62,11 +63,12 @@ public interface PostService extends BasePostService<Post> {
* @param postToUpdate post to update must not be null * @param postToUpdate post to update must not be null
* @param tagIds tag id set * @param tagIds tag id set
* @param categoryIds category id set * @param categoryIds category id set
* @param autoSave autoSave
* @return updated post * @return updated post
*/ */
@NonNull @NonNull
@Transactional @Transactional
PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds); PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave);
/** /**
* Gets post by post status and url. * Gets post by post status and url.

View File

@ -144,37 +144,26 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds) { public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave) {
return createOrUpdate(postToCreate, tagIds, categoryIds); PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds);
} if(!autoSave){
@Override
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> 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 // Log the creation
LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle()); LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle());
eventPublisher.publishEvent(logEvent); eventPublisher.publishEvent(logEvent);
}
return createdPost; return createdPost;
} }
@Override @Override
public Post update(Post post) { public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave) {
Post updatedPost = super.update(post); // Set edit time
postToUpdate.setEditTime(DateUtils.now());
PostDetailVO updatedPost = createOrUpdate(postToUpdate, tagIds, categoryIds);
if(!autoSave){
// Log the creation // Log the creation
LogEvent logEvent = new LogEvent(this, updatedPost.getId().toString(), LogType.POST_EDITED, updatedPost.getTitle()); LogEvent logEvent = new LogEvent(this, updatedPost.getId().toString(), LogType.POST_EDITED, updatedPost.getTitle());
eventPublisher.publishEvent(logEvent); eventPublisher.publishEvent(logEvent);
}
return updatedPost; return updatedPost;
} }