优化获取post列表功能
This commit is contained in:
parent
ae4386c1db
commit
63042d44e4
@ -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**
|
||||
@ -18,12 +18,14 @@
|
||||
|
||||
### 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 返回参数说明
|
||||
|
@ -25,8 +25,8 @@ public class PostController {
|
||||
|
||||
// https://iibiubiu.com/v1/posts?style=newest&page=1&page_size=20
|
||||
@GetMapping("/posts")
|
||||
public DataResult<Paged<PostDto>> 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<Paged<PostDto>> 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")
|
||||
|
@ -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<Post, Long> {
|
||||
@Query("SELECT p FROM Post p where isDel=0 ORDER BY p.createdOn DESC")
|
||||
Page<Post> findAllNewest(Pageable pageable);
|
||||
|
||||
@Query("SELECT p FROM Post p where isDel=0 and p.tags LIKE CONCAT('%', :tag, '%') ORDER BY p.createdOn DESC")
|
||||
Page<Post> findAllNewestByTag(String tag, Pageable pageable);
|
||||
|
||||
}
|
||||
|
@ -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<PostDto> getPosts(Long type, String style, Integer page, Integer size){
|
||||
size = size==null?10:size; // 如果未指定每页大小,默认为10
|
||||
public Paged<PostDto> 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<Post> posts = postRepository.findAllNewestByTag(query, pageable);
|
||||
return pageDataWrapper(posts);
|
||||
}
|
||||
Page<Post> posts = postRepository.findAllNewest(pageable);
|
||||
return pageDataWrapper(posts);
|
||||
}
|
||||
|
||||
return new Paged<PostDto>(new ArrayList<PostDto>(),0L,page,0);
|
||||
|
||||
Sort sort = Sort.by(Sort.Direction.DESC, "createdOn"); // 按创建时间倒序排序
|
||||
Pageable pageable = PageRequest.of(page-1, size, sort); // 创建分页请求
|
||||
Page<Post> posts = postRepository.findAll(pageable); // 执行分页查询
|
||||
List<PostDto> 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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user