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) { if (isAdmin == null) {
isAdmin = false; isAdmin = false;
} }
} }
} }

View File

@ -13,4 +13,13 @@ import javax.persistence.Entity;
@DiscriminatorValue("2") @DiscriminatorValue("2")
public class Journal extends BaseComment { 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 @Override
public COMMENT createBy(COMMENT COMMENT) { public COMMENT createBy(COMMENT comment) {
Assert.notNull(COMMENT, "Domain must not be null"); Assert.notNull(comment, "Domain must not be null");
// Check post id // Check post id
boolean isPostExist = postRepository.existsById(COMMENT.getPostId()); if (!ServiceUtils.isEmptyId(comment.getPostId())) {
if (!isPostExist) { postMustExist(comment.getPostId());
throw new NotFoundException("The post with id " + COMMENT.getPostId() + " was not found");
} }
// Check parent id // Check parent id
if (!ServiceUtils.isEmptyId(COMMENT.getParentId())) { if (!ServiceUtils.isEmptyId(comment.getParentId())) {
mustExistById(COMMENT.getParentId()); mustExistById(comment.getParentId());
} }
// Check user login status and set this field // Check user login status and set this field
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Set some default values // Set some default values
COMMENT.setIpAddress(ServletUtils.getRequestIp()); comment.setIpAddress(ServletUtils.getRequestIp());
COMMENT.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT)); comment.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT));
COMMENT.setGavatarMd5(DigestUtils.md5Hex(COMMENT.getEmail())); comment.setGavatarMd5(DigestUtils.md5Hex(comment.getEmail()));
if (authentication != null) { if (authentication != null) {
// Comment of blogger // Comment of blogger
COMMENT.setIsAdmin(true); comment.setIsAdmin(true);
COMMENT.setStatus(CommentStatus.PUBLISHED); comment.setStatus(CommentStatus.PUBLISHED);
} else { } else {
// Comment of guest // Comment of guest
// Handle comment status // Handle comment status
Boolean needAudit = optionService.getByPropertyOrDefault(CommentProperties.NEW_NEED_CHECK, Boolean.class, true); 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 // Create comment
COMMENT createdComment = create(COMMENT); COMMENT createdComment = create(comment);
if (ServiceUtils.isEmptyId(createdComment.getParentId())) { if (ServiceUtils.isEmptyId(createdComment.getParentId())) {
if (authentication == null) { if (authentication == null) {
@ -302,6 +301,18 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
return new BaseCommentDTO().convertFrom(comment); 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 @NonNull
protected Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) { protected Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
Assert.notNull(commentQuery, "Comment query must not be null"); 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.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import run.halo.app.exception.ForbiddenException;
import run.halo.app.model.entity.Journal; import run.halo.app.model.entity.Journal;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
import run.halo.app.model.params.JournalParam; 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.security.context.SecurityContextHolder;
import run.halo.app.service.JournalService; import run.halo.app.service.JournalService;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.utils.ServiceUtils;
import run.halo.app.utils.ValidationUtils; 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.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
journalParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL)); journalParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
journalParam.setEmail(user.getEmail()); 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 // Validate the journal param