提交 5f6a8ba7 编写于 作者: J johnniang

Refactor journal entity, repository, service and controller

上级 f99efc89
......@@ -4,11 +4,13 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.BaseCommentDTO;
import run.halo.app.model.dto.JournalDTO;
import run.halo.app.model.dto.JournalWithCmtCountDTO;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.params.JournalParam;
import run.halo.app.service.JournalService;
import javax.validation.Valid;
import java.util.List;
/**
......@@ -29,21 +31,21 @@ public class JournalController {
@GetMapping
@ApiOperation("Gets latest journals")
public Page<BaseCommentDTO> pageBy(Pageable pageable) {
Page<Journal> journalPage = journalService.pageBy(pageable);
return journalService.convertTo(journalPage);
public Page<JournalWithCmtCountDTO> pageBy(Pageable pageable) {
Page<Journal> journalPage = journalService.listAll(pageable);
return journalService.convertToCmtCountDto(journalPage);
}
@GetMapping("latest")
@ApiOperation("Gets latest journals")
public List<BaseCommentDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
public List<JournalWithCmtCountDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
List<Journal> journals = journalService.pageLatest(top).getContent();
return journalService.convertTo(journals);
return journalService.convertToCmtCountDto(journals);
}
@PostMapping
@ApiOperation("Creates a journal")
public BaseCommentDTO createBy(@RequestBody JournalParam journalParam) {
public JournalDTO createBy(@RequestBody @Valid JournalParam journalParam) {
Journal createdJournal = journalService.createBy(journalParam);
return journalService.convertTo(createdJournal);
}
......
package run.halo.app.model.dto;
import lombok.Data;
import run.halo.app.model.dto.base.OutputConverter;
import run.halo.app.model.entity.Journal;
/**
* Journal dto.
*
* @author johnniang
* @date 19-4-24
*/
public class JournalDTO extends BaseCommentDTO {
@Data
public class JournalDTO implements OutputConverter<JournalDTO, Journal> {
private Integer id;
private String content;
private Long likes;
}
package run.halo.app.model.dto;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
/**
* Journal with comment count dto.
*
* @author johnniang
* @date 19-4-25
*/
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class JournalWithCmtCountDTO extends JournalDTO {
private Long commentCount;
}
package run.halo.app.model.entity;
import run.halo.app.model.enums.PostCreateFrom;
import run.halo.app.model.enums.PostStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import run.halo.app.model.enums.PostCreateFrom;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.utils.MarkdownUtils;
import javax.persistence.*;
......@@ -143,10 +143,6 @@ public class BasePost extends BaseEntity {
thumbnail = "";
}
if (visits == null) {
visits = 0L;
}
if (disallowComment == null) {
disallowComment = false;
}
......@@ -167,7 +163,11 @@ public class BasePost extends BaseEntity {
createFrom = PostCreateFrom.ADMIN;
}
if (likes == null) {
if (visits == null || visits < 0) {
visits = 0L;
}
if (likes == null || likes < 0) {
likes = 0L;
}
}
......
package run.halo.app.model.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.*;
/**
* Journal entity
......@@ -9,17 +14,33 @@ import javax.persistence.Entity;
* @author johnniang
* @date 3/22/19
*/
@Entity(name = "Journal")
@DiscriminatorValue("2")
public class Journal extends BaseComment {
@Entity
@Table(name = "journals")
@SQLDelete(sql = "update journals set deleted = true where id = ?")
@Where(clause = "deleted = false")
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class Journal extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "content", columnDefinition = "varchar(1023) not null")
private String content;
@Column(name = "likes", columnDefinition = "bigint default 0")
private Long likes;
@Override
public void prePersist() {
super.prePersist();
if (getPostId() == null) {
setPostId(0);
id = null;
if (likes == null || likes < 0) {
likes = 0L;
}
}
}
......@@ -4,8 +4,6 @@ import lombok.Data;
import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Journal;
import javax.validation.constraints.Email;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
......@@ -18,22 +16,7 @@ import javax.validation.constraints.Size;
@Data
public class JournalParam implements InputConverter<Journal> {
@NotBlank(message = "Author name must not be blank")
@Size(max = 50, message = "Length of comment author name must not be more than {max}")
private String author;
@NotBlank(message = "Email must not be blank")
@Email(message = "Email's format is incorrect")
@Size(max = 255, message = "Length of comment email must not be more than {max}")
private String email;
@Size(max = 127, message = "Length of comment author url must not be more than {max}")
private String authorUrl;
@NotBlank(message = "Content must not be blank")
@Size(max = 511, message = "Length of comment content must not be more than {max}")
private String content;
@Min(value = 0, message = "Parent id must not be less than {value}")
private Long parentId = 0L;
}
package run.halo.app.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.projection.CommentCountProjection;
import run.halo.app.repository.base.BaseCommentRepository;
import java.util.List;
import run.halo.app.repository.base.BaseRepository;
/**
* Journal repository.
......@@ -16,24 +9,6 @@ import java.util.List;
* @author johnniang
* @date 3/22/19
*/
public interface JournalRepository extends BaseCommentRepository<Journal> {
/**
* Counts comment count by post id collection.
*
* @param postIds post id collection must not be null
* @return a list of comment count
*/
@Query("select new run.halo.app.model.projection.CommentCountProjection(count(comment.id), comment.postId) from Journal comment where comment.postId in ?1 group by comment.postId")
@NonNull
List<CommentCountProjection> countByPostIds(@NonNull Iterable<Integer> postIds);
public interface JournalRepository extends BaseRepository<Journal, Integer> {
/**
* Finds all journals by parent id.
*
* @param parentId parent id must not be null
* @param pageable page info must not be null
* @return a page of journal
*/
Page<Journal> findAllByParentId(@NonNull Long parentId, @NonNull Pageable pageable);
}
......@@ -3,9 +3,14 @@ package run.halo.app.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.model.dto.JournalDTO;
import run.halo.app.model.dto.JournalWithCmtCountDTO;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.params.JournalParam;
import run.halo.app.service.base.BaseCommentService;
import run.halo.app.service.base.CrudService;
import java.util.List;
/**
* Journal service interface.
......@@ -13,7 +18,7 @@ import run.halo.app.service.base.BaseCommentService;
* @author johnniang
* @date 19-4-24
*/
public interface JournalService extends BaseCommentService<Journal> {
public interface JournalService extends CrudService<Journal, Integer> {
/**
* Creates a journal.
......@@ -25,10 +30,46 @@ public interface JournalService extends BaseCommentService<Journal> {
Journal createBy(@NonNull JournalParam journalParam);
/**
* Gets a page of journal
* Gets latest journals.
*
* @param top max size
* @return latest journal page
*/
Page<Journal> pageLatest(int top);
/**
* Converts to journal dto.
*
* @param pageable page info must not be null
* @return a page of journal
* @param journal journal must not be null
* @return journal dto
*/
Page<Journal> pageBy(@NonNull Pageable pageable);
@NonNull
JournalDTO convertTo(@NonNull Journal journal);
/**
* Converts to journal with comment count dto.
*
* @param journal journal must not be null
* @return journal with comment count dto
*/
@NonNull
JournalWithCmtCountDTO convertToCmtCountDto(@NonNull Journal journal);
/**
* Converts to journal with comment count dto list.
*
* @param journals journal list
* @return journal with comment count dto list
*/
@NonNull
List<JournalWithCmtCountDTO> convertToCmtCountDto(@Nullable List<Journal> journals);
/**
* Converts to journal with comment count dto page.
*
* @param journalPage journal page must not be null
* @return a page of journal with comment count dto
*/
@NonNull
Page<JournalWithCmtCountDTO> convertToCmtCountDto(@NonNull Page<Journal> journalPage);
}
......@@ -5,7 +5,6 @@ import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
......@@ -78,7 +77,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
@Override
public Page<COMMENT> pageLatest(int top) {
return listAll(buildLatestPageable(top));
return listAll(ServiceUtils.buildLatestPageable(top));
}
@Override
......@@ -417,16 +416,4 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
}
}
/**
* Builds latest page request.
*
* @param top top must not be less than 1
* @return latest page request
*/
@NonNull
Pageable buildLatestPageable(int top) {
Assert.isTrue(top > 0, "Top number must not be less than 0");
return PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "createTime"));
}
}
package run.halo.app.service.impl;
import cn.hutool.core.lang.Assert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import run.halo.app.exception.ForbiddenException;
import run.halo.app.model.dto.JournalDTO;
import run.halo.app.model.dto.JournalWithCmtCountDTO;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.entity.User;
import run.halo.app.model.params.JournalParam;
import run.halo.app.model.properties.BlogProperties;
import run.halo.app.repository.JournalRepository;
import run.halo.app.repository.PostRepository;
import run.halo.app.security.authentication.Authentication;
import run.halo.app.security.context.SecurityContextHolder;
import run.halo.app.service.JournalService;
import run.halo.app.service.OptionService;
import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.ServiceUtils;
import run.halo.app.utils.ValidationUtils;
import java.util.List;
/**
* Journal service implementation.
......@@ -27,15 +22,12 @@ import run.halo.app.utils.ValidationUtils;
* @date 19-4-24
*/
@Service
public class JournalServiceImpl extends BaseCommentServiceImpl<Journal> implements JournalService {
public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> implements JournalService {
private final JournalRepository journalRepository;
public JournalServiceImpl(JournalRepository journalRepository,
PostRepository postRepository,
OptionService optionService,
ApplicationEventPublisher eventPublisher) {
super(journalRepository, postRepository, optionService, eventPublisher);
public JournalServiceImpl(JournalRepository journalRepository) {
super(journalRepository);
this.journalRepository = journalRepository;
}
......@@ -43,40 +35,36 @@ public class JournalServiceImpl extends BaseCommentServiceImpl<Journal> implemen
public Journal createBy(JournalParam journalParam) {
Assert.notNull(journalParam, "Journal param must not be null");
// Check user login status
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
// Get user detail
User user = authentication.getDetail().getUser();
// Set some default value
journalParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
journalParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
journalParam.setEmail(user.getEmail());
} else {
// Guest comment
if (ServiceUtils.isEmptyId(journalParam.getParentId())) {
throw new ForbiddenException("You have no right to create a journal");
}
}
// Validate the journal param
ValidationUtils.validate(journalParam);
// Convert, create and return
return createBy(journalParam.convertTo());
return create(journalParam.convertTo());
}
@Override
public Page<Journal> pageBy(Pageable pageable) {
Assert.notNull(pageable, "Page info must not be null");
public Page<Journal> pageLatest(int top) {
return listAll(ServiceUtils.buildLatestPageable(top));
}
return journalRepository.findAllByParentId(0L, pageable);
@Override
public JournalDTO convertTo(Journal journal) {
Assert.notNull(journal, "Journal must not be null");
return new JournalDTO().convertFrom(journal);
}
@Override
public Page<Journal> pageLatest(int top) {
return pageBy(buildLatestPageable(top));
public JournalWithCmtCountDTO convertToCmtCountDto(Journal journal) {
return null;
}
@Override
public List<JournalWithCmtCountDTO> convertToCmtCountDto(List<Journal> journals) {
return null;
}
@Override
public Page<JournalWithCmtCountDTO> convertToCmtCountDto(Page<Journal> journalPage) {
return null;
}
}
package run.halo.app.utils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
......@@ -116,4 +120,30 @@ public class ServiceUtils {
public static boolean isEmptyId(@Nullable Number id) {
return id == null || id.longValue() <= 0;
}
/**
* Builds latest page request.
*
* @param top top must not be less than 1
* @return latest page request
*/
@NonNull
public static Pageable buildLatestPageable(int top) {
return buildLatestPageable(top, "createTime");
}
/**
* Builds latest page request.
*
* @param top top must not be less than 1
* @param sortProperty sort property must not be blank
* @return latest page request
*/
@NonNull
public static Pageable buildLatestPageable(int top, @NonNull String sortProperty) {
Assert.isTrue(top > 0, "Top number must not be less than 0");
Assert.hasText(sortProperty, "Sort property must not be blank");
return PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, sortProperty));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册