Refactor Post entity for including Page and Journal entity

pull/137/head
johnniang 2019-03-22 23:50:17 +08:00
parent 31f237b594
commit 8575bcb2ad
18 changed files with 332 additions and 172 deletions

View File

@ -25,6 +25,7 @@ import java.nio.charset.Charset;
import java.util.List;
import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME;
import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;
/**
* <pre>
@ -120,7 +121,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
*/
private void cacheOwo() {
try {
File file = new File(ResourceUtils.getURL("classpath:").getPath(), "static/halo-common/OwO/OwO.path.json");
File file = new File(ResourceUtils.getURL(CLASSPATH_URL_PREFIX).getPath(), "static/halo-common/OwO/OwO.path.json");
HaloConst.OWO = JSONUtil.readJSONObject(file, Charset.forName("UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();

View File

@ -0,0 +1,127 @@
package cc.ryanc.halo.model.entity;
import cc.ryanc.halo.model.enums.PostCreateFrom;
import cc.ryanc.halo.model.enums.PostStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.persistence.*;
import java.util.Date;
/**
* Post entity.
*
* @author johnniang
*/
@Entity(name = "base_post")
@Table(name = "posts")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER, columnDefinition = "int default 0")
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class BasePost extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
*
*/
@Column(name = "title", columnDefinition = "varchar(100) not null")
private String title;
/**
* Post status.
*/
@Column(name = "status", columnDefinition = "int default 1")
private PostStatus status;
/**
* Post url.
*/
@Column(name = "url", columnDefinition = "varchar(255) not null")
private String url;
/**
*
*/
@Column(name = "original_content", columnDefinition = "text not null")
private String originalContent;
/**
*
*/
@Column(name = "format_content", columnDefinition = "text not null")
private String formatContent;
/**
*
*/
@Column(name = "summary", columnDefinition = "varchar(500) default ''")
private String summary;
/**
*
*/
@Column(name = "thumbnail", columnDefinition = "varchar(1023) default ''")
private String thumbnail;
/**
*
*/
@Column(name = "visits", columnDefinition = "bigint default 0")
private Long visits;
/**
*
*/
@Column(name = "disallow_comment", columnDefinition = "int default 0")
private Boolean disallowComment;
/**
*
*/
@Column(name = "password", columnDefinition = "varchar(255) default ''")
private String password;
/**
*
*/
@Column(name = "template", columnDefinition = "varchar(255) default ''")
private String template;
/**
*
*/
@Column(name = "top_priority", columnDefinition = "int default 0")
private Integer topPriority;
/**
*
*/
@Column(name = "create_from", columnDefinition = "int default 0")
private PostCreateFrom createFrom;
/**
* /
*/
@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;
@Override
public void prePersist() {
super.prePersist();
id = null;
editTime = getCreateTime();
}
}

View File

@ -0,0 +1,21 @@
package cc.ryanc.halo.model.entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* Journal entity
*
* @author johnniang
* @date 3/22/19
*/
@Entity(name = "journal")
@Where(clause = "deleted = false")
@SQLDelete(sql = "update posts set deleted = true where id = ?")
@DiscriminatorValue("2")
public class Journal extends BasePost {
}

View File

@ -0,0 +1,20 @@
package cc.ryanc.halo.model.entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* Page entity.
*
* @author johnniang
* @date 3/22/19
*/
@Entity(name = "page")
@Where(clause = "deleted = false")
@SQLDelete(sql = "update posts set deleted = true where id = ?")
@DiscriminatorValue("1")
public class Page extends BasePost {
}

View File

@ -1,143 +1,20 @@
package cc.ryanc.halo.model.entity;
import cc.ryanc.halo.model.enums.PostCreateFrom;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.*;
import java.util.Date;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* Post entity.
*
* @author johnniang
*/
@Entity
@Table(name = "posts")
@Entity(name = "post")
@SQLDelete(sql = "update posts set deleted = true where id = ?")
@Where(clause = "deleted = false")
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class Post extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
*
*/
@Column(name = "title", columnDefinition = "varchar(100) not null")
private String title;
/**
*
* 0:
* 1:
* 2:
*/
@Column(name = "type", columnDefinition = "int default 0")
private PostType type;
/**
* Post status.
*/
@Column(name = "status", columnDefinition = "int default 1")
private PostStatus status;
/**
* Post url.
*/
@Column(name = "url", columnDefinition = "varchar(255) not null")
private String url;
/**
*
*/
@Column(name = "original_content", columnDefinition = "text not null")
private String originalContent;
/**
*
*/
@Column(name = "format_content", columnDefinition = "text not null")
private String formatContent;
/**
*
*/
@Column(name = "summary", columnDefinition = "varchar(500) default ''")
private String summary;
/**
*
*/
@Column(name = "thumbnail", columnDefinition = "varchar(1023) default ''")
private String thumbnail;
/**
*
*/
@Column(name = "visits", columnDefinition = "bigint default 0")
private Long visits;
/**
*
*/
@Column(name = "disallow_comment", columnDefinition = "int default 0")
private Boolean disallowComment;
/**
*
*/
@Column(name = "password", columnDefinition = "varchar(255) default ''")
private String password;
/**
*
*/
@Column(name = "template", columnDefinition = "varchar(255) default ''")
private String template;
/**
*
*/
@Column(name = "top_priority", columnDefinition = "int default 0")
private Integer topPriority;
/**
*
*/
@Column(name = "create_from", columnDefinition = "int default 0")
private PostCreateFrom createFrom;
/**
* /
*/
@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;
@Override
public void prePersist() {
super.prePersist();
id = null;
editTime = getCreateTime();
if (type == null) {
type = PostType.POST;
}
}
@DiscriminatorValue(value = "0")
public class Post extends BasePost {
}

View File

@ -72,7 +72,7 @@ public class PostParam implements InputConverter<Post> {
}
// Set post type to
post.setType(PostType.POST);
// post.setType(PostType.POST);
return post;
}

View File

@ -0,0 +1,14 @@
package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Journal;
import cc.ryanc.halo.repository.base.BaseRepository;
/**
* Journal repository.
*
* @author johnniang
* @date 3/22/19
*/
public interface JournalRepository extends BaseRepository<Journal, Integer> {
}

View File

@ -0,0 +1,14 @@
package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Page;
import cc.ryanc.halo.repository.base.BaseRepository;
/**
* Page repository.
*
* @author johnniang
* @date 3/22/19
*/
public interface PageRepository extends BaseRepository<Page, Integer> {
}

View File

@ -2,7 +2,6 @@ package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -24,21 +23,19 @@ public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecif
* Finds posts by status and type.
*
* @param status status
* @param type type
* @param pageable pageable
* @return Page<Post>
*/
@NonNull
Page<Post> findAllByStatusAndType(@NonNull PostStatus status, @NonNull PostType type, @NonNull Pageable pageable);
Page<Post> findAllByStatus(@NonNull PostStatus status, @NonNull Pageable pageable);
/**
* Counts posts by status and type.
*
* @param status status
* @param type type
* @return posts count
*/
long countByStatusAndType(@NonNull PostStatus status, @NonNull PostType type);
long countByStatus(@NonNull PostStatus status);
/**
* Count by post url.
@ -60,9 +57,8 @@ public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecif
/**
* Get post by url
*
* @param url post url
* @param type post type enum
* @param url post url
* @return Optional<Post>
*/
Optional<Post> getByUrlAndType(@NonNull String url, @NonNull PostType type);
Optional<Post> getByUrl(@NonNull String url);
}

View File

@ -56,44 +56,40 @@ public interface PostService extends CrudService<Post, Integer> {
* List by status and type
*
* @param status post status must not be null
* @param type post type must not be null
* @param pageable page info must not be null
* @return Page<PostSimpleOutputDTO>
*/
@NonNull
Page<Post> pageBy(@NonNull PostStatus status, @NonNull PostType type, @NonNull Pageable pageable);
Page<Post> pageBy(@NonNull PostStatus status, @NonNull Pageable pageable);
/**
* List simple output dto by status and type
*
* @param status post status must not be null
* @param type post type must not be null
* @param pageable page info must not be null
* @return Page<PostSimpleOutputDTO>
*/
@NonNull
Page<PostSimpleOutputDTO> pageSimpleDtoByStatus(@NonNull PostStatus status, @NonNull PostType type, @NonNull Pageable pageable);
Page<PostSimpleOutputDTO> pageSimpleDtoByStatus(@NonNull PostStatus status, @NonNull Pageable pageable);
/**
* Lists page list vo by status, type and pageable.
*
* @param status post status must not be null
* @param type post type must not be null
* @param pageable page info must not be null
* @return a page of page list vo
*/
@NonNull
Page<PostListVO> pageListVoBy(@NonNull PostStatus status, @NonNull PostType type, @NonNull Pageable pageable);
Page<PostListVO> pageListVoBy(@NonNull PostStatus status, @NonNull Pageable pageable);
/**
* Count posts by status and type
*
* @param status status
* @param type type
* @return posts count
*/
Long countByStatus(PostStatus status, PostType type);
Long countByStatus(PostStatus status);
/**
* Creates post by post param.
@ -122,11 +118,10 @@ public interface PostService extends CrudService<Post, Integer> {
/**
* Get post by url.
*
* @param url post url.
* @param type post type enum.
* @param url post url.
* @return Post
*/
Post getByUrl(@NonNull String url, @NonNull PostType type);
Post getByUrl(@NonNull String url);
/**
* Get post detail vo by post id.

View File

@ -90,30 +90,28 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
}
@Override
public Page<Post> pageBy(PostStatus status, PostType type, Pageable pageable) {
public Page<Post> pageBy(PostStatus status, Pageable pageable) {
Assert.notNull(status, "Post status must not be null");
Assert.notNull(type, "Post type must not be null");
Assert.notNull(pageable, "Page info must not be null");
return postRepository.findAllByStatusAndType(status, type, pageable);
return postRepository.findAllByStatus(status, pageable);
}
/**
* List by status and type
*
* @param status status
* @param type type
* @param pageable pageable
* @return Page<PostSimpleOutputDTO>
*/
@Override
public Page<PostSimpleOutputDTO> pageSimpleDtoByStatus(PostStatus status, PostType type, Pageable pageable) {
return pageBy(status, type, pageable).map(post -> new PostSimpleOutputDTO().convertFrom(post));
public Page<PostSimpleOutputDTO> pageSimpleDtoByStatus(PostStatus status, Pageable pageable) {
return pageBy(status, pageable).map(post -> new PostSimpleOutputDTO().convertFrom(post));
}
@Override
public Page<PostListVO> pageListVoBy(PostStatus status, PostType type, Pageable pageable) {
Page<Post> postPage = pageBy(status, type, pageable);
public Page<PostListVO> pageListVoBy(PostStatus status, Pageable pageable) {
Page<Post> postPage = pageBy(status, pageable);
List<Post> posts = postPage.getContent();
@ -159,12 +157,11 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* Counts posts by status and type
*
* @param status status
* @param type type
* @return posts count
*/
@Override
public Long countByStatus(PostStatus status, PostType type) {
return postRepository.countByStatusAndType(status, type);
public Long countByStatus(PostStatus status) {
return postRepository.countByStatus(status);
}
@Override
@ -229,13 +226,12 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
/**
* Gets post by url.
*
* @param url post url.
* @param type post type enum.
* @param url post url.
* @return Post
*/
@Override
public Post getByUrl(String url, PostType type) {
return postRepository.getByUrlAndType(url, type).orElseThrow(() -> new NotFoundException("The post does not exist").setErrorData(url));
public Post getByUrl(String url) {
return postRepository.getByUrl(url).orElseThrow(() -> new NotFoundException("The post does not exist").setErrorData(url));
}
@Override

View File

@ -42,7 +42,7 @@ public class AdminController {
@ApiOperation("Gets count info")
public CountOutputDTO getCount() {
CountOutputDTO countOutputDTO = new CountOutputDTO();
countOutputDTO.setPostCount(postService.countByStatus(PostStatus.PUBLISHED, PostType.POST));
countOutputDTO.setPostCount(postService.countByStatus(PostStatus.PUBLISHED));
countOutputDTO.setAttachmentCount(attachmentService.count());
countOutputDTO.setCommentCount(commentService.count());
countOutputDTO.setEstablishDays(Long.valueOf(optionService.getByProperty(BlogProperties.WIDGET_DAYCOUNT).orElse("0")));

View File

@ -55,9 +55,9 @@ public class PostController {
@RequestParam(value = "more_info", required = false, defaultValue = "false") Boolean moreInfo,
@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
if (moreInfo) {
return postService.pageListVoBy(status, PostType.POST, pageable);
return postService.pageListVoBy(status, pageable);
}
return postService.pageSimpleDtoByStatus(status, PostType.POST, pageable);
return postService.pageSimpleDtoByStatus(status, pageable);
}
@GetMapping("{postId:\\d+}")

View File

@ -131,7 +131,7 @@ public class ContentFeedController {
* @return List<Post>
*/
private List<Post> buildPosts(Pageable pageable) {
final Page<Post> postsPage = postService.pageBy(PostStatus.PUBLISHED, PostType.POST, pageable).map(post -> {
final Page<Post> postsPage = postService.pageBy(PostStatus.PUBLISHED, pageable).map(post -> {
if (StrUtil.isNotEmpty(post.getPassword())) {
post.setFormatContent("该文章为加密文章");
post.setSummary("该文章为加密文章");

View File

@ -71,7 +71,7 @@ public class ContentIndexController extends BaseContentController {
log.debug("Requested index page, sort info: [{}]", sort);
int pageSize = optionService.getPostPageSize();
Pageable pageable = PageRequest.of(page - 1, pageSize, sort);
Page<PostListVO> posts = postService.pageListVoBy(PostStatus.PUBLISHED, PostType.POST, pageable);
Page<PostListVO> posts = postService.pageListVoBy(PostStatus.PUBLISHED, pageable);
int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_index", true);
model.addAttribute("posts", posts);

View File

@ -61,15 +61,15 @@ public class ContentPageController extends BaseContentController {
/**
* Render custom page
*
* @param url page url
* @param model model
* @param url page url
* @param model model
* @return template path: themes/{theme}/post
*/
@GetMapping(value = "/p/{url}")
public String getPage(@PathVariable(value = "url") String url,
@RequestParam(value = "cp", defaultValue = "1") Integer cp,
Model model) {
final Post post = postService.getByUrl(url, PostType.POST);
final Post post = postService.getByUrl(url);
if (null == post || !post.getStatus().equals(PostStatus.PUBLISHED)) {
return this.renderNotFound();
}

View File

@ -0,0 +1,67 @@
server:
port: 8090
use-forward-headers: true
undertow:
io-threads: 2
worker-threads: 36
buffer-size: 1024
directBuffers: true
servlet:
session:
timeout: 86400s
spring:
output:
ansi:
enabled: always
datasource:
type: com.zaxxer.hikari.HikariDataSource
# H2database 配置
driver-class-name: org.h2.Driver
url: jdbc:h2:file:~/halo-test/db/halo
username: admin
password: 123456
#MySql配置
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/halodb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
# username: root
# password: 123456
h2:
console:
settings:
web-allow-others: true
path: /h2-console
enabled: true
jpa:
hibernate:
ddl-auto: update
show-sql: true
freemarker:
allow-request-override: false
cache: false
check-template-location: true
charset: utf-8
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: true
suffix: .ftl
settings:
auto_import: /spring.ftl as spring
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
cache:
type: none
logging:
level:
cc.ryanc.halo: DEBUG
org.hibernate: ERROR
file: ./logs/log.log
halo:
doc-disabled: false

View File

@ -0,0 +1,32 @@
package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* Page repository test.
*
* @author johnniang
* @date 3/22/19
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class PageRepositoryTest {
@Autowired
private PageRepository pageRepository;
@Test
public void listAllTest() {
List<Page> allPages = pageRepository.findAll();
System.out.println(allPages);
}
}