Skip to content

Commit

Permalink
Merge pull request #137 from CSID-DGU/backend/feature/crew
Browse files Browse the repository at this point in the history
BE: [feat] 갤러리 댓글 조회 #83
  • Loading branch information
Seoyoung2222 authored Dec 1, 2024
2 parents aa02d01 + 25ae13c commit d9ee8e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package RunningMachines.R2R.domain.crew.post.gallery.comment.repository;

import RunningMachines.R2R.domain.crew.post.entity.CrewPost;
import RunningMachines.R2R.domain.crew.post.entity.CrewPostComment;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CrewPostCommentRepository extends JpaRepository<CrewPostComment, Long> {
List<CrewPostComment> findByCrewPost(CrewPost crewPost);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -49,4 +51,21 @@ public Long createComment(Long crewId, Long postId, CrewPostCommentRequestDto re
return savedComment.getId();
}

@Transactional(readOnly = true)
public List<CrewPostCommentResponseDto> getComments(Long crewPostId) {
CrewPost crewPost = crewPostRepository.findById(crewPostId)
.orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다."));

List<CrewPostComment> comments = crewPostCommentRepository.findByCrewPost(crewPost);

return comments.stream()
.map(comment -> CrewPostCommentResponseDto.builder()
.commentId(comment.getId())
.content(comment.getContent())
.authorName(comment.getUser().getNickname())
.authorProfile(comment.getUser().getProfileImageUrl())
.createdAt(comment.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.build())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ public ResponseEntity<Long> createComment(@PathVariable Long crewId, @PathVariab
Long commentId = crewPostCommentService.createComment(crewId, postId, requestDto);
return ResponseEntity.ok(commentId);
}

@GetMapping("/{crewPostId}/comments")
public ResponseEntity<List<CrewPostCommentResponseDto>> getComments(@PathVariable Long crewId, @PathVariable Long crewPostId) {
List<CrewPostCommentResponseDto> comments = crewPostCommentService.getComments(crewPostId);
return ResponseEntity.ok(comments);
}
}

0 comments on commit d9ee8e0

Please sign in to comment.