mirror of https://github.com/halo-dev/halo
Handler user agent with system
parent
7a679d0f8e
commit
8935f7478a
|
@ -10,6 +10,8 @@ import javax.validation.constraints.NotBlank;
|
|||
import javax.validation.constraints.Size;
|
||||
|
||||
/**
|
||||
* Comment param.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 3/22/19
|
||||
*/
|
||||
|
@ -32,9 +34,6 @@ public class CommentParam implements InputConverter<Comment> {
|
|||
@Size(max = 1023, message = "Length of comment content must not be more than {max}")
|
||||
private String content;
|
||||
|
||||
@Size(max = 512, message = "Length of comment user agent must not be more than {max}")
|
||||
private String userAgent;
|
||||
|
||||
@Min(value = 1, message = "Post id must not be less than {value}")
|
||||
private Integer postId;
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ package cc.ryanc.halo.service;
|
|||
|
||||
import cc.ryanc.halo.model.entity.Comment;
|
||||
import cc.ryanc.halo.model.enums.CommentStatus;
|
||||
import cc.ryanc.halo.model.params.CommentParam;
|
||||
import cc.ryanc.halo.model.vo.CommentListVO;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
@ -69,10 +68,10 @@ public interface CommentService extends CrudService<Comment, Long> {
|
|||
/**
|
||||
* Creates a comment by comment param.
|
||||
*
|
||||
* @param commentParam comment param must not be null and should be validated
|
||||
* @param request http servlet request must not be null
|
||||
* @param comment comment must not be null
|
||||
* @param request http servlet request must not be null
|
||||
* @return created comment
|
||||
*/
|
||||
@NonNull
|
||||
Comment createBy(@NonNull CommentParam commentParam, @NonNull HttpServletRequest request);
|
||||
Comment createBy(@NonNull Comment comment, @NonNull HttpServletRequest request);
|
||||
}
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
package cc.ryanc.halo.service.impl;
|
||||
|
||||
import cc.ryanc.halo.exception.NotFoundException;
|
||||
import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Comment;
|
||||
import cc.ryanc.halo.model.entity.Post;
|
||||
import cc.ryanc.halo.model.enums.BlogProperties;
|
||||
import cc.ryanc.halo.model.enums.CommentStatus;
|
||||
import cc.ryanc.halo.model.params.CommentParam;
|
||||
import cc.ryanc.halo.model.projection.CommentCountProjection;
|
||||
import cc.ryanc.halo.model.vo.CommentListVO;
|
||||
import cc.ryanc.halo.repository.CommentRepository;
|
||||
|
@ -22,6 +20,7 @@ import cn.hutool.extra.servlet.ServletUtil;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -99,27 +98,14 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
|
|||
}
|
||||
|
||||
@Override
|
||||
public Comment createBy(CommentParam commentParam, HttpServletRequest request) {
|
||||
Assert.notNull(commentParam, "Comment param must not be null");
|
||||
public Comment createBy(Comment comment, HttpServletRequest request) {
|
||||
Assert.notNull(comment, "Comment must not be null");
|
||||
Assert.notNull(request, "Http servlet request must not be null");
|
||||
|
||||
if (!postRepository.existsById(commentParam.getPostId())) {
|
||||
// Post id must exist
|
||||
log.error("Post: [{}] was not found", commentParam.getPostId());
|
||||
throw new NotFoundException("The post was not found").setErrorData(commentParam.getPostId());
|
||||
}
|
||||
|
||||
if (commentParam.getParentId() != null && commentParam.getParentId() > 0) {
|
||||
// Validate the comment parent id
|
||||
mustExistById(commentParam.getParentId());
|
||||
}
|
||||
|
||||
// Convert to comment
|
||||
Comment comment = commentParam.convertTo();
|
||||
|
||||
// Set some default value
|
||||
comment.setContent(OwoUtil.parseOwo(formatContent(comment.getContent())));
|
||||
comment.setIpAddress(ServletUtil.getClientIP(request));
|
||||
comment.setUserAgent(ServletUtil.getHeaderIgnoreCase(request, HttpHeaders.USER_AGENT));
|
||||
// TODO Check user login status and set this field
|
||||
comment.setIsAdmin(false);
|
||||
comment.setAuthor(HtmlUtils.htmlEscape(comment.getAuthor()));
|
||||
|
|
|
@ -57,7 +57,15 @@ public class CommentController {
|
|||
|
||||
@PostMapping
|
||||
public CommentOutputDTO createBy(@Valid @RequestBody CommentParam commentParam, HttpServletRequest request) {
|
||||
return new CommentOutputDTO().convertFrom(commentService.createBy(commentParam, request));
|
||||
// Check post id
|
||||
postService.mustExistById(commentParam.getPostId());
|
||||
|
||||
// Check parent id
|
||||
if (commentParam.getParentId() != null && commentParam.getParentId() > 0) {
|
||||
commentService.mustExistById(commentParam.getParentId());
|
||||
}
|
||||
|
||||
return new CommentOutputDTO().convertFrom(commentService.createBy(commentParam.convertTo(), request));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue