Complete Sheet base structure

pull/146/head
johnniang 2019-04-24 18:33:08 +08:00
parent 822a362a45
commit d256ef7653
27 changed files with 441 additions and 80 deletions

View File

@ -5,8 +5,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
import run.halo.app.model.dto.post.PostSimpleOutputDTO;
import run.halo.app.model.dto.post.PostMinimalDTO;
import run.halo.app.model.dto.post.PostSimpleDTO;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostParam;
@ -62,15 +62,15 @@ public class PostController {
@GetMapping("latest")
@ApiOperation("Pages latest post")
public List<PostMinimalOutputDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
public List<PostMinimalDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
return postService.pageLatestOfMinimal(top).getContent();
}
@GetMapping("status/{status}")
@ApiOperation("Gets a page of post by post status")
public Page<? extends PostSimpleOutputDTO> pageByStatus(@PathVariable(name = "status") PostStatus status,
@RequestParam(value = "more", required = false, defaultValue = "false") Boolean more,
@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
public Page<? extends PostSimpleDTO> pageByStatus(@PathVariable(name = "status") PostStatus status,
@RequestParam(value = "more", required = false, defaultValue = "false") Boolean more,
@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
if (more) {
return postService.pageListVoBy(status, pageable);
}

View File

@ -0,0 +1,77 @@
package run.halo.app.controller.admin.api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.SheetParam;
import run.halo.app.service.SheetService;
import javax.validation.Valid;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Sheet controller.
*
* @author johnniang
* @date 19-4-24
*/
@RestController
@RequestMapping("/api/admin/sheets")
public class SheetController {
private final SheetService sheetService;
public SheetController(SheetService sheetService) {
this.sheetService = sheetService;
}
@GetMapping("{sheetId:\\d+}")
@ApiOperation("Gets a sheet")
public Sheet getBy(@PathVariable("sheetId") Integer sheetId) {
return sheetService.getById(sheetId);
}
@GetMapping
@ApiOperation("Gets a page of sheet")
public Page<Sheet> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
return sheetService.pageBy(pageable);
}
@PostMapping
@ApiOperation("Creates a sheet")
public Sheet createBy(@RequestBody @Valid SheetParam sheetParam) {
return sheetService.createBy(sheetParam.convertTo());
}
@PutMapping("{sheetId:\\d+}")
@ApiOperation("Updates a sheet")
public Sheet updateBy(
@PathVariable("sheetId") Integer sheetId,
@RequestBody @Valid SheetParam sheetParam) {
return sheetService.updateBy(sheetParam.convertTo());
}
@PutMapping("{sheetId:\\d+}/{status}")
public Sheet updateStatusBy(
@PathVariable("sheetId") Integer sheetId,
@PathVariable("status") PostStatus status) {
Sheet sheet = sheetService.getById(sheetId);
// Set status
sheet.setStatus(status);
// Update and return
return sheetService.update(sheet);
}
@DeleteMapping("{sheetId:\\d+}")
@ApiOperation("Deletes a sheet")
public Sheet deleteBy(@PathVariable("sheetId") Integer sheetId) {
return sheetService.removeById(sheetId);
}
}

View File

@ -1,7 +1,7 @@
package run.halo.app.controller.content.api;
import run.halo.app.model.dto.post.PostDetailOutputDTO;
import run.halo.app.model.dto.post.PostSimpleOutputDTO;
import run.halo.app.model.dto.post.PostDetailDTO;
import run.halo.app.model.dto.post.PostSimpleDTO;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.CommentVO;
import run.halo.app.model.vo.CommentWithParentVO;
@ -16,14 +16,6 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.post.PostDetailOutputDTO;
import run.halo.app.model.dto.post.PostSimpleOutputDTO;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.CommentVO;
import run.halo.app.model.vo.CommentWithParentVO;
import run.halo.app.service.CommentService;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;
import static org.springframework.data.domain.Sort.Direction.DESC;
@ -53,16 +45,16 @@ public class PostController {
@GetMapping
@ApiOperation("Lists posts")
public Page<PostSimpleOutputDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
public Page<PostSimpleDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
return postService.pageSimpleDtoByStatus(PostStatus.PUBLISHED, pageable);
}
@GetMapping("{postId:\\d+}")
@ApiOperation("Gets a post")
public PostDetailOutputDTO getBy(@PathVariable("postId") Integer postId,
@RequestParam(value = "formatDisabled", required = false, defaultValue = "true") Boolean formatDisabled,
@RequestParam(value = "sourceDisabled", required = false, defaultValue = "false") Boolean sourceDisabled) {
PostDetailOutputDTO postDetail = new PostDetailOutputDTO().convertFrom(postService.getById(postId));
public PostDetailDTO getBy(@PathVariable("postId") Integer postId,
@RequestParam(value = "formatDisabled", required = false, defaultValue = "true") Boolean formatDisabled,
@RequestParam(value = "sourceDisabled", required = false, defaultValue = "false") Boolean sourceDisabled) {
PostDetailDTO postDetail = new PostDetailDTO().convertFrom(postService.getById(postId));
if (formatDisabled) {
// Clear the format content

View File

@ -1,7 +1,7 @@
package run.halo.app.controller.content.api;
import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.dto.post.PostSimpleOutputDTO;
import run.halo.app.model.dto.post.PostSimpleDTO;
import run.halo.app.model.entity.Tag;
import run.halo.app.service.PostTagService;
import run.halo.app.service.TagService;
@ -50,12 +50,12 @@ public class TagController {
@GetMapping("{slugName}/posts")
@ApiOperation("Lists posts by tag slug name")
public Page<PostSimpleOutputDTO> listPostsBy(@PathVariable("slugName") String slugName,
@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
public Page<PostSimpleDTO> listPostsBy(@PathVariable("slugName") String slugName,
@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
// Get tag by slug name
Tag tag = tagService.getBySlugNameOfNonNull(slugName);
// Get posts, convert and return
return postTagService.pagePostsBy(tag.getId(), pageable).map(post -> new PostSimpleOutputDTO().convertFrom(post));
return postTagService.pagePostsBy(tag.getId(), pageable).map(post -> new PostSimpleDTO().convertFrom(post));
}
}

View File

@ -12,7 +12,7 @@ import lombok.ToString;
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class PostDetailOutputDTO extends PostSimpleOutputDTO {
public class PostDetailDTO extends PostSimpleDTO {
private String originalContent;

View File

@ -1,16 +1,12 @@
package run.halo.app.model.dto.post;
import run.halo.app.model.dto.base.OutputConverter;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.BasePost;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.enums.PostType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import run.halo.app.model.dto.base.OutputConverter;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.enums.PostType;
import java.util.Date;
@ -22,7 +18,7 @@ import java.util.Date;
@Data
@ToString
@EqualsAndHashCode
public class PostMinimalOutputDTO implements OutputConverter<PostMinimalOutputDTO, Post> {
public class PostMinimalDTO implements OutputConverter<PostMinimalDTO, BasePost> {
private Integer id;

View File

@ -1,7 +1,5 @@
package run.halo.app.model.dto.post;
import run.halo.app.model.enums.PostCreateFrom;
import run.halo.app.model.enums.PostType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@ -16,7 +14,7 @@ import run.halo.app.model.enums.PostType;
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class PostSimpleOutputDTO extends PostMinimalOutputDTO {
public class PostSimpleDTO extends PostMinimalDTO {
private PostType type;

View File

@ -0,0 +1,11 @@
package run.halo.app.model.dto.post;
/**
* Simple detail dto.
*
* @author johnniang
* @date 19-4-24
*/
public class SheetDetailDTO extends PostDetailDTO {
}

View File

@ -0,0 +1,10 @@
package run.halo.app.model.dto.post;
/**
* Sheet minimal dto
*
* @author johnniang
* @date 19-4-24
*/
public class SheetMinimalDTO extends PostMinimalDTO {
}

View File

@ -0,0 +1,10 @@
package run.halo.app.model.dto.post;
/**
* Sheet simple dto.
*
* @author johnniang
* @date 19-4-24
*/
public class SheetSimpleDTO extends PostSimpleDTO {
}

View File

@ -12,4 +12,5 @@ import javax.persistence.Entity;
@Entity(name = "Sheet")
@DiscriminatorValue("1")
public class Sheet extends BasePost {
}

View File

@ -1,9 +1,8 @@
package run.halo.app.model.params;
import org.hibernate.validator.constraints.URL;
import lombok.Data;
import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Comment;
import lombok.Data;
import javax.validation.constraints.Email;
import javax.validation.constraints.Min;

View File

@ -23,26 +23,26 @@ import java.util.Set;
@Data
public class PostParam implements InputConverter<Post> {
@NotBlank(message = "Post title must not be blank")
@Size(max = 100, message = "Length of post title must not be more than {max}")
@NotBlank(message = "Title must not be blank")
@Size(max = 100, message = "Length of title must not be more than {max}")
private String title;
private PostStatus status = PostStatus.DRAFT;
private String url;
@NotBlank(message = "Post original content must not be blank")
@NotBlank(message = "Original content must not be blank")
private String originalContent;
@Size(max = 255, message = "Length of post thumbnail must not be more than {max}")
@Size(max = 255, message = "Length of thumbnail must not be more than {max}")
private String thumbnail;
private Boolean disallowComment = false;
@Size(max = 255, message = "Length of post password must not be more than {max}")
@Size(max = 255, message = "Length of password must not be more than {max}")
private String password;
@Size(max = 255, message = "Length of post template must not be more than {max}")
@Size(max = 255, message = "Length of template must not be more than {max}")
private String template;
@Min(value = 0, message = "Post top priority must not be less than {value}")
@ -70,9 +70,6 @@ public class PostParam implements InputConverter<Post> {
post.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
}
// Set post type to
// post.setType(PostType.POST);
return post;
}
@ -92,6 +89,5 @@ public class PostParam implements InputConverter<Post> {
if (StringUtils.isNotBlank(password)) {
post.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
}
}
}

View File

@ -0,0 +1,82 @@
package run.halo.app.model.params;
import cn.hutool.crypto.digest.BCrypt;
import org.apache.commons.lang3.StringUtils;
import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.utils.HaloUtils;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
/**
* @author johnniang
* @date 19-4-24
*/
public class SheetParam implements InputConverter<Sheet> {
@NotBlank(message = "Title must not be blank")
@Size(max = 100, message = "Length of title must not be more than {max}")
private String title;
private PostStatus status = PostStatus.DRAFT;
private String url;
@NotBlank(message = "Original content must not be blank")
private String originalContent;
@Size(max = 255, message = "Length of thumbnail must not be more than {max}")
private String thumbnail;
private Boolean disallowComment = false;
@Size(max = 255, message = "Length of password must not be more than {max}")
private String password;
@NotBlank(message = "Template must not be blank")
@Size(max = 255, message = "Length of template must not be more than {max}")
private String template;
@Min(value = 0, message = "Post top priority must not be less than {value}")
private Integer topPriority = 0;
@Override
public Sheet convertTo() {
if (StringUtils.isBlank(url)) {
url = HaloUtils.normalizeUrl(title);
} else {
url = HaloUtils.normalizeUrl(url);
}
url = HaloUtils.initializeUrlIfBlank(url);
Sheet sheet = InputConverter.super.convertTo();
// Crypt password
if (StringUtils.isNotBlank(password)) {
sheet.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
}
return sheet;
}
@Override
public void update(Sheet sheet) {
if (StringUtils.isBlank(url)) {
url = HaloUtils.normalizeUrl(title);
} else {
url = HaloUtils.normalizeUrl(url);
}
url = HaloUtils.initializeUrlIfBlank(url);
InputConverter.super.update(sheet);
// Crypt password
if (StringUtils.isNotBlank(password)) {
sheet.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
}
}
}

View File

@ -1,6 +1,6 @@
package run.halo.app.model.vo;
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
import run.halo.app.model.dto.post.PostMinimalDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@ -21,7 +21,7 @@ public class ArchiveYearVO {
private Integer year;
private List<PostMinimalOutputDTO> posts;
private List<PostMinimalDTO> posts;
public static class ArchiveComparator implements Comparator<ArchiveYearVO> {

View File

@ -1,7 +1,7 @@
package run.halo.app.model.vo;
import run.halo.app.model.dto.CommentDTO;
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
import run.halo.app.model.dto.post.PostMinimalDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@ -16,5 +16,5 @@ import lombok.ToString;
@EqualsAndHashCode(callSuper = true)
public class CommentWithPostVO extends CommentDTO {
private PostMinimalOutputDTO post;
private PostMinimalDTO post;
}

View File

@ -1,9 +1,8 @@
package run.halo.app.model.vo;
import run.halo.app.model.dto.post.PostDetailOutputDTO;
import run.halo.app.model.dto.post.PostDetailDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import run.halo.app.model.dto.post.PostDetailOutputDTO;
import java.util.Set;
@ -15,7 +14,7 @@ import java.util.Set;
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class PostDetailVO extends PostDetailOutputDTO {
public class PostDetailVO extends PostDetailDTO {
private Set<Integer> tagIds;

View File

@ -2,7 +2,7 @@ package run.halo.app.model.vo;
import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.dto.post.PostSimpleOutputDTO;
import run.halo.app.model.dto.post.PostSimpleDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -16,7 +16,7 @@ import java.util.List;
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class PostListVO extends PostSimpleOutputDTO {
public class PostListVO extends PostSimpleDTO {
private Long commentCount;

View File

@ -1,12 +1,12 @@
package run.halo.app.repository.base;
import run.halo.app.model.entity.BasePost;
import run.halo.app.model.enums.PostStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.lang.NonNull;
import run.halo.app.model.entity.BasePost;
import run.halo.app.model.enums.PostStatus;
import java.util.List;
import java.util.Optional;

View File

@ -10,4 +10,5 @@ import run.halo.app.service.base.CrudService;
* @date 19-4-24
*/
public interface JournalService extends CrudService<Journal, Long> {
}

View File

@ -4,8 +4,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import org.springframework.transaction.annotation.Transactional;
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
import run.halo.app.model.dto.post.PostSimpleOutputDTO;
import run.halo.app.model.dto.post.PostMinimalDTO;
import run.halo.app.model.dto.post.PostSimpleDTO;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostQuery;
@ -35,7 +35,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @return latest posts of minimal
*/
@NonNull
Page<PostMinimalOutputDTO> pageLatestOfMinimal(int top);
Page<PostMinimalDTO> pageLatestOfMinimal(int top);
/**
@ -45,7 +45,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @return latest posts of simple
*/
@NonNull
Page<PostSimpleOutputDTO> pageLatestOfSimple(int top);
Page<PostSimpleDTO> pageLatestOfSimple(int top);
/**
@ -92,10 +92,10 @@ public interface PostService extends CrudService<Post, Integer> {
*
* @param status post status must not be null
* @param pageable page info must not be null
* @return Page<PostSimpleOutputDTO>
* @return Page<PostSimpleDTO>
*/
@NonNull
Page<PostSimpleOutputDTO> pageSimpleDtoByStatus(@NonNull PostStatus status, @NonNull Pageable pageable);
Page<PostSimpleDTO> pageSimpleDtoByStatus(@NonNull PostStatus status, @NonNull Pageable pageable);
/**
* Lists page list vo by status and pageable.
@ -238,7 +238,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @return a page of post simple output dto
*/
@NonNull
Page<PostSimpleOutputDTO> convertToSimpleDto(@NonNull Page<Post> postPage);
Page<PostSimpleDTO> convertToSimpleDto(@NonNull Page<Post> postPage);
/**
* Converts to a page of post list vo.

View File

@ -1,5 +1,9 @@
package run.halo.app.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import run.halo.app.model.dto.post.SheetDetailDTO;
import run.halo.app.model.entity.Sheet;
import run.halo.app.service.base.CrudService;
@ -11,4 +15,39 @@ import run.halo.app.service.base.CrudService;
*/
public interface SheetService extends CrudService<Sheet, Integer> {
/**
* Creates a sheet.
*
* @param sheet sheet must not be null
* @return created sheet
*/
@NonNull
Sheet createBy(@NonNull Sheet sheet);
/**
* Updates a sheet.
*
* @param sheet sheet must not be null
* @return updated sheet
*/
@NonNull
Sheet updateBy(@NonNull Sheet sheet);
/**
* Gets a page of sheet.
*
* @param pageable page info must not be null
* @return a page of sheet
*/
@NonNull
Page<Sheet> pageBy(@NonNull Pageable pageable);
/**
* Converts to detail dto.
*
* @param sheet sheet must not be null
* @return sheet detail dto
*/
@NonNull
SheetDetailDTO convertToDetailDto(@NonNull Sheet sheet);
}

View File

@ -18,7 +18,7 @@ import run.halo.app.event.comment.CommentNewEvent;
import run.halo.app.event.comment.CommentPassEvent;
import run.halo.app.event.comment.CommentReplyEvent;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
import run.halo.app.model.dto.post.PostMinimalDTO;
import run.halo.app.model.entity.Comment;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.User;
@ -440,7 +440,7 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
CommentWithPostVO commentWithPostVO = new CommentWithPostVO().convertFrom(comment);
// Get post and set to the vo
commentWithPostVO.setPost(new PostMinimalOutputDTO().convertFrom(postMap.get(comment.getPostId())));
commentWithPostVO.setPost(new PostMinimalDTO().convertFrom(postMap.get(comment.getPostId())));
return commentWithPostVO;
}).collect(Collectors.toList());

View File

@ -0,0 +1,22 @@
package run.halo.app.service.impl;
import run.halo.app.model.entity.Journal;
import run.halo.app.repository.JournalRepository;
import run.halo.app.service.JournalService;
import run.halo.app.service.base.AbstractCrudService;
/**
* Journal service implementation.
*
* @author johnniang
* @date 19-4-24
*/
public class JournalServiceImpl extends AbstractCrudService<Journal, Long> implements JournalService {
private final JournalRepository journalRepository;
public JournalServiceImpl(JournalRepository journalRepository) {
super(journalRepository);
this.journalRepository = journalRepository;
}
}

View File

@ -20,8 +20,8 @@ import run.halo.app.exception.BadRequestException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
import run.halo.app.model.dto.post.PostSimpleOutputDTO;
import run.halo.app.model.dto.post.PostMinimalDTO;
import run.halo.app.model.dto.post.PostSimpleDTO;
import run.halo.app.model.entity.*;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.PostStatus;
@ -90,13 +90,13 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
}
@Override
public Page<PostMinimalOutputDTO> pageLatestOfMinimal(int top) {
return pageLatest(top).map(post -> new PostMinimalOutputDTO().convertFrom(post));
public Page<PostMinimalDTO> pageLatestOfMinimal(int top) {
return pageLatest(top).map(post -> new PostMinimalDTO().convertFrom(post));
}
@Override
public Page<PostSimpleOutputDTO> pageLatestOfSimple(int top) {
return pageLatest(top).map(post -> new PostSimpleOutputDTO().convertFrom(post));
public Page<PostSimpleDTO> pageLatestOfSimple(int top) {
return pageLatest(top).map(post -> new PostSimpleDTO().convertFrom(post));
}
@Override
@ -184,11 +184,11 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
*
* @param status status
* @param pageable pageable
* @return Page<PostSimpleOutputDTO>
* @return Page<PostSimpleDTO>
*/
@Override
public Page<PostSimpleOutputDTO> pageSimpleDtoByStatus(PostStatus status, Pageable pageable) {
return pageBy(status, pageable).map(post -> new PostSimpleOutputDTO().convertFrom(post));
public Page<PostSimpleDTO> pageSimpleDtoByStatus(PostStatus status, Pageable pageable) {
return pageBy(status, pageable).map(post -> new PostSimpleDTO().convertFrom(post));
}
@Override
@ -465,10 +465,10 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
}
@Override
public Page<PostSimpleOutputDTO> convertToSimpleDto(@NonNull Page<Post> postPage) {
public Page<PostSimpleDTO> convertToSimpleDto(@NonNull Page<Post> postPage) {
Assert.notNull(postPage, "Post page must not be null");
return postPage.map(post -> new PostSimpleOutputDTO().convertFrom(post));
return postPage.map(post -> new PostSimpleDTO().convertFrom(post));
}
@Override
@ -579,14 +579,14 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* @return a list of post minimal output dto
*/
@NonNull
private List<PostMinimalOutputDTO> convertTo(@NonNull List<Post> posts) {
private List<PostMinimalDTO> convertTo(@NonNull List<Post> posts) {
if (CollectionUtils.isEmpty(posts)) {
return Collections.emptyList();
}
// Convert
return posts.stream()
.map(post -> new PostMinimalOutputDTO().<PostMinimalOutputDTO>convertFrom(post))
.map(post -> new PostMinimalDTO().<PostMinimalDTO>convertFrom(post))
.collect(Collectors.toList());
}

View File

@ -0,0 +1,22 @@
package run.halo.app.service.impl;
import run.halo.app.model.entity.SheetComment;
import run.halo.app.repository.SheetCommentRepository;
import run.halo.app.service.SheetCommentService;
import run.halo.app.service.base.AbstractCrudService;
/**
* Sheet comment service implementation.
*
* @author johnniang
* @date 19-4-24
*/
public class SheetCommentServiceImpl extends AbstractCrudService<SheetComment, Long> implements SheetCommentService {
private final SheetCommentRepository sheetCommentRepository;
public SheetCommentServiceImpl(SheetCommentRepository sheetCommentRepository) {
super(sheetCommentRepository);
this.sheetCommentRepository = sheetCommentRepository;
}
}

View File

@ -0,0 +1,106 @@
package run.halo.app.service.impl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import run.halo.app.exception.AlreadyExistsException;
import run.halo.app.model.dto.post.SheetDetailDTO;
import run.halo.app.model.entity.Sheet;
import run.halo.app.repository.SheetRepository;
import run.halo.app.service.SheetService;
import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.DateUtils;
import run.halo.app.utils.MarkdownUtils;
import run.halo.app.utils.ServiceUtils;
/**
* Sheet service implementation.
*
* @author johnniang
* @date 19-4-24
*/
@Service
public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implements SheetService {
private final SheetRepository sheetRepository;
public SheetServiceImpl(SheetRepository sheetRepository) {
super(sheetRepository);
this.sheetRepository = sheetRepository;
}
@Override
public Sheet createBy(Sheet sheet) {
return createOrUpdateBy(sheet);
}
@Override
public Sheet updateBy(Sheet sheet) {
return createOrUpdateBy(sheet);
}
@Override
public Page<Sheet> pageBy(Pageable pageable) {
Assert.notNull(pageable, "Page info must not be null");
return listAll(pageable);
}
@Override
public SheetDetailDTO convertToDetailDto(Sheet sheet) {
Assert.notNull(sheet, "Sheet must not be null");
// Convert and return
return new SheetDetailDTO().convertFrom(sheet);
}
@NonNull
private Sheet createOrUpdateBy(@NonNull Sheet sheet) {
Assert.notNull(sheet, "Sheet must not be null");
// Check url
urlMustNotExist(sheet);
// Render content
sheet.setFormatContent(MarkdownUtils.renderMarkdown(sheet.getOriginalContent()));
// Create or update post
if (ServiceUtils.isEmptyId(sheet.getId())) {
// The sheet will be created
return create(sheet);
}
// The sheet will be updated
// Set edit time
sheet.setEditTime(DateUtils.now());
// Update it
return update(sheet);
}
/**
* Check if the url is exist.
*
* @param sheet sheet must not be null
*/
private void urlMustNotExist(@NonNull Sheet sheet) {
Assert.notNull(sheet, "Sheet must not be null");
// TODO Refactor this method with BasePostService
// TODO May refactor these queries
// Get url count
long count;
if (ServiceUtils.isEmptyId(sheet.getId())) {
// The sheet will be created
count = sheetRepository.countByUrl(sheet.getUrl());
} else {
// The sheet will be updated
count = sheetRepository.countByIdNotAndUrl(sheet.getId(), sheet.getUrl());
}
if (count > 0) {
throw new AlreadyExistsException("The sheet url has been exist");
}
}
}