Add additional counts to return

pull/137/head
johnniang 2019-03-30 00:38:04 +08:00
parent 943d8f0865
commit 4b6887acd9
8 changed files with 52 additions and 11 deletions

View File

@ -11,11 +11,17 @@ import lombok.Data;
@Data @Data
public class CountOutputDTO { public class CountOutputDTO {
private Long postCount; private long postCount;
private Long commentCount; private long commentCount;
private Long attachmentCount; private long attachmentCount;
private Long establishDays; private long establishDays;
private long linkCount;
private long visitCount;
private long likeCount;
} }

View File

@ -12,7 +12,7 @@ import javax.persistence.Entity;
* @author johnniang * @author johnniang
* @date 3/22/19 * @date 3/22/19
*/ */
@Entity(name = "journal") @Entity(name = "Journal")
@Where(clause = "deleted = false") @Where(clause = "deleted = false")
@SQLDelete(sql = "update posts set deleted = true where id = ?") @SQLDelete(sql = "update posts set deleted = true where id = ?")
@DiscriminatorValue("2") @DiscriminatorValue("2")

View File

@ -12,7 +12,7 @@ import javax.persistence.Entity;
* @author johnniang * @author johnniang
* @date 3/22/19 * @date 3/22/19
*/ */
@Entity(name = "page") @Entity(name = "Page")
@Where(clause = "deleted = false") @Where(clause = "deleted = false")
@SQLDelete(sql = "update posts set deleted = true where id = ?") @SQLDelete(sql = "update posts set deleted = true where id = ?")
@DiscriminatorValue("1") @DiscriminatorValue("1")

View File

@ -11,7 +11,7 @@ import javax.persistence.Entity;
* *
* @author johnniang * @author johnniang
*/ */
@Entity(name = "post") @Entity(name = "Post")
@SQLDelete(sql = "update posts set deleted = true where id = ?") @SQLDelete(sql = "update posts set deleted = true where id = ?")
@Where(clause = "deleted = false") @Where(clause = "deleted = false")
@DiscriminatorValue(value = "0") @DiscriminatorValue(value = "0")

View File

@ -3,6 +3,7 @@ package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.repository.base.BasePostRepository; import cc.ryanc.halo.repository.base.BasePostRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
/** /**
@ -13,4 +14,9 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
*/ */
public interface PostRepository extends BasePostRepository<Post>, JpaSpecificationExecutor<Post> { public interface PostRepository extends BasePostRepository<Post>, JpaSpecificationExecutor<Post> {
@Query("select sum(p.visits) from Post p")
Long countVisit();
@Query("select sum(p.likes) from Post p")
Long countLike();
} }

View File

@ -4,7 +4,6 @@ import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO;
import cc.ryanc.halo.model.dto.post.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.model.enums.PostStatus; import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.vo.PostDetailVO; import cc.ryanc.halo.model.vo.PostDetailVO;
import cc.ryanc.halo.model.vo.PostListVO; import cc.ryanc.halo.model.vo.PostListVO;
import cc.ryanc.halo.service.base.CrudService; import cc.ryanc.halo.service.base.CrudService;
@ -131,4 +130,18 @@ public interface PostService extends CrudService<Post, Integer> {
*/ */
@NonNull @NonNull
PostDetailVO getDetailVoBy(@NonNull Integer postId); PostDetailVO getDetailVoBy(@NonNull Integer postId);
/**
* Counts visit total number.
*
* @return visit total number
*/
long countVisit();
/**
* Counts like total number.
*
* @return like total number
*/
long countLike();
} }

View File

@ -8,7 +8,6 @@ import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO;
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO; import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.model.entity.*; import cc.ryanc.halo.model.entity.*;
import cc.ryanc.halo.model.enums.PostStatus; import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.vo.PostDetailVO; import cc.ryanc.halo.model.vo.PostDetailVO;
import cc.ryanc.halo.model.vo.PostListVO; import cc.ryanc.halo.model.vo.PostListVO;
import cc.ryanc.halo.repository.PostRepository; import cc.ryanc.halo.repository.PostRepository;
@ -244,6 +243,16 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return convertTo(post); return convertTo(post);
} }
@Override
public long countVisit() {
return Optional.ofNullable(postRepository.countVisit()).orElse(0L);
}
@Override
public long countLike() {
return Optional.ofNullable(postRepository.countLike()).orElse(0L);
}
@Override @Override
@Transactional @Transactional
public Post removeById(Integer postId) { public Post removeById(Integer postId) {
@ -254,7 +263,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
// Remove post tags // Remove post tags
List<PostTag> postTags = postTagService.removeByPostId(postId); List<PostTag> postTags = postTagService.removeByPostId(postId);
log.debug("Removd post tags: [{}]", postTags); log.debug("Removed post tags: [{}]", postTags);
// Remove post categories // Remove post categories
List<PostCategory> postCategories = postCategoryService.removeByPostId(postId); List<PostCategory> postCategories = postCategoryService.removeByPostId(postId);

View File

@ -38,16 +38,20 @@ public class AdminController {
private final UserService userService; private final UserService userService;
private final LinkService linkService;
public AdminController(PostService postService, public AdminController(PostService postService,
AttachmentService attachmentService, AttachmentService attachmentService,
CommentService commentService, CommentService commentService,
OptionService optionService, OptionService optionService,
UserService userService) { UserService userService,
LinkService linkService) {
this.postService = postService; this.postService = postService;
this.attachmentService = attachmentService; this.attachmentService = attachmentService;
this.commentService = commentService; this.commentService = commentService;
this.optionService = optionService; this.optionService = optionService;
this.userService = userService; this.userService = userService;
this.linkService = linkService;
} }
@GetMapping("counts") @GetMapping("counts")
@ -58,6 +62,9 @@ public class AdminController {
countOutputDTO.setAttachmentCount(attachmentService.count()); countOutputDTO.setAttachmentCount(attachmentService.count());
countOutputDTO.setCommentCount(commentService.count()); countOutputDTO.setCommentCount(commentService.count());
countOutputDTO.setEstablishDays(Long.valueOf(optionService.getByProperty(BlogProperties.WIDGET_DAYCOUNT).orElse("0"))); countOutputDTO.setEstablishDays(Long.valueOf(optionService.getByProperty(BlogProperties.WIDGET_DAYCOUNT).orElse("0")));
countOutputDTO.setLinkCount(linkService.count());
countOutputDTO.setVisitCount(postService.countVisit());
countOutputDTO.setLikeCount(postService.countLike());
return countOutputDTO; return countOutputDTO;
} }