mirror of https://github.com/halo-dev/halo
Refactor ApiCommentController
parent
3606e25184
commit
87dddd8a03
|
@ -1,10 +1,11 @@
|
|||
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;
|
||||
import cc.ryanc.halo.model.dto.JsonResult;
|
||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||
import cc.ryanc.halo.model.enums.ResponseStatusEnum;
|
||||
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
||||
import cc.ryanc.halo.service.CommentService;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
|
@ -16,8 +17,7 @@ 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.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -37,6 +37,8 @@ import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
|
|||
@RequestMapping(value = "/api/comments")
|
||||
public class ApiCommentController {
|
||||
|
||||
private final Logger log = Logger.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
|
@ -47,7 +49,6 @@ public class ApiCommentController {
|
|||
* 新增评论
|
||||
*
|
||||
* @param comment comment
|
||||
* @param result result
|
||||
* @param postId postId
|
||||
* @param request request
|
||||
* @return JsonResult
|
||||
|
@ -55,14 +56,8 @@ public class ApiCommentController {
|
|||
@PostMapping(value = "/save")
|
||||
@ResponseBody
|
||||
public JsonResult save(@Valid Comment comment,
|
||||
BindingResult result,
|
||||
@RequestParam(value = "postId") Long postId,
|
||||
HttpServletRequest request) {
|
||||
if (result.hasErrors()) {
|
||||
for (ObjectError error : result.getAllErrors()) {
|
||||
return new JsonResult(ResponseStatusEnum.ERROR.getCode(), error.getDefaultMessage());
|
||||
}
|
||||
}
|
||||
try {
|
||||
Comment lastComment = null;
|
||||
final Post post = postService.fetchById(postId).orElse(new Post());
|
||||
|
@ -92,12 +87,12 @@ public class ApiCommentController {
|
|||
}
|
||||
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(ResponseStatusEnum.SUCCESS.getCode(), "你的评论已经提交,待博主审核之后可显示。");
|
||||
return new JsonResult(HttpStatus.OK.value(), "你的评论已经提交,待博主审核之后可显示。");
|
||||
} else {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), "你的评论已经提交,刷新后即可显示。");
|
||||
return new JsonResult(HttpStatus.OK.value(), "你的评论已经提交,刷新后即可显示。");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return new JsonResult(ResponseStatusEnum.ERROR.getCode(), "评论失败!");
|
||||
throw new BadRequestException("评论失败!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue