Complete cc.ryanc.halo.web.controller.portal.api.TagController.listPostsBy api

pull/137/head
johnniang 2019-04-03 02:11:19 +08:00
parent e64298209e
commit 717e5b25b4
6 changed files with 47 additions and 8 deletions

View File

@ -5,6 +5,8 @@ import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.entity.PostTag; import cc.ryanc.halo.model.entity.PostTag;
import cc.ryanc.halo.model.entity.Tag; import cc.ryanc.halo.model.entity.Tag;
import cc.ryanc.halo.service.base.CrudService; import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -58,6 +60,15 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
@NonNull @NonNull
List<Post> listPostsBy(@NonNull Integer tagId); List<Post> listPostsBy(@NonNull Integer tagId);
/**
* Pages posts by tag id.
*
* @param tagId must not be null
* @param pageable must not be null
* @return a page of post
*/
Page<Post> pagePostsBy(@NonNull Integer tagId, Pageable pageable);
/** /**
* Merges or creates post tags by post id and tag id set if absent. * Merges or creates post tags by post id and tag id set if absent.
* *

View File

@ -21,7 +21,8 @@ public interface TagService extends CrudService<Tag, Integer> {
* @param slugName slug name * @param slugName slug name
* @return Tag * @return Tag
*/ */
Tag getBySlugName(@NonNull String slugName); @NonNull
Tag getBySlugNameOfNonNull(@NonNull String slugName);
/** /**
* Converts to tag output dtos. * Converts to tag output dtos.

View File

@ -11,6 +11,8 @@ import cc.ryanc.halo.repository.TagRepository;
import cc.ryanc.halo.service.PostTagService; import cc.ryanc.halo.service.PostTagService;
import cc.ryanc.halo.service.base.AbstractCrudService; import cc.ryanc.halo.service.base.AbstractCrudService;
import cc.ryanc.halo.utils.ServiceUtils; import cc.ryanc.halo.utils.ServiceUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -111,6 +113,17 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
return postRepository.findAllById(postIds); return postRepository.findAllById(postIds);
} }
@Override
public Page<Post> pagePostsBy(Integer tagId, Pageable pageable) {
Assert.notNull(tagId, "Tag id must not be null");
Assert.notNull(pageable, "Page info must not be null");
// Find all post ids
Set<Integer> postIds = postTagRepository.findAllPostIdsByTagId(tagId);
return postRepository.findAllByIdIn(postIds, pageable);
}
@Override @Override
public List<PostTag> mergeOrCreateByIfAbsent(Integer postId, Set<Integer> tagIds) { public List<PostTag> mergeOrCreateByIfAbsent(Integer postId, Set<Integer> tagIds) {
Assert.notNull(postId, "Post id must not be null"); Assert.notNull(postId, "Post id must not be null");

View File

@ -55,7 +55,7 @@ public class TagServiceImpl extends AbstractCrudService<Tag, Integer> implements
* @return Tag * @return Tag
*/ */
@Override @Override
public Tag getBySlugName(String slugName) { public Tag getBySlugNameOfNonNull(String slugName) {
return tagRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The tag does not exist").setErrorData(slugName)); return tagRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The tag does not exist").setErrorData(slugName));
} }

View File

@ -79,7 +79,7 @@ public class ContentTagController extends BaseContentController {
@PathVariable("slugName") String slugName, @PathVariable("slugName") String slugName,
@PathVariable("page") Integer page, @PathVariable("page") Integer page,
@SortDefault(sort = "postDate", direction = DESC) Sort sort) { @SortDefault(sort = "postDate", direction = DESC) Sort sort) {
final Tag tag = tagService.getBySlugName(slugName); final Tag tag = tagService.getBySlugNameOfNonNull(slugName);
if (null == tag) { if (null == tag) {
return this.renderNotFound(); return this.renderNotFound();
} }

View File

@ -1,18 +1,22 @@
package cc.ryanc.halo.web.controller.portal.api; package cc.ryanc.halo.web.controller.portal.api;
import cc.ryanc.halo.model.dto.TagOutputDTO; import cc.ryanc.halo.model.dto.TagOutputDTO;
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.model.entity.Tag;
import cc.ryanc.halo.service.PostTagService; import cc.ryanc.halo.service.PostTagService;
import cc.ryanc.halo.service.TagService; import cc.ryanc.halo.service.TagService;
import io.swagger.annotations.ApiOperation; 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.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault; import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC;
/** /**
* Portal tag controller. * Portal tag controller.
* *
@ -34,7 +38,7 @@ public class TagController {
@GetMapping @GetMapping
@ApiOperation("Lists tags") @ApiOperation("Lists tags")
public List<? extends TagOutputDTO> listTags(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort, public List<? extends TagOutputDTO> listTags(@SortDefault(sort = "updateTime", direction = DESC) Sort sort,
@RequestParam(name = "more", required = false, defaultValue = "false") Boolean more) { @RequestParam(name = "more", required = false, defaultValue = "false") Boolean more) {
if (more) { if (more) {
return postTagService.listTagWithCountDtos(sort); return postTagService.listTagWithCountDtos(sort);
@ -42,4 +46,14 @@ public class TagController {
return tagService.convertTo(tagService.listAll(sort)); return tagService.convertTo(tagService.listAll(sort));
} }
@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) {
// 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));
}
} }