mirror of https://github.com/halo-dev/halo
complete list latest comments service
parent
00d0670ac6
commit
ef3780369d
|
@ -0,0 +1,41 @@
|
||||||
|
package cc.ryanc.halo.model.dto;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.dto.base.OutputConverter;
|
||||||
|
import cc.ryanc.halo.model.entity.Comment;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comment output dto.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
@EqualsAndHashCode
|
||||||
|
public class CommentOutputDTO implements OutputConverter<CommentOutputDTO, Comment> {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
private String ipAddress;
|
||||||
|
|
||||||
|
private String gavatarMd5;
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
private String userAgent;
|
||||||
|
|
||||||
|
private Long parentId;
|
||||||
|
|
||||||
|
private Boolean isAdmin;
|
||||||
|
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cc.ryanc.halo.model.dto;
|
package cc.ryanc.halo.model.dto.post;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
|
@ -1,7 +1,5 @@
|
||||||
package cc.ryanc.halo.model.dto;
|
package cc.ryanc.halo.model.dto.post;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.base.OutputConverter;
|
|
||||||
import cc.ryanc.halo.model.entity.Post;
|
|
||||||
import cc.ryanc.halo.model.enums.PostCreateFrom;
|
import cc.ryanc.halo.model.enums.PostCreateFrom;
|
||||||
import cc.ryanc.halo.model.enums.PostType;
|
import cc.ryanc.halo.model.enums.PostType;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -17,15 +15,8 @@ import java.util.Date;
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@ToString
|
@ToString
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class PostSimpleOutputDTO implements OutputConverter<PostSimpleOutputDTO, Post> {
|
public class PostSimpleOutputDTO extends PostWithTitleDTO {
|
||||||
|
|
||||||
private Integer id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文章标题
|
|
||||||
*/
|
|
||||||
private String title;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文章类型
|
* 文章类型
|
|
@ -0,0 +1,26 @@
|
||||||
|
package cc.ryanc.halo.model.dto.post;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.dto.base.OutputConverter;
|
||||||
|
import cc.ryanc.halo.model.entity.Post;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Page with title only dto.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
@EqualsAndHashCode
|
||||||
|
public class PostWithTitleDTO implements OutputConverter<PostWithTitleDTO, Post> {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文章标题
|
||||||
|
*/
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
}
|
|
@ -69,6 +69,12 @@ public class Comment {
|
||||||
@Column(name = "is_admin", columnDefinition = "tinyint default 0")
|
@Column(name = "is_admin", columnDefinition = "tinyint default 0")
|
||||||
private Boolean isAdmin;
|
private Boolean isAdmin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post id.
|
||||||
|
*/
|
||||||
|
@Column(name = "post_id", columnDefinition = "int not null")
|
||||||
|
private Integer postId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上级评论
|
* 上级评论
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package cc.ryanc.halo.model.vo;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.dto.CommentOutputDTO;
|
||||||
|
import cc.ryanc.halo.model.dto.post.PostWithTitleDTO;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comment vo.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class CommentVO extends CommentOutputDTO {
|
||||||
|
|
||||||
|
private PostWithTitleDTO post;
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
package cc.ryanc.halo.service;
|
package cc.ryanc.halo.service;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.entity.Comment;
|
import cc.ryanc.halo.model.entity.Comment;
|
||||||
|
import cc.ryanc.halo.model.vo.CommentVO;
|
||||||
import cc.ryanc.halo.service.base.CrudService;
|
import cc.ryanc.halo.service.base.CrudService;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comment service.
|
* Comment service.
|
||||||
|
@ -10,4 +12,11 @@ import cc.ryanc.halo.service.base.CrudService;
|
||||||
*/
|
*/
|
||||||
public interface CommentService extends CrudService<Comment, Long> {
|
public interface CommentService extends CrudService<Comment, Long> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists latest comments.
|
||||||
|
*
|
||||||
|
* @param top top number must not be less than 0
|
||||||
|
* @return a page of comments
|
||||||
|
*/
|
||||||
|
Page<CommentVO> listLatest(int top);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package cc.ryanc.halo.service;
|
package cc.ryanc.halo.service;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.PostSimpleOutputDTO;
|
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Post;
|
import cc.ryanc.halo.model.entity.Post;
|
||||||
import cc.ryanc.halo.service.base.CrudService;
|
import cc.ryanc.halo.service.base.CrudService;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
|
@ -1,10 +1,22 @@
|
||||||
package cc.ryanc.halo.service.impl;
|
package cc.ryanc.halo.service.impl;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.dto.post.PostWithTitleDTO;
|
||||||
import cc.ryanc.halo.model.entity.Comment;
|
import cc.ryanc.halo.model.entity.Comment;
|
||||||
|
import cc.ryanc.halo.model.entity.Post;
|
||||||
|
import cc.ryanc.halo.model.vo.CommentVO;
|
||||||
import cc.ryanc.halo.repository.CommentRepository;
|
import cc.ryanc.halo.repository.CommentRepository;
|
||||||
import cc.ryanc.halo.service.CommentService;
|
import cc.ryanc.halo.service.CommentService;
|
||||||
|
import cc.ryanc.halo.service.PostService;
|
||||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||||
|
import cc.ryanc.halo.utils.ServiceUtils;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CommentService implementation class
|
* CommentService implementation class
|
||||||
|
@ -17,8 +29,39 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
|
||||||
|
|
||||||
private final CommentRepository commentRepository;
|
private final CommentRepository commentRepository;
|
||||||
|
|
||||||
public CommentServiceImpl(CommentRepository commentRepository) {
|
private final PostService postService;
|
||||||
|
|
||||||
|
public CommentServiceImpl(CommentRepository commentRepository,
|
||||||
|
PostService postService) {
|
||||||
super(commentRepository);
|
super(commentRepository);
|
||||||
this.commentRepository = commentRepository;
|
this.commentRepository = commentRepository;
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<CommentVO> listLatest(int top) {
|
||||||
|
Assert.isTrue(top > 0, "Top number must not be less than 0");
|
||||||
|
|
||||||
|
// Build page request
|
||||||
|
PageRequest latestPageable = PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "createTime"));
|
||||||
|
|
||||||
|
// List all comments
|
||||||
|
Page<Comment> comments = listAll(latestPageable);
|
||||||
|
|
||||||
|
// Fetch goods ids
|
||||||
|
Set<Integer> postIds = ServiceUtils.fetchProperty(comments.getContent(), Comment::getPostId);
|
||||||
|
|
||||||
|
// Get all posts
|
||||||
|
Map<Integer, Post> postMap = ServiceUtils.convertToMap(postService.listAllByIds(postIds), Post::getId);
|
||||||
|
|
||||||
|
return comments.map(comment -> {
|
||||||
|
// Convert to vo
|
||||||
|
CommentVO commentVO = new CommentVO().convertFrom(comment);
|
||||||
|
|
||||||
|
// Get post and set to the vo
|
||||||
|
commentVO.setPost(new PostWithTitleDTO().convertFrom(postMap.get(comment.getPostId())));
|
||||||
|
|
||||||
|
return commentVO;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package cc.ryanc.halo.service.impl;
|
package cc.ryanc.halo.service.impl;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.PostSimpleOutputDTO;
|
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Post;
|
import cc.ryanc.halo.model.entity.Post;
|
||||||
import cc.ryanc.halo.repository.PostRepository;
|
import cc.ryanc.halo.repository.PostRepository;
|
||||||
import cc.ryanc.halo.service.PostService;
|
import cc.ryanc.halo.service.PostService;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package cc.ryanc.halo.web.controller.admin;
|
package cc.ryanc.halo.web.controller.admin;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.PostSimpleOutputDTO;
|
import cc.ryanc.halo.model.dto.LogOutputDTO;
|
||||||
|
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
||||||
|
import cc.ryanc.halo.model.vo.CommentVO;
|
||||||
import cc.ryanc.halo.service.AttachmentService;
|
import cc.ryanc.halo.service.AttachmentService;
|
||||||
import cc.ryanc.halo.service.CommentService;
|
import cc.ryanc.halo.service.CommentService;
|
||||||
import cc.ryanc.halo.service.LogService;
|
import cc.ryanc.halo.service.LogService;
|
||||||
|
@ -48,14 +50,20 @@ public class AdminController {
|
||||||
@GetMapping(value = {"", "/index"})
|
@GetMapping(value = {"", "/index"})
|
||||||
public String admin(Model model) {
|
public String admin(Model model) {
|
||||||
|
|
||||||
Page<PostSimpleOutputDTO> postPage = postService.listLatest(10);
|
Page<PostSimpleOutputDTO> postPage = postService.listLatest(5);
|
||||||
|
|
||||||
|
Page<CommentVO> commentPage = commentService.listLatest(5);
|
||||||
|
|
||||||
|
Page<LogOutputDTO> logPage = logService.listLatest(5);
|
||||||
|
|
||||||
model.addAttribute("postsCount", postPage.getTotalElements());
|
model.addAttribute("postsCount", postPage.getTotalElements());
|
||||||
model.addAttribute("commentsCount", commentService.count());
|
model.addAttribute("commentsCount", commentPage.getTotalElements());
|
||||||
model.addAttribute("attachmentsCount", attachmentService.count());
|
model.addAttribute("attachmentsCount", attachmentService.count());
|
||||||
|
|
||||||
model.addAttribute("latestPosts", postPage.getContent());
|
model.addAttribute("latestPosts", postPage.getContent());
|
||||||
model.addAttribute("latestLogs", logService.listLatest(10).getContent());
|
model.addAttribute("latestLogs", logPage.getContent());
|
||||||
|
model.addAttribute("latestComments", commentPage.getContent());
|
||||||
|
|
||||||
return "admin/admin_index";
|
return "admin/admin_index";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue