Create ContentCategoryController and ContentIndexController

pull/137/head
ruibaby 2019-03-20 10:55:17 +08:00
parent e6982a22bc
commit fc2349314d
5 changed files with 165 additions and 24 deletions

View File

@ -116,7 +116,7 @@ public class ThemeUtils {
* @param theme theme * @param theme theme
* @return List<String> * @return List<String>
*/ */
public static List<String> getTplName(String theme) { public static List<String> getTemplates(String theme) {
final List<String> templates = new ArrayList<>(); final List<String> templates = new ArrayList<>();
try { try {
final File themesPath = new File(getThemesPath(theme), theme); final File themesPath = new File(getThemesPath(theme), theme);

View File

@ -27,6 +27,4 @@ public class ThemeController {
public List<Theme> listAll() { public List<Theme> listAll() {
return ThemeUtils.getThemes(); return ThemeUtils.getThemes();
} }
} }

View File

@ -0,0 +1,78 @@
package cc.ryanc.halo.web.controller.content;
import cc.ryanc.halo.model.entity.Category;
import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
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.RequestMapping;
import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* @author : RYAN0UP
* @date : 2019/3/20
*/
@Controller
@RequestMapping(value = "/categories")
public class ContentCategoryController extends BaseContentController {
private CategoryService categoryService;
private PostService postService;
public ContentCategoryController(CategoryService categoryService,
PostService postService) {
this.categoryService = categoryService;
this.postService = postService;
}
/**
* Render category list page
*
* @param model model
* @return template path: /{theme}/categories.ftl
*/
@GetMapping
public String categories(Model model) {
final List<Category> categories = categoryService.listAll();
model.addAttribute("categories", categories);
return this.render("categories");
}
/**
* Render post list page by category
*
* @param model model
* @param slugName slugName
* @return template path: /{theme}/category.ftl
*/
@GetMapping(value = "{slugName}")
public String categories(Model model,
@PathVariable("slugName") String slugName) {
return this.categories(model, slugName, 1, Sort.by(DESC, "postDate"));
}
/**
* Render post list page by category
*
* @param model model
* @param slugName slugName
* @param page current page number
* @return template path: /{theme}/category.ftl
*/
@GetMapping("{slugName}/page/{page}")
public String categories(Model model,
@PathVariable("slugName") String slugName,
@PathVariable("page") Integer page,
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
return "";
}
}

View File

@ -0,0 +1,86 @@
package cc.ryanc.halo.web.controller.content;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.vo.PostListVO;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
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.web.SortDefault;
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.RequestMapping;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Blog index page controller
*
* @author : RYAN0UP
* @date : 2019-03-17
*/
@Slf4j
@Controller
@RequestMapping
public class ContentIndexController extends BaseContentController {
private PostService postService;
public ContentIndexController(PostService postService) {
this.postService = postService;
}
/**
* Render blog index
*
* @param model model
* @return template path: /{theme}/post.ftl
*/
@GetMapping
public String index(Model model) {
return this.index(model, 1, Sort.by(DESC, "topPriority").and(Sort.by(DESC, "createTime")));
}
/**
* Render blog index
*
* @param model model
* @param page current page number
* @return template path: /{theme}/post.ftl
*/
@GetMapping(value = "page/{page}")
public String index(Model model,
@PathVariable(value = "page") Integer page,
@SortDefault.SortDefaults({
@SortDefault(sort = "topPriority", direction = DESC),
@SortDefault(sort = "createTime", direction = DESC)
}) Sort sort) {
log.debug("Requested index page, sort info: [{}]", sort);
int size = 10;
if (StrUtil.isNotBlank(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue()))) {
size = Integer.parseInt(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue()));
}
final Pageable pageable = PageRequest.of(page - 1, size, sort);
final Page<PostListVO> posts = postService.pageListVoBy(PostStatus.PUBLISHED, PostType.POST, pageable);
if (null == posts) {
return this.renderNotFound();
}
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_index", true);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
return this.render("index");
}
}

View File

@ -1,21 +0,0 @@
package cc.ryanc.halo.web.controller.content;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Blog index page controller
*
* @author : RYAN0UP
* @date : 2019-03-17
*/
@Controller
@RequestMapping
public class IndexController {
@GetMapping(value = "/sweetalert")
public String sweetalert(){
return "sweetalert";
}
}