mirror of https://github.com/halo-dev/halo
complete list latest posts service
parent
6b06ac4264
commit
5a1327ade7
|
@ -0,0 +1,24 @@
|
|||
package cc.ryanc.halo.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Post detail output dto.
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class PostDetailOutputDTO extends PostSimpleOutputDTO {
|
||||
|
||||
/**
|
||||
* 源内容
|
||||
*/
|
||||
private String originalContent;
|
||||
|
||||
/**
|
||||
* 渲染后内容
|
||||
*/
|
||||
private String formatContent;
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
package cc.ryanc.halo.model.dto;
|
||||
|
||||
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.PostType;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Page simple output dto.
|
||||
*
|
||||
* @author johnniang
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
public class PostSimpleOutputDTO implements OutputConverter<PostSimpleOutputDTO, Post> {
|
||||
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 文章标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 文章类型
|
||||
* 0: 普通文章
|
||||
* 1: 自定义页面
|
||||
* 2: 日志
|
||||
*/
|
||||
private PostType type;
|
||||
|
||||
/**
|
||||
* 摘要
|
||||
*/
|
||||
private String summary;
|
||||
|
||||
/**
|
||||
* 缩略图
|
||||
*/
|
||||
private String thumbnail;
|
||||
|
||||
/**
|
||||
* 浏览量
|
||||
*/
|
||||
private Long visits;
|
||||
|
||||
/**
|
||||
* 是否允许评论
|
||||
*/
|
||||
private Boolean disallowComment;
|
||||
|
||||
/**
|
||||
* 文章密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 自定义渲染模板名称
|
||||
*/
|
||||
private String template;
|
||||
|
||||
/**
|
||||
* 是否置顶
|
||||
*/
|
||||
private Integer topPriority;
|
||||
|
||||
/**
|
||||
* 发布来源
|
||||
*/
|
||||
private PostCreateFrom createFrom;
|
||||
|
||||
/**
|
||||
* 点赞量/喜欢量
|
||||
*/
|
||||
private Long likes;
|
||||
|
||||
/**
|
||||
* 创建时间戳
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间戳
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* Edit time.
|
||||
*/
|
||||
private Date editTime;
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package cc.ryanc.halo.model.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.Where;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
@ -14,6 +16,8 @@ import java.util.Date;
|
|||
@Data
|
||||
@Entity
|
||||
@Table(name = "attachments")
|
||||
@SQLDelete(sql = "update attachments set deleted = true where id = ?")
|
||||
@Where(clause = "deleted = false")
|
||||
public class Attachment {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cc.ryanc.halo.model.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.Where;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
@ -14,6 +16,8 @@ import java.util.Date;
|
|||
@Data
|
||||
@Entity
|
||||
@Table(name = "galleries")
|
||||
@SQLDelete(sql = "update galleries set deleted = true where id = ?")
|
||||
@Where(clause = "deleted = false")
|
||||
public class Gallery {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cc.ryanc.halo.model.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.Where;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
@ -14,6 +16,8 @@ import java.util.Date;
|
|||
@Data
|
||||
@Entity
|
||||
@Table(name = "links")
|
||||
@SQLDelete(sql = "update links set deleted = true where id = ?")
|
||||
@Where(clause = "deleted = false")
|
||||
public class Link {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cc.ryanc.halo.model.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.Where;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
@ -14,6 +16,8 @@ import java.util.Date;
|
|||
@Data
|
||||
@Entity
|
||||
@Table(name = "menus")
|
||||
@SQLDelete(sql = "update menus set deleted = true where id = ?")
|
||||
@Where(clause = "deleted = false")
|
||||
public class Menu {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -110,6 +110,13 @@ public class Post {
|
|||
@Column(name = "likes", columnDefinition = "bigint default 0")
|
||||
private Long likes;
|
||||
|
||||
/**
|
||||
* Edit time.
|
||||
*/
|
||||
@Column(name = "edit_time", columnDefinition = "timestamp default CURRENT_TIMESTAMP")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date editTime;
|
||||
|
||||
/**
|
||||
* 创建时间戳
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cc.ryanc.halo.model.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.Where;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
@ -14,6 +16,8 @@ import java.util.Date;
|
|||
@Data
|
||||
@Entity
|
||||
@Table(name = "menus")
|
||||
@SQLDelete(sql = "update menus set deleted = true where id = ?")
|
||||
@Where(clause = "deleted = false")
|
||||
public class Tag {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cc.ryanc.halo.model.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.Where;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
@ -14,6 +16,8 @@ import java.util.Date;
|
|||
@Data
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
@SQLDelete(sql = "update users set deleted = true where id = ?")
|
||||
@Where(clause = "deleted = false")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.dto.PostSimpleOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Post;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
/**
|
||||
* Post service.
|
||||
|
@ -10,4 +13,13 @@ import cc.ryanc.halo.service.base.CrudService;
|
|||
*/
|
||||
public interface PostService extends CrudService<Post, Integer> {
|
||||
|
||||
/**
|
||||
* Lists latest posts.
|
||||
*
|
||||
* @param top top number must not be less than 0
|
||||
* @return latest posts
|
||||
*/
|
||||
@NonNull
|
||||
Page<PostSimpleOutputDTO> listLatest(int top);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
package cc.ryanc.halo.service.impl;
|
||||
|
||||
import cc.ryanc.halo.model.dto.PostSimpleOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Post;
|
||||
import cc.ryanc.halo.repository.PostRepository;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||
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.util.Assert;
|
||||
|
||||
/**
|
||||
* Post service implementation.
|
||||
|
@ -20,4 +25,15 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
|||
super(postRepository);
|
||||
this.postRepository = postRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PostSimpleOutputDTO> listLatest(int top) {
|
||||
Assert.isTrue(top > 0, "Top number must not be less than 0");
|
||||
|
||||
PageRequest latestPageable = PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "editTime"));
|
||||
|
||||
Page<Post> posts = listAll(latestPageable);
|
||||
|
||||
return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package cc.ryanc.halo.web.controller.admin;
|
||||
|
||||
import cc.ryanc.halo.model.dto.PostSimpleOutputDTO;
|
||||
import cc.ryanc.halo.service.AttachmentService;
|
||||
import cc.ryanc.halo.service.CommentService;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -40,13 +42,14 @@ public class AdminController {
|
|||
*/
|
||||
@GetMapping(value = {"", "/index"})
|
||||
public String admin(Model model) {
|
||||
final Long postsCount = postService.count();
|
||||
final Long commentsCount = commentService.count();
|
||||
final Long attachmentsCount = attachmentService.count();
|
||||
|
||||
model.addAttribute("postsCount", postsCount);
|
||||
model.addAttribute("commentsCount", commentsCount);
|
||||
model.addAttribute("attachmentsCount", attachmentsCount);
|
||||
Page<PostSimpleOutputDTO> postPage = postService.listLatest(10);
|
||||
|
||||
model.addAttribute("postsCount", postPage.getTotalElements());
|
||||
model.addAttribute("commentsCount", commentService.count());
|
||||
model.addAttribute("attachmentsCount", attachmentService.count());
|
||||
|
||||
model.addAttribute("latestPosts", postPage.getContent());
|
||||
return "admin/admin_index";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue