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
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}")

View File

@ -50,11 +50,12 @@ public interface PostService extends BasePostService<Post> {
* @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<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.
@ -62,11 +63,12 @@ public interface PostService extends BasePostService<Post> {
* @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<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.

View File

@ -144,37 +144,26 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
}
@Override
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds) {
return createOrUpdate(postToCreate, tagIds, categoryIds);
}
@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
LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle());
eventPublisher.publishEvent(logEvent);
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> 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<Integer> tagIds, Set<Integer> 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;
}