Refactor related page operations

pull/137/head
johnniang 2019-03-14 21:58:07 +08:00
parent e895725a9c
commit 06e18f3e8c
5 changed files with 27 additions and 33 deletions

View File

@ -7,6 +7,7 @@ import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.lang.NonNull;
/** /**
@ -18,23 +19,22 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecificationExecutor<Post> { public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecificationExecutor<Post> {
/** /**
* Find posts by status and type * Finds posts by status and type.
* *
* @param status status * @param status status
* @param type type * @param type type
* @param pageable pageable * @param pageable pageable
*
* @return Page<Post> * @return Page<Post>
*/ */
Page<Post> queryAllByStatusAndType(PostStatus status, PostType type, Pageable pageable); @NonNull
Page<Post> findAllByStatusAndType(@NonNull PostStatus status, @NonNull PostType type, @NonNull Pageable pageable);
/** /**
* Count posts by status and type * Counts posts by status and type.
* *
* @param status status * @param status status
* @param type type * @param type type
*
* @return posts count * @return posts count
*/ */
Long countAllByStatusAndType(PostStatus status, PostType type); long countByStatusAndType(@NonNull PostStatus status, @NonNull PostType type);
} }

View File

@ -21,6 +21,7 @@ public class AdminAuthenticationFilter extends OncePerRequestFilter {
@Override @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// TODO Handle admin authentication // TODO Handle admin authentication
filterChain.doFilter(request, response);
} }
public void setFailureHandler(AuthenticationFailureHandler failureHandler) { public void setFailureHandler(AuthenticationFailureHandler failureHandler) {

View File

@ -37,7 +37,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @return Page<PostSimpleOutputDTO> * @return Page<PostSimpleOutputDTO>
*/ */
@NonNull @NonNull
Page<PostSimpleOutputDTO> listByStatus(PostStatus status, PostType type, Pageable pageable); Page<PostSimpleOutputDTO> pageByStatusAndType(PostStatus status, PostType type, Pageable pageable);
/** /**
* Count posts by status and type * Count posts by status and type
@ -46,5 +46,5 @@ public interface PostService extends CrudService<Post, Integer> {
* @param type type * @param type type
* @return posts count * @return posts count
*/ */
Long countByStatus(PostStatus status, PostType type); Long countByStatusAndType(PostStatus status, PostType type);
} }

View File

@ -51,8 +51,8 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* @return Page<PostSimpleOutputDTO> * @return Page<PostSimpleOutputDTO>
*/ */
@Override @Override
public Page<PostSimpleOutputDTO> listByStatus(PostStatus status, PostType type, Pageable pageable) { public Page<PostSimpleOutputDTO> pageByStatusAndType(PostStatus status, PostType type, Pageable pageable) {
Page<Post> posts = postRepository.queryAllByStatusAndType(status, type, pageable); Page<Post> posts = postRepository.findAllByStatusAndType(status, type, pageable);
return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post)); return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post));
} }
@ -65,7 +65,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* @return posts count * @return posts count
*/ */
@Override @Override
public Long countByStatus(PostStatus status, PostType type) { public Long countByStatusAndType(PostStatus status, PostType type) {
return postRepository.countAllByStatusAndType(status,type); return postRepository.countByStatusAndType(status,type);
} }
} }

View File

@ -5,10 +5,8 @@ import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; 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.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -36,28 +34,23 @@ public class PostController {
/** /**
* posts manage * posts manage
* *
* @param model model * @param model model
* @param status post status * @param status post status
* @param page current page * @param pageable page info
* @param sort sort
*
* @return template path: admin/admin_post.ftl * @return template path: admin/admin_post.ftl
*/ */
@GetMapping @GetMapping
public String posts(Model model, public String listPosts(Model model,
@RequestParam(value = "status", defaultValue = "published") PostStatus status, @PageableDefault(sort = {"topPriority", "createTime"}, direction = DESC) Pageable pageable,
@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "status", defaultValue = "published") PostStatus status) {
@SortDefault.SortDefaults({ final Page<PostSimpleOutputDTO> postPage = postService.pageByStatusAndType(status, PostType.POST, pageable);
@SortDefault(sort = "topPriority", direction = DESC),
@SortDefault(sort = "createTime", direction = DESC) model.addAttribute("posts", postPage.getContent());
}) Sort sort) { model.addAttribute("publishedCount", postService.countByStatusAndType(PostStatus.PUBLISHED, PostType.POST));
final Pageable pageable = PageRequest.of(page, 10, sort); model.addAttribute("draftCount", postService.countByStatusAndType(PostStatus.DRAFT, PostType.POST));
final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable); model.addAttribute("recycleCount", postService.countByStatusAndType(PostStatus.RECYCLE, PostType.POST));
model.addAttribute("posts", posts);
model.addAttribute("publishedCount", postService.countByStatus(PostStatus.PUBLISHED, PostType.POST));
model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT, PostType.POST));
model.addAttribute("recycleCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST));
model.addAttribute("status", status); model.addAttribute("status", status);
return "admin/admin_post"; return "admin/admin_post";
} }
} }