Refactor comment creation

pull/137/head
johnniang 2019-02-25 14:17:05 +08:00
parent a84f747111
commit 2446b21650
5 changed files with 88 additions and 70 deletions

View File

@ -1,6 +1,9 @@
package cc.ryanc.halo.model.dto;
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
/**
* <pre>
@ -24,7 +27,7 @@ public class JsonResult {
private String msg;
/**
* Dev message.(only setting in dev environment)
* Development message.(Development environment only)
*/
private String devMsg;
@ -86,4 +89,27 @@ public class JsonResult {
this.devMsg = devMsg;
this.result = result;
}
/**
* Create an ok result with message and data.
*
* @param data result data
* @param message result message
* @return ok result with message and data
*/
@NonNull
public static JsonResult ok(@Nullable String message, @Nullable Object data) {
return new JsonResult(HttpStatus.OK.value(), message, data);
}
/**
* Creates an ok result with message only.
*
* @param message result message
* @return ok result with message only
*/
@NonNull
public static JsonResult ok(@Nullable String message) {
return ok(message, null);
}
}

View File

@ -1,11 +1,8 @@
package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -72,13 +69,8 @@ public class ApiArchivesController {
* @return JsonResult
*/
@GetMapping(value = "/year")
public JsonResult archivesYear() {
final List<Archive> archives = postService.findPostGroupByYear();
if (!CollectionUtils.isEmpty(archives)) {
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), archives);
} else {
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
}
public List<Archive> archivesYear() {
return postService.findPostGroupByYear();
}
/**
@ -132,7 +124,6 @@ public class ApiArchivesController {
/**
* @return JsonResult
*
* @Author Aquan
* @Description
* @Date 2019.1.4 11:06

View File

@ -1,6 +1,5 @@
package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.exception.BadRequestException;
import cc.ryanc.halo.logging.Logger;
import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post;
@ -16,8 +15,9 @@ import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@ -37,13 +37,19 @@ import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
@RequestMapping(value = "/api/comments")
public class ApiCommentController {
private final Logger log = Logger.getLogger(getClass());
/**
* Comment format [formatter 1: commentId, formatter 2: commentAuthor, formatter 3: commentContent]
*/
private final static String COMMENT_FORMAT = "<a href='#comment-id-%s'>@%s</a> %s";
@Autowired
private CommentService commentService;
private final CommentService commentService;
@Autowired
private PostService postService;
private final PostService postService;
public ApiCommentController(CommentService commentService, PostService postService) {
this.commentService = commentService;
this.postService = postService;
}
/**
*
@ -51,49 +57,54 @@ public class ApiCommentController {
* @param comment comment
* @param postId postId
* @param request request
*
* @return JsonResult
*/
@PostMapping(value = "/save")
@ResponseBody
@PostMapping("save")
public JsonResult save(@Valid Comment comment,
@RequestParam(value = "postId") Long postId,
HttpServletRequest request) {
try {
Comment lastComment = null;
final Post post = postService.fetchById(postId).orElse(new Post());
comment.setCommentAuthorEmail(HtmlUtil.escape(comment.getCommentAuthorEmail()).toLowerCase());
comment.setPost(post);
comment.setPost(postService.fetchById(postId).orElse(new Post()));
comment.setCommentAuthorIp(ServletUtil.getClientIP(request));
comment.setIsAdmin(0);
comment.setCommentAuthor(HtmlUtil.escape(comment.getCommentAuthor()));
if (StrUtil.isNotBlank(comment.getCommentAuthorEmail())) {
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail()));
}
if (comment.getCommentParent() > 0) {
lastComment = commentService.fetchById(comment.getCommentParent()).orElse(new Comment());
final StrBuilder buildContent = new StrBuilder("<a href='#comment-id-");
buildContent.append(lastComment.getCommentId());
buildContent.append("'>@");
buildContent.append(lastComment.getCommentAuthor());
buildContent.append("</a> ");
buildContent.append(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>")));
comment.setCommentContent(buildContent.toString());
// Get last comment
Comment lastComment = commentService.fetchById(comment.getCommentParent()).orElse(new Comment());
// Format and set comment content
comment.setCommentContent(String.format(COMMENT_FORMAT, lastComment.getCommentId(), lastComment.getCommentAuthor(), convertToSecureString(comment.getCommentContent())));
} else {
//将评论内容的字符专为安全字符
comment.setCommentContent(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>")));
comment.setCommentContent(convertToSecureString(comment.getCommentContent()));
}
if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl()));
}
// Create the comment
commentService.create(comment);
if (StrUtil.equals(OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
return new JsonResult(HttpStatus.OK.value(), "你的评论已经提交,待博主审核之后可显示。");
return JsonResult.ok("你的评论已经提交,待博主审核之后可显示。");
} else {
return new JsonResult(HttpStatus.OK.value(), "你的评论已经提交,刷新后即可显示。");
return JsonResult.ok("你的评论已经提交,刷新后即可显示。");
}
} catch (Exception e) {
throw new BadRequestException("评论失败!", e);
}
/**
* Converts content to secure content.
*
* @param originalContent original content must not be null
* @return secure content
*/
@NonNull
private String convertToSecureString(@NonNull String originalContent) {
Assert.hasText(originalContent, "Original content must not be blank");
return OwoUtil.markToImg(HtmlUtil.escape(originalContent).replace("&lt;br/&gt;", "<br/>"));
}
}

View File

@ -112,9 +112,6 @@ public class ApiPostController {
}
final Pageable pageable = PageRequest.of(page - 1, size, sort);
final Page<Post> posts = postService.findPostByStatus(PostStatusEnum.PUBLISHED.getCode(), PostTypeEnum.POST_TYPE_POST.getDesc(), pageable);
if (null == posts) {
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
}
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), posts);
}

View File

@ -2,10 +2,8 @@ package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.exception.NotFoundException;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -51,13 +49,8 @@ public class ApiTagController {
* @return JsonResult
*/
@GetMapping
public JsonResult tags() {
final List<Tag> tags = tagService.listAll();
if (null != tags && tags.size() > 0) {
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tags);
} else {
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
}
public List<Tag> tags() {
return tagService.listAll();
}
/**
@ -87,7 +80,7 @@ public class ApiTagController {
final Tag tag = tagService.findByTagUrl(tagUrl);
if (tag == null) {
throw new NotFoundException("Tag with url: " + tagUrl + " was not found");
throw new NotFoundException("Tag with url: " + tagUrl + " was not found").setErrorData(tagUrl);
}
return tag;