Render sheet page.

pull/146/head
ruibaby 2019-04-24 19:01:31 +08:00
parent d256ef7653
commit 511b952b00
4 changed files with 71 additions and 49 deletions

View File

@ -1,20 +1,17 @@
package run.halo.app.controller.content;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.Comment;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.CommentService;
import run.halo.app.service.PostService;
import run.halo.app.service.SheetService;
import run.halo.app.service.ThemeService;
import java.util.List;
/**
* @author : RYAN0UP
* @date : 2019-03-21
@ -23,16 +20,16 @@ import java.util.List;
public class ContentSheetController {
private final PostService postService;
private final SheetService sheetService;
private final CommentService commentService;
private final ThemeService themeService;
public ContentSheetController(PostService postService,
public ContentSheetController(SheetService sheetService,
CommentService commentService,
ThemeService themeService) {
this.postService = postService;
this.sheetService = sheetService;
this.commentService = commentService;
this.themeService = themeService;
}
@ -40,7 +37,7 @@ public class ContentSheetController {
/**
* Render gallery page
*
* @return template path: themes/{theme}/gallery
* @return template path: themes/{theme}/gallery.ftl
*/
@GetMapping(value = "/gallery")
public String gallery() {
@ -50,7 +47,7 @@ public class ContentSheetController {
/**
* Render links page
*
* @return template path: themes/{theme}/links
* @return template path: themes/{theme}/links.ftl
*/
@GetMapping(value = "/links")
public String links() {
@ -58,50 +55,23 @@ public class ContentSheetController {
}
/**
* Render custom page
* Render custom sheet
*
* @param url page url
* @param url sheet url
* @param model model
* @return template path: themes/{theme}/post
* @return template path: themes/{theme}/sheet.ftl
*/
@GetMapping(value = "/s/{url}")
public String sheet(@PathVariable(value = "url") String url,
@RequestParam(value = "cp", defaultValue = "1") Integer cp,
Model model) {
final Post post = postService.getByUrl(url);
final Sheet sheet = sheetService.getBy(PostStatus.PUBLISHED, url);
if (!post.getStatus().equals(PostStatus.PUBLISHED)) {
throw new NotFoundException("The post isn't published").setErrorData(url);
model.addAttribute("sheet", sheet);
if (StrUtil.isNotEmpty(sheet.getTemplate())) {
return themeService.render(sheet.getTemplate());
}
List<Comment> comments;
// TODO Complete this api
// if (StrUtil.equals(OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()), "true") || OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()) == null) {
// comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getValue());
// } else {
// comments = commentService.findCommentsByPostAndCommentStatusNot(post, CommentStatusEnum.RECYCLE.getCode());
// }
// //默认显示10条
// int size = 10;
// if (StrUtil.isNotBlank(OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()))) {
// size = Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
// }
// //评论分页
// final ListPage<Comment> commentsPage = new ListPage<>(CommentUtil.getComments(comments), cp, size);
// final int[] rainbow = PageUtil.rainbow(cp, commentsPage.getTotalPage(), 3);
// model.addAttribute("is_page", true);
// model.addAttribute("post", post);
// model.addAttribute("comments", commentsPage);
// model.addAttribute("commentsCount", comments.size());
// model.addAttribute("rainbow", rainbow);
// postService.cacheViews(post.getPostId());
//
// //如果设置了自定义模板,则渲染自定义模板
// if (StrUtil.isNotEmpty(post.getCustomTpl())) {
// return this.render(post.getCustomTpl());
// }
return themeService.render("sheet");
}
}

View File

@ -1,8 +1,13 @@
package run.halo.app.repository;
import org.springframework.lang.NonNull;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.repository.base.BasePostRepository;
import java.util.Optional;
/**
* Sheet repository.
*
@ -11,4 +16,6 @@ import run.halo.app.repository.base.BasePostRepository;
*/
public interface SheetRepository extends BasePostRepository<Sheet> {
@NonNull
Optional<Sheet> getByUrlAndStatus(@NonNull String url, @NonNull PostStatus status);
}

View File

@ -5,6 +5,7 @@ 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.model.enums.PostStatus;
import run.halo.app.service.base.CrudService;
/**
@ -50,4 +51,14 @@ public interface SheetService extends CrudService<Sheet, Integer> {
*/
@NonNull
SheetDetailDTO convertToDetailDto(@NonNull Sheet sheet);
/**
* Gets sheet by post status and url.
*
* @param status post status must not be null
* @param url sheet url must not be blank
* @return sheet info
*/
@NonNull
Sheet getBy(@NonNull PostStatus status, @NonNull String url);
}

View File

@ -1,13 +1,18 @@
package run.halo.app.service.impl;
import org.springframework.context.ApplicationEventPublisher;
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.event.post.VisitEvent;
import run.halo.app.exception.AlreadyExistsException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.post.SheetDetailDTO;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.repository.SheetRepository;
import run.halo.app.service.SheetService;
import run.halo.app.service.base.AbstractCrudService;
@ -15,6 +20,8 @@ import run.halo.app.utils.DateUtils;
import run.halo.app.utils.MarkdownUtils;
import run.halo.app.utils.ServiceUtils;
import java.util.Optional;
/**
* Sheet service implementation.
*
@ -25,10 +32,13 @@ import run.halo.app.utils.ServiceUtils;
public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implements SheetService {
private final SheetRepository sheetRepository;
private final ApplicationEventPublisher eventPublisher;
public SheetServiceImpl(SheetRepository sheetRepository) {
public SheetServiceImpl(SheetRepository sheetRepository,
ApplicationEventPublisher eventPublisher) {
super(sheetRepository);
this.sheetRepository = sheetRepository;
this.eventPublisher = eventPublisher;
}
@Override
@ -48,6 +58,30 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
return listAll(pageable);
}
/**
* Gets sheet by post status and url.
*
* @param status post status must not be null
* @param url sheet url must not be blank
* @return sheet info
*/
@Override
public Sheet getBy(PostStatus status, String url) {
Assert.notNull(status, "Post status must not be null");
Assert.hasText(url, "Sheet url must not be blank");
Optional<Sheet> sheetOptional = sheetRepository.getByUrlAndStatus(url, status);
Sheet sheet = sheetOptional.orElseThrow(() -> new NotFoundException("The sheet with status " + status + " and url " + url + "was not existed").setErrorData(url));
if (PostStatus.PUBLISHED.equals(status)) {
// Log it
eventPublisher.publishEvent(new VisitEvent(this, sheet.getId()));
}
return sheet;
}
@Override
public SheetDetailDTO convertToDetailDto(Sheet sheet) {
Assert.notNull(sheet, "Sheet must not be null");