Skip to content

Commit

Permalink
Merge pull request #71 from nick102030/view
Browse files Browse the repository at this point in the history
[refactoring] : 유저 닉네임 게시글 조회 수정
  • Loading branch information
nick102030 authored Jun 10, 2024
2 parents 27d27e2 + 24b6e05 commit 552a8fc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/example/jolvre/post/api/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public ResponseEntity<Page<postResponse>> getAllPosts(

//특정 유저가 작성한 모든 글 조회
@Operation(summary = "유저의 게시글 조회")
@GetMapping("/user/{userId}")
@GetMapping("/user/{userNickname}")
public ResponseEntity<Page<postResponse>> getPostsByUserId(
@PathVariable("userId") Long userId,
@PathVariable("userNickname") String userNickname,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "10") int size) {

PageRequest pageable = PageRequest.of(page - 1, size, Sort.by("createdDate").descending());
Page<postResponse> postList = postService.getPostsByUserId(userId, (PageRequest) pageable);
Page<postResponse> postList = postService.getPostsByUser(userNickname, (PageRequest) pageable);

return ResponseEntity.status(HttpStatus.OK).body(postList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ public Page<postResponse> getAllPost(PageRequest pageable) {
return postList.map(postResponse::toDTO);
}

public Page<postResponse> getPostsByUserId(Long userId, PageRequest pageable) {
if (userRepository.findById(userId).isEmpty()) {
public Page<postResponse> getPostsByUser(String userNickname, PageRequest pageable) {
if (userRepository.findByNickname(userNickname).isEmpty()) {
throw new UserNotFoundException();
}
else{
Long userId = userRepository.findByNickname(userNickname).get().getId();
Page<Post> postList = postRepository.findAllByUserId(userId, pageable);
log.info("[post] : {}의 모든 게시글 조회", userId);
log.info("[post] : {}의 모든 게시글 조회", userNickname);
return postList.map(postResponse::toDTO);
}
}
Expand Down

0 comments on commit 552a8fc

Please sign in to comment.