Skip to content

Commit

Permalink
Merge pull request #76 from Leets-Official/feature-#75
Browse files Browse the repository at this point in the history
#75 Feat: 15개씩 게시글 조회하는 기능 추가
  • Loading branch information
jj0526 authored Jul 30, 2024
2 parents b8e7732 + 64baaee commit 05ff32d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package leets.weeth.domain.post.controller;

import leets.weeth.domain.post.dto.RequestPostDTO;
import leets.weeth.domain.post.dto.ResponsePostDTO;
import leets.weeth.domain.post.dto.PostDTO;
import leets.weeth.domain.post.service.PostService;
import leets.weeth.global.auth.annotation.CurrentUser;
import leets.weeth.global.common.error.exception.custom.InvalidAccessException;
Expand All @@ -28,20 +28,28 @@ public CommonResponse<String> createOrUpdate(@RequestPart(value = "requestPostDT
}

@GetMapping("")
public CommonResponse<List<ResponsePostDTO>> findAllPosts(){
List<ResponsePostDTO> posts = postService.findAllPosts();
public CommonResponse<List<PostDTO>> findAllPosts(){
List<PostDTO> posts = postService.findAllPosts();
return CommonResponse.createSuccess(posts);
}

@GetMapping("/load")
public CommonResponse<List<PostDTO>> loadPosts(@RequestParam(required = false) Long lastPostId) throws InvalidAccessException {
List<PostDTO> postsLoaded = postService.loadPosts(lastPostId);
return CommonResponse.createSuccess(postsLoaded);
}



@GetMapping("/myPosts")
public CommonResponse<List<ResponsePostDTO>> showMyPost(@CurrentUser Long userId){
List<ResponsePostDTO> myPost = postService.myPosts(userId);
public CommonResponse<List<PostDTO>> showMyPost(@CurrentUser Long userId){
List<PostDTO> myPost = postService.myPosts(userId);
return CommonResponse.createSuccess(myPost);
}

@GetMapping("/{postId}")
public CommonResponse<ResponsePostDTO> showPost(@PathVariable Long postId){
ResponsePostDTO newPost = postService.show(postId);
public CommonResponse<PostDTO> showPost(@PathVariable Long postId){
PostDTO newPost = postService.show(postId);
return CommonResponse.createSuccess(newPost);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@ToString
public class ResponsePostDTO {
public class PostDTO {

private Long id;
@NotBlank
Expand All @@ -30,8 +30,8 @@ public class ResponsePostDTO {
private Long totalComments;


public static ResponsePostDTO createResponsePostDTO(Post post) {
return ResponsePostDTO.builder()
public static PostDTO createResponsePostDTO(Post post) {
return PostDTO.builder()
.id(post.getId())
.name(post.getUser().getName())
.title(post.getTitle())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package leets.weeth.domain.post.repository;

import leets.weeth.domain.post.entity.Post;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.ArrayList;
import java.util.List;

public interface PostRepository extends JpaRepository<Post, Long> {
ArrayList<Post> findAll();
List<Post> findByUserId(Long userId, Sort sort);

@Query("SELECT MAX(p.id) FROM Post p")
Long findMaxPostId();

@Query("SELECT p FROM Post p WHERE p.id < :maxPostId ORDER BY p.id DESC")
List<Post> findRecentPosts(@Param("maxPostId") Long maxPostId, Pageable pageable);

}
34 changes: 27 additions & 7 deletions src/main/java/leets/weeth/domain/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import leets.weeth.domain.file.entity.File;
import leets.weeth.domain.file.service.FileService;
import leets.weeth.domain.post.dto.RequestPostDTO;
import leets.weeth.domain.post.dto.ResponsePostDTO;
import leets.weeth.domain.post.dto.PostDTO;
import leets.weeth.domain.post.entity.Post;
import leets.weeth.domain.post.repository.PostRepository;
import leets.weeth.domain.user.entity.User;
Expand All @@ -14,6 +14,8 @@
import leets.weeth.global.common.error.exception.custom.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -29,30 +31,30 @@ public class PostService {
private final FileService fileService;

//모든 게시물 가져오기
public List<ResponsePostDTO> findAllPosts() {
public List<PostDTO> findAllPosts() {
// 모든 게시물을 id에 대해 오름차순으로 조회
List<Post> posts = postRepository.findAll(Sort.by(Sort.Direction.ASC, "id"));
return posts.stream()
.map(ResponsePostDTO::createResponsePostDTO)
.map(PostDTO::createResponsePostDTO)
.collect(Collectors.toList());
}

// 특정 postId의 게시물만 조회
public ResponsePostDTO show(Long postId) {
public PostDTO show(Long postId) {
Post target = postRepository.findById(postId)
.orElseThrow(PostNotFoundException::new);

return ResponsePostDTO.createResponsePostDTO(target);
return PostDTO.createResponsePostDTO(target);
}

// 특정 유저(본인)의 게시물만 조회
public List<ResponsePostDTO> myPosts(Long userId){
public List<PostDTO> myPosts(Long userId){
// 특정 유저의 모든 게시물을 오름차순으로 조회
List<Post> myPosts = postRepository.findByUserId(userId, Sort.by(Sort.Direction.ASC, "id"));

// Post 리스트를 ResponsePostDTO 리스트로 변환
return myPosts.stream()
.map(ResponsePostDTO::createResponsePostDTO) // Post -> ResponsePostDTO 변환
.map(PostDTO::createResponsePostDTO) // Post -> ResponsePostDTO 변환
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -100,4 +102,22 @@ public void delete(Long postId, Long userId) throws InvalidAccessException {
}
postRepository.delete(deleted);
}

public List<PostDTO> loadPosts(Long lastPostId) throws InvalidAccessException {
Long maxPostId = postRepository.findMaxPostId();

if(lastPostId==null){ // 첫번째 요청인 경우
lastPostId = maxPostId + 1;
}
if(lastPostId <= 1 || lastPostId > maxPostId + 1){
throw new InvalidAccessException(); // postId가 1 이하이거나 최대값보다 클경우
}

Pageable pageable = PageRequest.of(0, 15); // 첫 페이지, 페이지당 15개 게시글
List<Post> posts = postRepository.findRecentPosts(lastPostId, pageable);
return posts.stream()
.map(PostDTO::createResponsePostDTO)
.toList();
}

}

0 comments on commit 05ff32d

Please sign in to comment.