Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 피드(게시글 목록) 조회 API #36

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;
import treehouse.server.api.post.presentation.dto.PostRequestDTO;
import treehouse.server.api.post.presentation.dto.PostResponseDTO;
import treehouse.server.api.member.business.MemberMapper;
Expand All @@ -27,6 +28,7 @@ public static PostResponseDTO.getPostDetails toGetPostDetails(Post post) {
.pictureUrlList(post.getPostImageList().stream()
.map(PostImage::getImageUrl).toList()
)
.commentCount(post.getCommentList().size())
// .reactionList() // Reaction 기능 개발 이후 수정
.postedAt(TimeFormatter.format(post.getCreatedAt()))
.build();
Expand Down Expand Up @@ -55,4 +57,5 @@ public static PostResponseDTO.createPostResult toCreatePostResult (Post post){
.postId(post.getId())
.build();
}

}
26 changes: 26 additions & 0 deletions src/main/java/treehouse/server/api/post/business/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import treehouse.server.api.member.implementation.MemberQueryAdapter;
Expand All @@ -18,6 +21,7 @@
import treehouse.server.global.entity.treeHouse.TreeHouse;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
Expand Down Expand Up @@ -60,4 +64,26 @@ public PostResponseDTO.createPostResult createPost(User user, PostRequestDTO.cre
postImageCommandAdapter.savePostImageList(postImageList);
return PostMapper.toCreatePostResult(postCommandAdapter.savePost(post));
}

/**
* 게시글 목록 조회
* @param user
* @param treehouseId - 게시글 정보에 표시할 memberBranch을 계산하고 감정표현의 isPushed 상태를 반환하기 위해 user와 treehouseId 사용
* @return List<PostResponseDTO.getPostDetails>
*/
@Transactional(readOnly = true)
public List<PostResponseDTO.getPostDetails> getPosts(User user, Long treehouseId, int page){
// TODO 신고한 게시물과 탈퇴 및 차단한 작성자의 게시물은 제외하는 로직 추가

TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(treehouseId);

Pageable pageable = PageRequest.of(page, 10);
Page<Post> postsPage = postQueryAdapter.findAllByTreehouse(treehouse, pageable);

List<PostResponseDTO.getPostDetails> postDtos = postsPage.getContent().stream()
.map(PostMapper::toGetPostDetails)
.collect(Collectors.toList());

return postDtos;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package treehouse.server.api.post.implement;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import treehouse.server.api.post.persistence.PostRepository;
import treehouse.server.global.annotations.Adapter;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.treeHouse.TreeHouse;
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.PostException;


@Adapter
@RequiredArgsConstructor
public class PostQueryAdapter {
Expand All @@ -16,4 +20,8 @@ public class PostQueryAdapter {
public Post findById(Long postId) {
return postRepository.findById(postId).orElseThrow(() -> new PostException(GlobalErrorCode.POST_NOT_FOUND));
}

public Page<Post> findAllByTreehouse(TreeHouse treehouse, Pageable pageable) {
return postRepository.findAllByTreeHouse(treehouse, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package treehouse.server.api.post.persistence;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.treeHouse.TreeHouse;


public interface PostRepository extends JpaRepository<Post, Long> {
Page<Post> findAllByTreeHouse(TreeHouse treehouse, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import treehouse.server.global.entity.User.User;
import treehouse.server.global.security.handler.annotation.AuthMember;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
Expand All @@ -35,7 +37,7 @@ public CommonResponse<PostResponseDTO.getPostDetails> getPostDetails(
return CommonResponse.onSuccess(postService.getPostDetails(user, postId, treehouseId));
}

@PostMapping("/")
@PostMapping("/posts")
@Operation(summary = "게시글 작성 🔑", description = "게시글 작성 API 입니다.")
public CommonResponse<PostResponseDTO.createPostResult> createPost(
@PathVariable(name = "treehouseId") Long treehouseId,
Expand All @@ -44,4 +46,14 @@ public CommonResponse<PostResponseDTO.createPostResult> createPost(
){
return CommonResponse.onSuccess(postService.createPost(user,request, treehouseId));
}

@GetMapping
@Operation(summary = "게시글 목록 조회 🔑", description = "트리하우스의 게시글 목록을 조회합니다.")
public CommonResponse<List<PostResponseDTO.getPostDetails>> getPosts(
@PathVariable Long treehouseId,
@RequestParam(defaultValue = "0") int page,
@AuthMember @Parameter(hidden = true) User user
){
return CommonResponse.onSuccess(postService.getPosts(user, treehouseId, page));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static class getPostDetails {
private Long postId;
private String context;
private List<String> pictureUrlList;
private Integer commentCount;
// private List<ReactionResponseDto.getReaction> reactionList; Reaction 기능 개발 이후 적용
private String postedAt;
}
Expand All @@ -36,4 +37,5 @@ public static class createPostResult{
@JsonProperty("postId")
private Long postId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Comment extends BaseDateTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
private Member writer;

@JoinColumn(name = "feedId")
@JoinColumn(name = "postId")
@ManyToOne(fetch = FetchType.LAZY)
private Post post;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,69 @@
@RequiredArgsConstructor
public enum GlobalErrorCode implements BaseErrorCode{

// 401 Unauthorized - 권한 없음
// AUTH + 401 Unauthorized - 권한 없음
TOKEN_EXPIRED(UNAUTHORIZED, "AUTH401_1", "인증 토큰이 만료 되었습니다. 토큰을 재발급 해주세요"),
INVALID_TOKEN(UNAUTHORIZED, "AUTH401_2", "인증 토큰이 유효하지 않습니다."),
INVALID_REFRESH_TOKEN(UNAUTHORIZED, "AUTH401_3", "리프레시 토큰이 유효하지 않습니다."),
REFRESH_TOKEN_EXPIRED(UNAUTHORIZED, "AUTH401_4", "리프레시 토큰이 만료 되었습니다."),
AUTHENTICATION_REQUIRED(UNAUTHORIZED, "AUTH401_5", "인증 정보가 유효하지 않습니다."),
LOGIN_REQUIRED(UNAUTHORIZED, "AUTH401_6", "로그인이 필요한 서비스입니다."),

// 403 Forbidden - 인증 거부
// AUTH + 403 Forbidden - 인증 거부
AUTHENTICATION_DENIED(FORBIDDEN, "AUTH403_1", "인증이 거부 되었습니다."),

// 404 Not Found - 찾을 수 없음
// AUTH + 404 Not Found - 찾을 수 없음
REFRESH_TOKEN_NOT_FOUND(NOT_FOUND, "AUTH404_1", "리프레시 토큰이 존재하지 않습니다."),

// 500 Server Error
// GLOBAL + 500 Server Error
SERVER_ERROR(INTERNAL_SERVER_ERROR, "GLOBAL500_1", "서버 에러, 서버 개발자에게 알려주세요."),

// Args Validation Error
// GLOBAL + Args Validation Error
BAD_ARGS_ERROR(BAD_REQUEST, "GLOBAL400_1", "request body의 validation이 실패했습니다. 응답 body를 참고해주세요"),

// Member
// 400 BAD_REQUEST - 잘못된 요청
// USER + 400 BAD_REQUEST - 잘못된 요청
NOT_VALID_PHONE_NUMBER(BAD_REQUEST, "USER400_1", "유효하지 않은 전화번호 입니다."),

// 401 Unauthorized - 권한 없음
// USER + 401 Unauthorized - 권한 없음

// 403 Forbidden - 인증 거부
// USER + 403 Forbidden - 인증 거부

// 404 Not Found - 찾을 수 없음
// USER + 404 Not Found - 찾을 수 없음
MEMBER_NOT_FOUND(NOT_FOUND, "USER404_1", "등록된 사용자 정보가 없습니다."),


// 409 CONFLICT : Resource 를 찾을 수 없음
// USER + 409 CONFLICT : Resource 를 찾을 수 없음
DUPLICATE_PHONE_NUMBER(CONFLICT, "USER409_1", "중복된 전화번호가 존재합니다."),

//Profile
//404 Not Found - 찾을 수 없음
// MEMBER + 404 Not Found - 찾을 수 없음
PROFILE_NOT_FOUND(NOT_FOUND, "MEMBER404_1", "존재하지 않는 프로필입니다."),
AVAILABLE_PROFILE_NOT_FOUND(NOT_FOUND, "MEMBER404_2", "현재 선택된 프로필이 없습니다."),

//Treehouse
//404 Not Found - 찾을 수 없음
// TREEHOUSE + 404 Not Found - 찾을 수 없음
TREEHOUSE_NOT_FOUND(NOT_FOUND, "TREEHOUSE404_1", "존재하지 않는 트리입니다."),

//Invitation
//404 Not Found - 찾을 수 없음
// INVITATION + 404 Not Found - 찾을 수 없음
INVITATION_NOT_FOUND(NOT_FOUND, "INVITATION404_1", "존재하지 않는 초대장입니다."),

//Post
//404 Not Found - 찾을 수 없음
// POST + 404 Not Found - 찾을 수 없음
POST_NOT_FOUND(NOT_FOUND, "POST404_1", "존재하지 않는 게시글입니다."),

//Comment
//404 Not Found - 찾을 수 없음
// COMMENT + 404 Not Found - 찾을 수 없음
COMMENT_NOT_FOUND(NOT_FOUND, "COMMENT404_1", "존재하지 않는 댓글입니다."),

//Reply
//404 Not Found - 찾을 수 없음
// REPLY + 404 Not Found - 찾을 수 없음
REPLY_NOT_FOUND(NOT_FOUND, "REPLY404_1", "존재하지 않는 답글입니다."),

//Branch
//404 Not Found - 찾을 수 없음
// BRANCH + 404 Not Found - 찾을 수 없음
BRANCH_NOT_FOUND(NOT_FOUND, "BRANCH404_1", "브랜치 정보를 찾을 수 없습니다."),

//Notification
//404 Not Found - 찾을 수 없음

// NOTIFICATION + 404 Not Found - 찾을 수 없음
NOTIFICATION_NOT_FOUND(NOT_FOUND, "NOTIFICATION", "존재하지 않는 알림입니다."),

//FeignClient
//FEIGN + 400 BAD_REQUEST - 잘못된 요청
FEIGN_CLIENT_ERROR_400(BAD_REQUEST, "FEIGN400", "feignClient 에서 400번대 에러가 발생했습니다."),

//FEIGN + 500 INTERNAL_SERVER_ERROR - 서버 에러
FEIGN_CLIENT_ERROR_500(INTERNAL_SERVER_ERROR, "FEIGN500", "feignClient 에서 500번대 에러가 발생했습니다."),

// NCP Phone Auth
Expand Down
Loading