From 63042d44e42c27c5e3b1987fad5a0782badeb47c Mon Sep 17 00:00:00 2001 From: many2many <6168830@qq.com> Date: Fri, 31 May 2024 09:21:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=8E=B7=E5=8F=96post?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api doc/分页 获取post接口.md | 10 +++--- .../lk/paopao/controller/PostController.java | 4 +-- .../lk/paopao/repository/PostRepository.java | 11 +++++- .../com/lk/paopao/service/PostService.java | 35 ++++++++++++------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/docs/api doc/分页 获取post接口.md b/docs/api doc/分页 获取post接口.md index bde6fe0..1a3bbad 100644 --- a/docs/api doc/分页 获取post接口.md +++ b/docs/api doc/分页 获取post接口.md @@ -6,7 +6,7 @@ ### 2 请求URL -`/v1/posts?type=&style=&page=&page_size=` +`/v1/posts?query=JAVA&type=tag&style=newest&page=1&page_size=20` ### 3 请求方式 **GET** @@ -17,13 +17,15 @@ | | | || ### 5 Query参数 -| 参数名称 | 必填 | 数据类型 | 约束条件 | 描述 | 示例 | 允许空值 | -|--------|----| -------- | ------- |----|------------|------| -| style | 是 | 字符串 | | 类型 | "newest" | 否 | +| 参数名称 | 必填 | 数据类型 | 约束条件 | 描述 | 示例 | 允许空值 | +|-------|----| -------- | ------- |--|------------------------------------------------------------------|------| +| style | 是 | 字符串 | | | "newest" | 否 | +| type | 是 | 字符串 | | 类型 | 可能是"tag":表示根据tag查找post. 可能是时间戳,如 "1716477250320" ,则表示查询最新post | 否 | ### 6 请求示例 ```http GET /v1/posts?type=1716477250320&style=newest&page=1&page_size=20 +GET /v1/posts?query=JAVA&type=tag&style=newest&page=1&page_size=20 ``` ### 7 返回参数说明 diff --git a/src/main/java/com/lk/paopao/controller/PostController.java b/src/main/java/com/lk/paopao/controller/PostController.java index 8c9cfe8..123f1ca 100644 --- a/src/main/java/com/lk/paopao/controller/PostController.java +++ b/src/main/java/com/lk/paopao/controller/PostController.java @@ -25,8 +25,8 @@ public class PostController { // https://iibiubiu.com/v1/posts?style=newest&page=1&page_size=20 @GetMapping("/posts") - public DataResult> getPosts(@PathParam("type") Long type, @PathParam("style") String style, @PathParam("page") Integer page, @PathParam("page_size") Integer pageSize) { - return ResultUtil.ok(postService.getPosts(type, style, page, pageSize)); + public DataResult> getPosts(@PathParam("type") String type, @PathParam("style") String style, @PathParam("style") String query, @PathParam("page") Integer page, @PathParam("page_size") Integer pageSize) { + return ResultUtil.ok(postService.getPosts(type, style,query, page, pageSize)); } @GetMapping("/post") diff --git a/src/main/java/com/lk/paopao/repository/PostRepository.java b/src/main/java/com/lk/paopao/repository/PostRepository.java index b39eb71..b4c5689 100644 --- a/src/main/java/com/lk/paopao/repository/PostRepository.java +++ b/src/main/java/com/lk/paopao/repository/PostRepository.java @@ -1,7 +1,16 @@ package com.lk.paopao.repository; import com.lk.paopao.entity.Post; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +public interface PostRepository extends JpaRepository { + @Query("SELECT p FROM Post p where isDel=0 ORDER BY p.createdOn DESC") + Page findAllNewest(Pageable pageable); + + @Query("SELECT p FROM Post p where isDel=0 and p.tags LIKE CONCAT('%', :tag, '%') ORDER BY p.createdOn DESC") + Page findAllNewestByTag(String tag, Pageable pageable); -public interface PostRepository extends JpaRepository { } diff --git a/src/main/java/com/lk/paopao/service/PostService.java b/src/main/java/com/lk/paopao/service/PostService.java index 02e9ba4..2f7c92a 100644 --- a/src/main/java/com/lk/paopao/service/PostService.java +++ b/src/main/java/com/lk/paopao/service/PostService.java @@ -64,23 +64,31 @@ public class PostService { /** * 获取文章列表 - * @param type 文章类型 - * @param style 文章样式 + * @param type + * @param style * @param page 页码 + * @param query * @param size 每页大小 * @return 文章DTO的分页数据 */ - public Paged getPosts(Long type, String style, Integer page, Integer size){ - size = size==null?10:size; // 如果未指定每页大小,默认为10 + public Paged getPosts(String type, String style, String query, Integer page, Integer size){ + // 如果未指定每页大小,默认为10 + size = size==null?10:size; + if("newest".equals(style)){ + // 按创建时间倒序排序 + Sort sort = Sort.by(Sort.Direction.DESC, "createdOn"); + // 创建分页请求 + Pageable pageable = PageRequest.of(page-1, size, sort); + if("tag".equals(type)){ + Page posts = postRepository.findAllNewestByTag(query, pageable); + return pageDataWrapper(posts); + } + Page posts = postRepository.findAllNewest(pageable); + return pageDataWrapper(posts); + } + + return new Paged(new ArrayList(),0L,page,0); - Sort sort = Sort.by(Sort.Direction.DESC, "createdOn"); // 按创建时间倒序排序 - Pageable pageable = PageRequest.of(page-1, size, sort); // 创建分页请求 - Page posts = postRepository.findAll(pageable); // 执行分页查询 - List dtos = new ArrayList<>(); // 用于存储转换后的DTO对象的列表 - posts.getContent().forEach((post)->{ - dtos.add(postMapper.toDto(post)); // 将查询结果转换为DTO - }); - return new Paged<>(dtos,posts.getTotalElements(),page,posts.getSize()); // 构建并返回分页数据 } /** @@ -105,7 +113,7 @@ public class PostService { Post post = new Post(); post.setUser(currentUser); // 设置附件价格,默认为0 - post.setAttachmentPrice(postRequest.getAttachmentPrice()!=null?postRequest.getAttachmentPrice():0); + post.setAttachmentPrice(postRequest.getAttachmentPrice()); // 设置文章可见性 post.setVisibility(postRequest.getVisibility()); // 文章内容 @@ -120,6 +128,7 @@ public class PostService { contents.add(content); // 添加到列表 }); post.setContents(contents); + post.setTags(String.join(",",postRequest.getTags())); // 保存文章 return postRepository.save(post); }