Skip to content

Commit

Permalink
Complete content api.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Jun 9, 2019
1 parent 56243d9 commit 006edf1
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
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.service.CategoryService;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostCategoryService;
import run.halo.app.service.ThemeService;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*;

import static org.springframework.data.domain.Sort.Direction.DESC;

Expand All @@ -34,15 +32,18 @@ public class ContentCategoryController {

private final PostCategoryService postCategoryService;

private final PostService postService;

private final OptionService optionService;

public ContentCategoryController(CategoryService categoryService,
ThemeService themeService,
PostCategoryService postCategoryService,
OptionService optionService) {
PostService postService, OptionService optionService) {
this.categoryService = categoryService;
this.themeService = themeService;
this.postCategoryService = postCategoryService;
this.postService = postService;
this.optionService = optionService;
}

Expand Down Expand Up @@ -87,7 +88,8 @@ public String categories(Model model,
final Category category = categoryService.getBySlugName(slugName);

final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort);
Page<Post> posts = postCategoryService.pagePostBy(category.getId(), pageable);
Page<Post> postPage = postCategoryService.pagePostBy(category.getId(), pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);

model.addAttribute("is_category", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public String search(Model model,
@PathVariable(value = "page") Integer page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort);
final Page<Post> posts = postService.pageBy(keyword, pageable);
final Page<Post> postPage = postService.pageBy(keyword, pageable);

final Page<PostListVO> postPage = postService.convertToListVo(posts);
final Page<PostListVO> posts = postService.convertToListVo(postPage);

final int[] rainbow = PageUtil.rainbow(page, postPage.getTotalPages(), 3);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_search", true);
model.addAttribute("keyword", keyword);
model.addAttribute("posts", postPage);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
return themeService.render("search");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.support.HaloConst;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public String tags(Model model,
@PathVariable("slugName") String slugName,
@PathVariable("page") Integer page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
Tag tag = tagService.getBySlugNameOfNonNull(slugName);
// 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(), pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package run.halo.app.controller.content.api;

import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.CategoryDTO;
import run.halo.app.model.dto.post.BasePostSimpleDTO;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.service.CategoryService;
import run.halo.app.service.PostCategoryService;
import run.halo.app.service.PostService;

import java.util.List;

import static org.springframework.data.domain.Sort.Direction.DESC;

/**
* Category portal controller.
*
* @author ryanwang
* @date 6/9/19
*/
@RestController("ApiContentCategoryController")
@RequestMapping("/api/content/categories")
public class CategoryController {

private final CategoryService categoryService;

private final PostCategoryService postCategoryService;

private final PostService postService;

public CategoryController(CategoryService categoryService,
PostCategoryService postCategoryService,
PostService postService) {
this.categoryService = categoryService;
this.postCategoryService = postCategoryService;
this.postService = postService;
}

@GetMapping
@ApiOperation("Lists categories")
public List<? extends CategoryDTO> listCategories(@SortDefault(sort = "updateTime", direction = DESC) Sort sort,
@RequestParam(name = "more", required = false, defaultValue = "false") Boolean more) {
if (more) {
return postCategoryService.listCategoryWithPostCountDto(sort);
}
return categoryService.convertTo(categoryService.listAll(sort));
}

@GetMapping("{slugName}/posts")
@ApiOperation("Lists posts by category slug name")
public Page<BasePostSimpleDTO> listPostsBy(@PathVariable("slugName") String slugName,
@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
// Get category by slug name
Category category = categoryService.getBySlugName(slugName);

Page<Post> postPage = postCategoryService.pagePostBy(category.getId(), pageable);
return postService.convertToSimple(postPage);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package run.halo.app.controller.content.api;

import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.model.dto.LinkDTO;
import run.halo.app.model.vo.LinkTeamVO;
import run.halo.app.service.LinkService;

import java.util.List;

import static org.springframework.data.domain.Sort.Direction.DESC;

/**
* Portal link controller.
*
* @author johnniang
* @author ryanwang
* @date 4/3/19
*/
@RestController("ApiContentLinkController")
Expand All @@ -25,7 +31,14 @@ public LinkController(LinkService linkService) {
this.linkService = linkService;
}

@GetMapping
@ApiOperation("List all links")
public List<LinkDTO> listLinks(@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
return linkService.listDtos(sort);
}

@GetMapping("team_view")
@ApiOperation("List all links with team view")
public List<LinkTeamVO> listTeamVos(Sort sort) {
return linkService.listTeamVos(sort);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.model.dto.MenuDTO;
import run.halo.app.model.vo.MenuVO;
import run.halo.app.service.MenuService;

import java.util.List;
Expand All @@ -17,6 +18,7 @@
* Portal menu controller.
*
* @author johnniang
* @author ryanwang
* @date 4/3/19
*/
@RestController("ApiContentMenuController")
Expand All @@ -34,4 +36,10 @@ public MenuController(MenuService menuService) {
public List<MenuDTO> listAll(@SortDefault(sort = "priority", direction = DESC) Sort sort) {
return menuService.listDtos(sort);
}

@GetMapping(value = "tree_view")
@ApiOperation("Lists menus with tree view")
public List<MenuVO> listMenusTree(@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
return menuService.listAsTree(sort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public Page<BasePostSimpleDTO> pageBy(@PageableDefault(sort = "updateTime", dire
return postService.convertToSimple(postPage);
}

@PostMapping(value = "search")
@ApiOperation("Lists posts by keyword")
public Page<BasePostSimpleDTO> pageBy(@RequestParam(value = "keyword") String keyword,
@PageableDefault(sort = "createTime", direction = DESC) Pageable pageable) {
Page<Post> postPage = postService.pageBy(keyword, pageable);
return postService.convertToSimple(postPage);
}

@GetMapping("{postId:\\d+}")
@ApiOperation("Gets a post")
public BasePostDetailDTO getBy(@PathVariable("postId") Integer postId,
Expand Down

0 comments on commit 006edf1

Please sign in to comment.