Skip to content

Commit

Permalink
Merge pull request #117 from Team-GAJI/feature/#116-READ-3POST-API/GA…
Browse files Browse the repository at this point in the history
…JI-132

✨ [feature] #116 - 스터디룸 게시글, 정보나눔 게시글, 트러블슈팅 게시글 구현 API
  • Loading branch information
mmingoo authored Aug 18, 2024
2 parents 61b4eee + 17b86b8 commit de0083a
Show file tree
Hide file tree
Showing 17 changed files with 427 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gaji.service.domain.roomBoard.entity.RoomInfo;

import gaji.service.domain.common.entity.BaseEntity;
import gaji.service.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -12,7 +13,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
public class InfoPostComment {
public class InfoPostComment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gaji.service.domain.roomBoard.entity.RoomPost;

import gaji.service.domain.common.entity.BaseEntity;
import gaji.service.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -12,7 +13,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
public class PostComment {
public class PostComment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gaji.service.domain.roomBoard.entity.RoomTrouble;

import gaji.service.domain.common.entity.BaseEntity;
import gaji.service.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -12,7 +13,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
public class TroublePostComment {
public class TroublePostComment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package gaji.service.domain.roomBoard.repository.RoomInfo;

import gaji.service.domain.roomBoard.entity.RoomInfo.InfoPostComment;
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;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface InfoPostCommentRepository extends JpaRepository<InfoPostComment, Long> {

@Query("SELECT c FROM InfoPostComment c " +
"LEFT JOIN FETCH c.replies " +
"WHERE c.roomInfoPost.id = :postId " +
"AND c.parentComment IS NULL " +
"ORDER BY c.createdAt ASC, c.id ASC")
Page<InfoPostComment> findOldestComments(@Param("postId") Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gaji.service.domain.roomBoard.repository.RoomInfo;

import gaji.service.domain.roomBoard.entity.RoomInfo.InfoPostComment;
import gaji.service.domain.roomBoard.entity.RoomInfo.RoomInfoPost;
import gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto;
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;
Expand All @@ -21,4 +23,5 @@ List<RoomPostResponseDto.InfoPostSummaryDto> findInfoPostSummariesForInfiniteScr
@Param("boardId") Long boardId,
@Param("lastPostId") Long lastPostId,
Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package gaji.service.domain.roomBoard.repository.RoomPost;

import gaji.service.domain.roomBoard.entity.RoomPost.PostComment;
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;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface PostCommentRepository extends JpaRepository<PostComment, Long> {
@Query("SELECT c FROM PostComment c " +
"LEFT JOIN FETCH c.replies " +
"WHERE c.roomPost.id = :postId " +
"AND c.parentComment IS NULL " +
"ORDER BY c.createdAt ASC, c.id ASC")
Page<PostComment> findOldestComments(@Param("postId") Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
package gaji.service.domain.roomBoard.repository.RoomTrouble;

import gaji.service.domain.roomBoard.entity.RoomTrouble.TroublePostComment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TroublePostCommentRepository extends JpaRepository<TroublePostComment, Long> {
@Query("SELECT c FROM TroublePostComment c " +
"LEFT JOIN FETCH c.replies " +
"WHERE c.roomTroublePost.id = :postId " +
"AND c.isReply = false " +
"AND (:lastCommentId IS NULL OR c.id > :lastCommentId) " +
"ORDER BY c.createdAt ASC, c.id ASC")
List<TroublePostComment> findCommentsWithReplies(@Param("postId") Long postId,
@Param("lastCommentId") Long lastCommentId,
Pageable pageable);

@Query("SELECT c FROM TroublePostComment c " +
"WHERE c.roomTroublePost.id = :postId " +
"AND c.isReply = false " +
"ORDER BY c.createdAt ASC, c.id ASC")
Page<TroublePostComment> findOldestComments(@Param("postId") Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import gaji.service.domain.roomBoard.entity.RoomInfo.InfoPostComment;
import gaji.service.domain.roomBoard.entity.RoomInfo.RoomInfoPost;
import gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

Expand All @@ -14,4 +16,8 @@ public interface RoomInfoPostQueryService {
InfoPostComment findPostCommentById(Long troublePostId);

List<RoomPostResponseDto.InfoPostSummaryDto> getNextPosts(Long boardId, Long lastPostId, int size);

RoomPostResponseDto.RoomInfoPostDetailDTO getPostDetail(Long postId, Long userId, int page, int size);

Page<RoomPostResponseDto.CommentWithRepliesDTO> getCommentsWithReplies(Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
import gaji.service.domain.roomBoard.repository.RoomInfo.InfoPostCommentRepository;
import gaji.service.domain.roomBoard.repository.RoomInfo.RoomInfoPostRepository;
import gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto;
import gaji.service.domain.studyMate.entity.StudyMate;
import gaji.service.domain.studyMate.service.StudyMateQueryService;
import gaji.service.global.exception.RestApiException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class RoomInfoPostQueryServiceImpl implements RoomInfoPostQueryService{

private final RoomInfoPostRepository roomInfoPostRepository;
private final InfoPostCommentRepository infoPostCommentRepository;

private final StudyMateQueryService studyMateQueryService;
@Override
public RoomInfoPost findInfoPostById(Long PostId){
return roomInfoPostRepository.findById(PostId)
Expand All @@ -46,4 +48,65 @@ public List<RoomPostResponseDto.InfoPostSummaryDto> getNextPosts(Long boardId, L
Pageable pageable = PageRequest.of(0, size, Sort.by(Sort.Direction.DESC, "createdAt"));
return roomInfoPostRepository.findInfoPostSummariesForInfiniteScroll(boardId, lastPostId,pageable);
}


@Override
public RoomPostResponseDto.RoomInfoPostDetailDTO getPostDetail(Long postId, Long userId, int page, int size) {
RoomInfoPost post = roomInfoPostRepository.findById(postId)
.orElseThrow(() ->new RestApiException( RoomPostErrorStatus._POST_NOT_FOUND));

StudyMate studyMate = studyMateQueryService.findByUserIdAndRoomId(userId, post.getRoomBoard().getRoom().getId());

RoomPostResponseDto.RoomInfoPostDetailDTO dto = new RoomPostResponseDto.RoomInfoPostDetailDTO();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
dto.setBody(post.getBody());
dto.setAuthorName(post.getStudyMate().getUser().getName());
dto.setCreatedAt(post.getCreatedAt());
dto.setViewCount(post.getViewCount());
dto.setLikeCount(post.getLikeCount());
dto.setBookmarkCount(post.getBookmarkCount());
dto.setLiked(post.getRoomInfoPostLikesList().stream()
.anyMatch(like -> like.getStudyMate().getId().equals(studyMate.getId())));
dto.setBookmarked(post.getRoomInfoPostBookmarkList().stream()
.anyMatch(bookmark -> bookmark.getStudyMate().getId().equals(studyMate.getId())));

Page<RoomPostResponseDto.CommentWithRepliesDTO> comments = getCommentsWithReplies(postId, PageRequest.of(page, size));
dto.setComments(comments);

return dto;
}

@Override
public Page<RoomPostResponseDto.CommentWithRepliesDTO> getCommentsWithReplies(Long postId, Pageable pageable) {
Page<InfoPostComment> commentPage = infoPostCommentRepository.findOldestComments(postId, pageable);

List<RoomPostResponseDto.CommentWithRepliesDTO> commentDTOs = commentPage.getContent().stream()
.map(this::convertToCommentWithRepliesDTO)
.collect(Collectors.toList());

return new PageImpl<>(commentDTOs, pageable, commentPage.getTotalElements());
}

private RoomPostResponseDto.CommentWithRepliesDTO convertToCommentWithRepliesDTO(InfoPostComment comment) {
RoomPostResponseDto.CommentWithRepliesDTO dto = new RoomPostResponseDto.CommentWithRepliesDTO();
dto.setId(comment.getId());
dto.setAuthorName(comment.getUser().getName());
dto.setBody(comment.getBody());
dto.setCreatedAt(comment.getCreatedAt());
dto.setReplies(comment.getReplies().stream()
.sorted(Comparator.comparing(InfoPostComment::getCreatedAt))
.map(this::convertToCommentDTO)
.collect(Collectors.toList()));
return dto;
}

private RoomPostResponseDto.CommentDTO convertToCommentDTO(InfoPostComment reply) {
RoomPostResponseDto.CommentDTO dto = new RoomPostResponseDto.CommentDTO();
dto.setId(reply.getId());
dto.setAuthorName(reply.getUser().getName());
dto.setBody(reply.getBody());
dto.setCreatedAt(reply.getCreatedAt());
return dto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import gaji.service.domain.roomBoard.entity.RoomPost.PostComment;
import gaji.service.domain.roomBoard.entity.RoomPost.RoomPost;
import gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

Expand All @@ -18,4 +20,8 @@ public interface RoomPostQueryService {
PostComment findCommentByCommentId(Long commentId);

PostComment findPostCommentById(Long troublePostId);

RoomPostResponseDto.RoomPostDetailDTO getPostDetail(Long postId, Long userId, int page, int size);

Page<RoomPostResponseDto.CommentWithRepliesDTO> getCommentsWithReplies(Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import gaji.service.domain.roomBoard.repository.RoomPost.RoomPostQueryRepository;
import gaji.service.domain.roomBoard.repository.RoomPost.RoomPostRepository;
import gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto;
import gaji.service.domain.studyMate.entity.StudyMate;
import gaji.service.domain.studyMate.service.StudyMateQueryService;
import gaji.service.global.exception.RestApiException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
Expand All @@ -23,6 +25,7 @@ public class RoomPostQueryServiceImpl implements RoomPostQueryService {
private final RoomPostQueryRepository roomPostQueryRepository;
private final RoomPostRepository roomPostRepository;
private final PostCommentRepository postCommentRepository;
private final StudyMateQueryService studyMateQueryService;

@Override
public List<RoomPostResponseDto.PostListDto> getTop3RecentPosts(Long roomId) {
Expand Down Expand Up @@ -52,4 +55,65 @@ public PostComment findPostCommentById(Long troublePostId) {
return postCommentRepository.findById(troublePostId)
.orElseThrow(() -> new RestApiException(RoomPostErrorStatus._NOT_FOUND_COMMENT));
}

@Override
public RoomPostResponseDto.RoomPostDetailDTO getPostDetail(Long postId, Long userId, int page, int size) {
RoomPost post = roomPostRepository.findById(postId)
.orElseThrow(() -> new RestApiException(RoomPostErrorStatus._POST_NOT_FOUND));

StudyMate studyMate = studyMateQueryService.findByUserIdAndRoomId(userId, post.getRoomBoard().getRoom().getId());

RoomPostResponseDto.RoomPostDetailDTO dto = new RoomPostResponseDto.RoomPostDetailDTO();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
dto.setBody(post.getBody());
dto.setAuthorName(post.getStudyMate().getUser().getName());
dto.setCreatedAt(post.getCreatedAt());
dto.setViewCount(post.getViewCount());
dto.setLikeCount(post.getLikeCount());
dto.setBookmarkCount(post.getBookmarkCount());
dto.setLiked(post.getRoomPostLikesList().stream()
.anyMatch(like -> like.getStudyMate().getId().equals(studyMate.getId())));
dto.setBookmarked(post.getRoomPostBookmarkList().stream()
.anyMatch(bookmark -> bookmark.getStudyMate().getId().equals(studyMate.getId())));

Page<RoomPostResponseDto.CommentWithRepliesDTO> comments = getCommentsWithReplies(postId, PageRequest.of(page, size));
dto.setComments(comments);

return dto;
}

@Override
public Page<RoomPostResponseDto.CommentWithRepliesDTO> getCommentsWithReplies(Long postId, Pageable pageable) {
Page<PostComment> commentPage = postCommentRepository.findOldestComments(postId, pageable);

List<RoomPostResponseDto.CommentWithRepliesDTO> commentDTOs = commentPage.getContent().stream()
.map(this::convertToCommentWithRepliesDTO)
.collect(Collectors.toList());

return new PageImpl<>(commentDTOs, pageable, commentPage.getTotalElements());
}

private RoomPostResponseDto.CommentWithRepliesDTO convertToCommentWithRepliesDTO(PostComment comment) {
RoomPostResponseDto.CommentWithRepliesDTO dto = new RoomPostResponseDto.CommentWithRepliesDTO();
dto.setId(comment.getId());
dto.setAuthorName(comment.getUser().getName());
dto.setBody(comment.getBody());
dto.setCreatedAt(comment.getCreatedAt());
dto.setReplies(comment.getReplies().stream()
.sorted(Comparator.comparing(PostComment::getCreatedAt))
.map(this::convertToCommentDTO)
.collect(Collectors.toList()));
return dto;
}

private RoomPostResponseDto.CommentDTO convertToCommentDTO(PostComment reply) {
RoomPostResponseDto.CommentDTO dto = new RoomPostResponseDto.CommentDTO();
dto.setId(reply.getId());
dto.setAuthorName(reply.getUser().getName());
dto.setBody(reply.getBody());
dto.setCreatedAt(reply.getCreatedAt());
return dto;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import gaji.service.domain.roomBoard.entity.RoomTrouble.TroublePostComment;
import gaji.service.domain.roomBoard.web.dto.RoomPostResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

Expand All @@ -11,4 +13,8 @@ public interface RoomTroublePostQueryService {
TroublePostComment findTroublePostCommentById(Long troublePostId);

List<RoomPostResponseDto.TroublePostSummaryDto> getNextTroublePosts(Long boardId, Long lastPostId, int size);

RoomPostResponseDto.TroublePostDetailDTO getPostDetail(Long postId, Long userId, int page, int size);

Page<RoomPostResponseDto.CommentWithRepliesDTO> getCommentsWithReplies(Long postId, Pageable pageable);
}
Loading

0 comments on commit de0083a

Please sign in to comment.