Fix journal creation error

pull/146/head
johnniang 2019-04-25 13:10:50 +08:00
parent 6cfca880ab
commit f99efc891f
4 changed files with 41 additions and 15 deletions

View File

@ -132,7 +132,6 @@ public class BaseComment extends BaseEntity {
if (isAdmin == null) {
isAdmin = false;
}
}
}

View File

@ -13,4 +13,13 @@ import javax.persistence.Entity;
@DiscriminatorValue("2")
public class Journal extends BaseComment {
@Override
public void prePersist() {
super.prePersist();
if (getPostId() == null) {
setPostId(0);
}
}
}

View File

@ -207,41 +207,40 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
}
@Override
public COMMENT createBy(COMMENT COMMENT) {
Assert.notNull(COMMENT, "Domain must not be null");
public COMMENT createBy(COMMENT comment) {
Assert.notNull(comment, "Domain must not be null");
// Check post id
boolean isPostExist = postRepository.existsById(COMMENT.getPostId());
if (!isPostExist) {
throw new NotFoundException("The post with id " + COMMENT.getPostId() + " was not found");
if (!ServiceUtils.isEmptyId(comment.getPostId())) {
postMustExist(comment.getPostId());
}
// Check parent id
if (!ServiceUtils.isEmptyId(COMMENT.getParentId())) {
mustExistById(COMMENT.getParentId());
if (!ServiceUtils.isEmptyId(comment.getParentId())) {
mustExistById(comment.getParentId());
}
// Check user login status and set this field
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Set some default values
COMMENT.setIpAddress(ServletUtils.getRequestIp());
COMMENT.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT));
COMMENT.setGavatarMd5(DigestUtils.md5Hex(COMMENT.getEmail()));
comment.setIpAddress(ServletUtils.getRequestIp());
comment.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT));
comment.setGavatarMd5(DigestUtils.md5Hex(comment.getEmail()));
if (authentication != null) {
// Comment of blogger
COMMENT.setIsAdmin(true);
COMMENT.setStatus(CommentStatus.PUBLISHED);
comment.setIsAdmin(true);
comment.setStatus(CommentStatus.PUBLISHED);
} else {
// Comment of guest
// Handle comment status
Boolean needAudit = optionService.getByPropertyOrDefault(CommentProperties.NEW_NEED_CHECK, Boolean.class, true);
COMMENT.setStatus(needAudit ? CommentStatus.AUDITING : CommentStatus.PUBLISHED);
comment.setStatus(needAudit ? CommentStatus.AUDITING : CommentStatus.PUBLISHED);
}
// Create comment
COMMENT createdComment = create(COMMENT);
COMMENT createdComment = create(comment);
if (ServiceUtils.isEmptyId(createdComment.getParentId())) {
if (authentication == null) {
@ -302,6 +301,18 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
return new BaseCommentDTO().convertFrom(comment);
}
/**
* Post must exist.
*
* @param postId post id must not be null
*/
protected void postMustExist(@NonNull Integer postId) {
boolean isPostExist = postRepository.existsById(postId);
if (!isPostExist) {
throw new NotFoundException("The post with id " + postId + " was not found");
}
}
@NonNull
protected Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
Assert.notNull(commentQuery, "Comment query must not be null");

View File

@ -6,6 +6,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import run.halo.app.exception.ForbiddenException;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.entity.User;
import run.halo.app.model.params.JournalParam;
@ -16,6 +17,7 @@ import run.halo.app.security.authentication.Authentication;
import run.halo.app.security.context.SecurityContextHolder;
import run.halo.app.service.JournalService;
import run.halo.app.service.OptionService;
import run.halo.app.utils.ServiceUtils;
import run.halo.app.utils.ValidationUtils;
/**
@ -51,6 +53,11 @@ public class JournalServiceImpl extends BaseCommentServiceImpl<Journal> implemen
journalParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
journalParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
journalParam.setEmail(user.getEmail());
} else {
// Guest comment
if (ServiceUtils.isEmptyId(journalParam.getParentId())) {
throw new ForbiddenException("You have no right to create a journal");
}
}
// Validate the journal param