Skip to content

Commit 827e75d

Browse files
author
ZheNing Hu
committed
feat(page): add page params to apis
Signed-off-by: ZheNing Hu <adlternative@gamil.com>
1 parent 5b1226c commit 827e75d

File tree

7 files changed

+64
-33
lines changed

7 files changed

+64
-33
lines changed

src/main/java/com/adlternative/tinyhacknews/controller/NewsController.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.adlternative.tinyhacknews.entity.SubmitNewsInputDTO;
66
import com.adlternative.tinyhacknews.service.CommentService;
77
import com.adlternative.tinyhacknews.service.NewsService;
8-
import java.util.List;
8+
import com.baomidou.mybatisplus.core.metadata.IPage;
99
import lombok.RequiredArgsConstructor;
1010
import org.springframework.web.bind.annotation.DeleteMapping;
1111
import org.springframework.web.bind.annotation.GetMapping;
@@ -85,10 +85,12 @@ public void deleteNews(
8585
* @return
8686
*/
8787
@GetMapping("/{id}/comments")
88-
public List<CommentData> getComments(
89-
@PathVariable(name = "id") Long newsId, @RequestParam(name = "user_id") Long userId) {
90-
// TODO: 添加分页参数
91-
return commentService.getComments(newsId, userId);
88+
public IPage<CommentData> getComments(
89+
@PathVariable(name = "id") Long newsId,
90+
@RequestParam(name = "user_id") Long userId,
91+
@RequestParam(name = "page_num", defaultValue = "1") Long pageNum,
92+
@RequestParam(name = "page_size", defaultValue = "10") Long pageSize) {
93+
return commentService.getComments(newsId, userId, pageNum, pageSize);
9294
}
9395

9496
/**
@@ -98,9 +100,11 @@ public List<CommentData> getComments(
98100
* @return
99101
*/
100102
@GetMapping("/all")
101-
public List<NewsData> getAllNews(@RequestParam(name = "user_id") Long userId) {
102-
// TODO: 添加分页参数
103-
return newsService.getAllNews(userId);
103+
public IPage<NewsData> getAllNews(
104+
@RequestParam(name = "user_id") Long userId,
105+
@RequestParam(name = "page_num", defaultValue = "1") Long pageNum,
106+
@RequestParam(name = "page_size", defaultValue = "10") Long pageSize) {
107+
return newsService.getAllNews(userId, pageNum, pageSize);
104108
}
105109

106110
private final NewsService newsService;

src/main/java/com/adlternative/tinyhacknews/controller/UsersController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import org.springframework.web.bind.annotation.RequestParam;
1919
import org.springframework.web.bind.annotation.RestController;
2020

21-
import java.util.List;
22-
2321
@RestController
2422
@RequestMapping("/api/v1/users")
2523
@RequiredArgsConstructor
@@ -69,7 +67,7 @@ public UserInfo getUserInfoByUserName(@RequestParam(name = "name") String name)
6967
*/
7068
@PutMapping
7169
public UserInfo updateUserInfo(
72-
@RequestParam(name = "id") Long id, @RequestBody UpdateUserInfoDTO updateUserInfoDTO) {
70+
@RequestParam(name = "id") Long id, @RequestBody UpdateUserInfoDTO updateUserInfoDTO) {
7371
return userService.updateUserInfo(id, updateUserInfoDTO);
7472
}
7573

@@ -89,8 +87,10 @@ public void deleteUser(@RequestParam(name = "id") Long id) {
8987
* @param id
9088
*/
9189
@GetMapping("/{id}/news")
92-
public List<NewsData> getAllNewsOfUser(@PathVariable(name = "id") Long id) {
93-
// TODO: 添加分页逻辑
94-
return newsService.getAllNewsOfUser(id);
90+
public IPage<NewsData> getAllNewsOfUser(
91+
@PathVariable(name = "id") Long id,
92+
@RequestParam(name = "page_num", defaultValue = "1") Long pageNum,
93+
@RequestParam(name = "page_size", defaultValue = "10") Long pageSize) {
94+
return newsService.getAllNewsOfUser(id, pageNum, pageSize);
9595
}
9696
}

src/main/java/com/adlternative/tinyhacknews/service/CommentService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.adlternative.tinyhacknews.entity.CommentData;
44
import com.adlternative.tinyhacknews.entity.SubmitCommentInputDTO;
55
import com.adlternative.tinyhacknews.entity.UpdateCommentInputDTO;
6+
import com.baomidou.mybatisplus.core.metadata.IPage;
67
import java.util.List;
78

89
public interface CommentService {
@@ -48,7 +49,7 @@ public interface CommentService {
4849
* @param userId
4950
* @return
5051
*/
51-
List<CommentData> getComments(Long newsId, Long userId);
52+
IPage<CommentData> getComments(Long newsId, Long userId, Long pageNum, Long pageSize);
5253

5354
List<CommentData> getSubComments(Long commentId, Long userId);
5455
}

src/main/java/com/adlternative/tinyhacknews/service/NewsService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.adlternative.tinyhacknews.entity.NewsData;
44
import com.adlternative.tinyhacknews.entity.SubmitNewsInputDTO;
5-
import java.util.List;
5+
import com.baomidou.mybatisplus.core.metadata.IPage;
66

77
public interface NewsService {
88

@@ -48,13 +48,13 @@ public interface NewsService {
4848
* @param userId
4949
* @return
5050
*/
51-
List<NewsData> getAllNewsOfUser(Long userId);
51+
IPage<NewsData> getAllNewsOfUser(Long userId, Long pageNum, Long pageSize);
5252

5353
/**
5454
* 获取所有新闻
5555
*
5656
* @param userId
5757
* @return
5858
*/
59-
List<NewsData> getAllNews(Long userId);
59+
IPage<NewsData> getAllNews(Long userId, Long pageNum, Long pageSize);
6060
}

src/main/java/com/adlternative/tinyhacknews/service/impl/CommentServiceImpl.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.adlternative.tinyhacknews.mapper.UsersMapper;
1919
import com.adlternative.tinyhacknews.service.CommentService;
2020
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
21+
import com.baomidou.mybatisplus.core.metadata.IPage;
22+
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
2123
import java.time.LocalDateTime;
2224
import java.util.List;
2325
import java.util.Objects;
@@ -197,16 +199,18 @@ public CommentData getComment(Long id, Long userId) {
197199
}
198200

199201
@Override
200-
public List<CommentData> getComments(Long newsId, Long userId) {
202+
public IPage<CommentData> getComments(Long newsId, Long userId, Long pageNum, Long pageSize) {
201203
// TODO: userId 用于权限检查
202204

203205
// 证明新闻存在
204206
News news =
205207
Optional.ofNullable(newsMapper.selectById(newsId))
206208
.orElseThrow(() -> new NewsNotFoundException("News not found for id: " + newsId));
207209
// TODO: 一条 sql, join 一下就好
208-
return commentMapper.selectByNewsId(news.getId()).stream()
209-
.map(
210+
return commentMapper
211+
.selectPage(
212+
new Page<>(pageNum, pageSize), new QueryWrapper<Comments>().eq("news_id", news.getId()))
213+
.convert(
210214
comment -> {
211215
Users user =
212216
Optional.ofNullable(userMapper.selectById(comment.getAuthorId()))
@@ -215,8 +219,7 @@ public List<CommentData> getComments(Long newsId, Long userId) {
215219
new UserNotFoundException(
216220
"User not found for id: " + comment.getAuthorId()));
217221
return CommentData.convertFrom(comment, user);
218-
})
219-
.collect(Collectors.toList());
222+
});
220223
}
221224

222225
@Override

src/main/java/com/adlternative/tinyhacknews/service/impl/NewsServiceImpl.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
import com.adlternative.tinyhacknews.mapper.UsersMapper;
1515
import com.adlternative.tinyhacknews.service.NewsService;
1616
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
17+
import com.baomidou.mybatisplus.core.metadata.IPage;
18+
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
1719
import java.time.LocalDateTime;
18-
import java.util.List;
1920
import java.util.Objects;
2021
import java.util.Optional;
21-
import java.util.stream.Collectors;
2222
import lombok.RequiredArgsConstructor;
2323
import lombok.extern.slf4j.Slf4j;
2424
import org.springframework.stereotype.Service;
@@ -134,23 +134,24 @@ public NewsData changeNews(Long id, Long userId, SubmitNewsInputDTO submitNewsIn
134134
}
135135

136136
@Override
137-
public List<NewsData> getAllNewsOfUser(Long userId) {
137+
public IPage<NewsData> getAllNewsOfUser(Long userId, Long pageNum, Long pageSize) {
138138
Users user =
139139
Optional.ofNullable(userMapper.selectById(userId))
140140
.orElseThrow(() -> new UserNotFoundException("Failed to get user, user not found"));
141-
return newsMapper.selectByAuthorId(userId).stream()
142-
.map(singleNew -> NewsData.convertFromNews(singleNew, user))
143-
.collect(Collectors.toList());
141+
return newsMapper
142+
.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<News>().eq("author_id", userId))
143+
.convert(singleNew -> NewsData.convertFromNews(singleNew, user));
144144
}
145145

146146
@Override
147-
public List<NewsData> getAllNews(Long userId) {
147+
public IPage<NewsData> getAllNews(Long userId, Long pageNum, Long pageSize) {
148148
Users user =
149149
Optional.ofNullable(userMapper.selectById(userId))
150150
.orElseThrow(() -> new UserNotFoundException("Failed to get user, user not found"));
151-
return newsMapper.selectAll().stream()
152-
.map(singleNew -> NewsData.convertFromNews(singleNew, user))
153-
.collect(Collectors.toList());
151+
152+
return newsMapper
153+
.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<>())
154+
.convert(singleNew -> NewsData.convertFromNews(singleNew, user));
154155
}
155156

156157
private final NewsMapper newsMapper;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.adlternative.tinyhacknews.web;
2+
3+
import com.baomidou.mybatisplus.annotation.DbType;
4+
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
5+
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
6+
import org.mybatis.spring.annotation.MapperScan;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
@MapperScan("com.adlternative.tinyhacknews.mapper")
12+
public class MybatisPlusConfig {
13+
14+
/** 添加分页插件 */
15+
@Bean
16+
public MybatisPlusInterceptor mybatisPlusInterceptor() {
17+
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
18+
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
19+
20+
return interceptor;
21+
}
22+
}

0 commit comments

Comments
 (0)