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