feat: support set categories and tags mapping prefix.

pull/472/head
ruibaby 2020-01-11 20:30:57 +08:00
parent 62153403df
commit de796b35d6
7 changed files with 196 additions and 242 deletions

View File

@ -7,7 +7,6 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import run.halo.app.cache.StringCacheStore;
import run.halo.app.cache.lock.CacheLock;
import run.halo.app.controller.content.model.PostModel;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.OptionService;
@ -34,39 +33,12 @@ public class ContentArchiveController {
private final StringCacheStore cacheStore;
private final PostModel postModel;
public ContentArchiveController(PostService postService,
OptionService optionService,
StringCacheStore cacheStore,
PostModel postModel) {
StringCacheStore cacheStore) {
this.postService = postService;
this.optionService = optionService;
this.cacheStore = cacheStore;
this.postModel = postModel;
}
/**
* Render post archives page.
*
* @param model model
* @return template path : themes/{theme}/archives.ftl
*/
@GetMapping
public String archives(Model model) {
return this.archives(model, 1);
}
/**
* Render post archives page.
*
* @param model model
* @return template path : themes/{theme}/archives.ftl
*/
@GetMapping(value = "page/{page}")
public String archives(Model model,
@PathVariable(value = "page") Integer page) {
return postModel.list(page, model, "is_archives", "archives");
}
@GetMapping(value = "{url}/password")

View File

@ -1,104 +0,0 @@
package run.halo.app.controller.content;
import cn.hutool.core.util.PageUtil;
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 run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Category controller.
*
* @author ryanwang
* @date 2019-03-20
*/
@Controller
@RequestMapping(value = "/categories")
public class ContentCategoryController {
private final CategoryService categoryService;
private final ThemeService themeService;
private final PostCategoryService postCategoryService;
private final PostService postService;
private final OptionService optionService;
public ContentCategoryController(CategoryService categoryService,
ThemeService themeService,
PostCategoryService postCategoryService,
PostService postService, OptionService optionService) {
this.categoryService = categoryService;
this.themeService = themeService;
this.postCategoryService = postCategoryService;
this.postService = postService;
this.optionService = optionService;
}
/**
* Render category list page
*
* @return template path: themes/{theme}/categories.ftl
*/
@GetMapping
public String categories(Model model) {
model.addAttribute("is_categories", true);
return themeService.render("categories");
}
/**
* Render post list page by category
*
* @param model model
* @param slugName slugName
* @return template path: themes/{theme}/category.ftl
*/
@GetMapping(value = "{slugName}")
public String categories(Model model,
@PathVariable("slugName") String slugName) {
return this.categories(model, slugName, 1, Sort.by(DESC, "createTime"));
}
/**
* Render post list page by category
*
* @param model model
* @param slugName slugName
* @param page current page number
* @return template path: themes/{theme}/category.ftl
*/
@GetMapping("{slugName}/page/{page}")
public String categories(Model model,
@PathVariable("slugName") String slugName,
@PathVariable("page") Integer page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
// Get category by slug name
final Category category = categoryService.getBySlugNameOfNonNull(slugName);
final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort);
Page<Post> postPage = postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_category", true);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
model.addAttribute("category", category);
return themeService.render("category");
}
}

View File

@ -7,8 +7,10 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import run.halo.app.controller.content.model.CategoryModel;
import run.halo.app.controller.content.model.PostModel;
import run.halo.app.controller.content.model.SheetModel;
import run.halo.app.controller.content.model.TagModel;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.Sheet;
@ -31,6 +33,10 @@ public class ContentContentController {
private final SheetModel sheetModel;
private final CategoryModel categoryModel;
private final TagModel tagModel;
private final OptionService optionService;
private final PostService postService;
@ -39,16 +45,50 @@ public class ContentContentController {
public ContentContentController(PostModel postModel,
SheetModel sheetModel,
CategoryModel categoryModel,
TagModel tagModel,
OptionService optionService,
PostService postService,
SheetService sheetService) {
this.postModel = postModel;
this.sheetModel = sheetModel;
this.categoryModel = categoryModel;
this.tagModel = tagModel;
this.optionService = optionService;
this.postService = postService;
this.sheetService = sheetService;
}
@GetMapping("{prefix}")
public String content(@PathVariable("prefix") String prefix,
Model model) {
String archivesPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.ARCHIVES_PREFIX, String.class, PermalinkProperties.ARCHIVES_PREFIX.defaultValue());
String categoriesPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.CATEGORIES_PREFIX, String.class, PermalinkProperties.CATEGORIES_PREFIX.defaultValue());
String tagsPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.TAGS_PREFIX, String.class, PermalinkProperties.TAGS_PREFIX.defaultValue());
if (archivesPrefix.equals(prefix)) {
return postModel.list(1, model, "is_archives", "archives");
} else if (categoriesPrefix.equals(prefix)) {
return categoryModel.list(model);
} else if (tagsPrefix.equals(prefix)) {
return tagModel.list(model);
} else {
throw new NotFoundException("Not Found");
}
}
@GetMapping("{prefix}/page/{page}")
public String content(@PathVariable("prefix") String prefix,
@PathVariable(value = "page") Integer page,
Model model) {
String archivesPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.ARCHIVES_PREFIX, String.class, PermalinkProperties.ARCHIVES_PREFIX.defaultValue());
if (archivesPrefix.equals(prefix)) {
return postModel.list(page, model, "is_archives", "archives");
} else {
throw new NotFoundException("Not Found");
}
}
@GetMapping("{prefix}/{url}")
public String content(@PathVariable("prefix") String prefix,
@PathVariable("url") String url,
@ -57,6 +97,8 @@ public class ContentContentController {
PostPermalinkType postPermalinkType = optionService.getPostPermalinkType();
String archivesPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.ARCHIVES_PREFIX, String.class, PermalinkProperties.ARCHIVES_PREFIX.defaultValue());
String sheetPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.SHEET_PREFIX, String.class, PermalinkProperties.SHEET_PREFIX.defaultValue());
String categoriesPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.CATEGORIES_PREFIX, String.class, PermalinkProperties.CATEGORIES_PREFIX.defaultValue());
String tagsPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.TAGS_PREFIX, String.class, PermalinkProperties.TAGS_PREFIX.defaultValue());
if (postPermalinkType.equals(PostPermalinkType.DEFAULT) && archivesPrefix.equals(prefix)) {
Post post = postService.getByUrl(url);
@ -64,6 +106,27 @@ public class ContentContentController {
} else if (sheetPrefix.equals(prefix)) {
Sheet sheet = sheetService.getByUrl(url);
return sheetModel.content(sheet, token, model);
} else if (categoriesPrefix.equals(prefix)) {
return categoryModel.listPost(model, url, 1);
} else if (tagsPrefix.equals(prefix)) {
return tagModel.listPost(model, url, 1);
} else {
throw new NotFoundException("Not Found");
}
}
@GetMapping("{prefix}/{url}/page/{page}")
public String content(@PathVariable("prefix") String prefix,
@PathVariable("url") String url,
@PathVariable("page") Integer page,
Model model) {
String categoriesPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.CATEGORIES_PREFIX, String.class, PermalinkProperties.CATEGORIES_PREFIX.defaultValue());
String tagsPrefix = optionService.getByPropertyOrDefault(PermalinkProperties.TAGS_PREFIX, String.class, PermalinkProperties.TAGS_PREFIX.defaultValue());
if (categoriesPrefix.equals(prefix)) {
return categoryModel.listPost(model, url, page);
} else if (tagsPrefix.equals(prefix)) {
return tagModel.listPost(model, url, page);
} else {
throw new NotFoundException("Not Found");
}

View File

@ -30,10 +30,8 @@ public class ContentSheetController {
private final PhotoService photoService;
public ContentSheetController(
ThemeService themeService,
PhotoService photoService
) {
public ContentSheetController(ThemeService themeService,
PhotoService photoService) {
this.themeService = themeService;
this.photoService = photoService;
}

View File

@ -1,105 +0,0 @@
package run.halo.app.controller.content;
import cn.hutool.core.util.PageUtil;
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 run.halo.app.model.entity.Post;
import run.halo.app.model.entity.Tag;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Tag controller.
*
* @author ryanwang
* @date 2019-03-21
*/
@Controller
@RequestMapping(value = "/tags")
public class ContentTagController {
private final TagService tagService;
private final PostService postService;
private final PostTagService postTagService;
private final OptionService optionService;
private final ThemeService themeService;
public ContentTagController(TagService tagService,
PostService postService,
PostTagService postTagService,
OptionService optionService,
ThemeService themeService) {
this.tagService = tagService;
this.postService = postService;
this.postTagService = postTagService;
this.optionService = optionService;
this.themeService = themeService;
}
/**
* All of tags
*
* @return template path: themes/{theme}/tags.ftl
*/
@GetMapping
public String tags(Model model) {
model.addAttribute("is_tags", true);
return themeService.render("tags");
}
/**
* List tags by tag slug
*
* @param model model
* @param slugName slug name
* @return template path: themes/{theme}/tag.ftl
*/
@GetMapping(value = "{slugName}")
public String tags(Model model,
@PathVariable("slugName") String slugName) {
return this.tags(model, slugName, 1, Sort.by(DESC, "createTime"));
}
/**
* List tags by tag slug
*
* @param model model
* @param slugName slug name
* @param page current page
* @return template path: themes/{theme}/tag.ftl
*/
@GetMapping(value = "{slugName}/page/{page}")
public String tags(Model model,
@PathVariable("slugName") String slugName,
@PathVariable("page") Integer page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
// Get tag by slug name
final Tag tag = tagService.getBySlugNameOfNonNull(slugName);
final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort);
Page<Post> postPage = postTagService.pagePostsBy(tag.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_tag", true);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
model.addAttribute("tag", tag);
return themeService.render("tag");
}
}

View File

@ -0,0 +1,65 @@
package run.halo.app.controller.content.model;
import cn.hutool.core.util.PageUtil;
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.stereotype.Component;
import org.springframework.ui.Model;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Category Model.
*
* @author ryanwang
* @date 2020-01-11
*/
@Component
public class CategoryModel {
private final CategoryService categoryService;
private final ThemeService themeService;
private final PostCategoryService postCategoryService;
private final PostService postService;
private final OptionService optionService;
public CategoryModel(CategoryService categoryService, ThemeService themeService, PostCategoryService postCategoryService, PostService postService, OptionService optionService) {
this.categoryService = categoryService;
this.themeService = themeService;
this.postCategoryService = postCategoryService;
this.postService = postService;
this.optionService = optionService;
}
public String list(Model model) {
model.addAttribute("is_categories", true);
return themeService.render("categories");
}
public String listPost(Model model, String slugName, Integer page) {
// Get category by slug name
final Category category = categoryService.getBySlugNameOfNonNull(slugName);
final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), Sort.by(DESC, "createTime"));
Page<Post> postPage = postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_category", true);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
model.addAttribute("category", category);
return themeService.render("category");
}
}

View File

@ -0,0 +1,65 @@
package run.halo.app.controller.content.model;
import cn.hutool.core.util.PageUtil;
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.stereotype.Component;
import org.springframework.ui.Model;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.Tag;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Tag Model.
*
* @author ryanwang
* @date 2020-01-11
*/
@Component
public class TagModel {
private final TagService tagService;
private final PostService postService;
private final PostTagService postTagService;
private final OptionService optionService;
private final ThemeService themeService;
public TagModel(TagService tagService, PostService postService, PostTagService postTagService, OptionService optionService, ThemeService themeService) {
this.tagService = tagService;
this.postService = postService;
this.postTagService = postTagService;
this.optionService = optionService;
this.themeService = themeService;
}
public String list(Model model) {
model.addAttribute("is_tags", true);
return themeService.render("tags");
}
public String listPost(Model model, String slugName, Integer page) {
// Get tag by slug name
final Tag tag = tagService.getBySlugNameOfNonNull(slugName);
final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), Sort.by(DESC, "createTime"));
Page<Post> postPage = postTagService.pagePostsBy(tag.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_tag", true);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
model.addAttribute("tag", tag);
return themeService.render("tag");
}
}