Skip to content

Commit

Permalink
fix: Fix DiaryRecomment
Browse files Browse the repository at this point in the history
fix: Fix DiaryRecomment
  • Loading branch information
KellyKimHyeJin authored Aug 19, 2023
2 parents c65bd87 + 0f464f6 commit 6ba1b22
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public ResponseEntity<DiaryCommentResponseDto.UpdateDiaryCommentDto> updateComme
return ResponseEntity.ok(DiaryCommentResponseDto.UpdateDiaryCommentDto.builder().commentId(diaryComment.getId()).build());
}

// 댓글 조회시 대댓글도 조회
@PreAuthorize("isAuthenticated()")
@GetMapping("/diary/{postId}/comment")
public ResponseEntity<List<DiaryCommentResponseDto.CommentDto>> getComment(@PathVariable(name = "postId")Long postId){
List<DiaryComment> commentList = diaryCommentService.findAllByDiary(postId);
return ResponseEntity.ok(diaryCommentService.getCommentList(commentList));
return ResponseEntity.ok(diaryCommentService.getCommentList(postId));
}

@PreAuthorize("isAuthenticated()")
Expand All @@ -59,14 +59,6 @@ public ResponseEntity<DiaryCommentResponseDto.CreateDiaryCommentDto> createReCom
return ResponseEntity.ok(DiaryCommentResponseDto.CreateDiaryCommentDto.builder().commentId(diaryComment.getId()).build());
}

// 대댓글 조회
@PreAuthorize("isAuthenticated()")
@GetMapping("/diary/{postId}/comment/{commentId}/recomment")
public ResponseEntity<List<DiaryCommentResponseDto.CommentDto>> getRecomment(@PathVariable(name = "postId")Long postId,
@PathVariable(name = "commentId")Long commentId){
List<DiaryComment> commentList = diaryCommentService.findAllByDiaryAndComment(postId, commentId);
return ResponseEntity.ok(diaryCommentService.getCommentList(commentList));
}

@PreAuthorize("isAuthenticated()")
@PatchMapping("/diary/{postId}/comment/{commentId}/recomment/{recommentId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static class CommentDto{
private Long commentId;
private String content;
private int recommentCount;
private List<DiaryCommentResponseDto.ReCommentDto> recomments;
private MemberResponseDto.MemberSortDto user;
private LocalDateTime createdDate;


}

@Builder
Expand All @@ -40,6 +40,18 @@ public static class UpdateDiaryCommentDto{

}

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class ReCommentDto{
private Long recommentId;
private String content;
private MemberResponseDto.MemberSortDto user;
private LocalDateTime createdDate;

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface DiaryCommentRepository extends JpaRepository<DiaryComment, Long

int countAllByParent(DiaryComment parent);



List<DiaryComment> findAllByDiaryAndParent(Diary diary, DiaryComment parent);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ public DiaryComment update(Long diaryId, Long diaryCommentId, DiaryCommentReques
return diaryComment;
}

public List<DiaryComment> findAllByDiary(Long diaryId){
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(()-> new NotFoundException("Diary를 찾을 수 없습니다."));
return diaryCommentRepository.findAllByDiary(diary);
}

@Transactional
public void delete(Long diaryId, Long diaryCommentId){
Diary diary = diaryRepository.findById(diaryId)
Expand Down Expand Up @@ -98,15 +92,30 @@ public DiaryComment updateReComment(Long diaryId, Long commentId, Long recomment

}

public List<DiaryCommentResponseDto.CommentDto> getCommentList(List<DiaryComment> diaryCommentList){
public List<DiaryCommentResponseDto.CommentDto> getCommentList(Long postId){
List<DiaryCommentResponseDto.CommentDto> list = new ArrayList<>();
Diary diary = diaryRepository.findById(postId)
.orElseThrow(()-> new NotFoundException("일기장이 존재하지 않습니다."));
List<DiaryComment> diaryCommentList = diaryCommentRepository.findAllByDiaryAndParent(diary, null);
for(DiaryComment c: diaryCommentList){
Member member = c.getMember();
int reCommentCount = diaryCommentRepository.countAllByParent(c.getParent());
int reCommentCount = diaryCommentRepository.countAllByParent(c);
List<DiaryComment> recommentList = diaryCommentRepository.findAllByDiaryAndParent(diary, c);
List<DiaryCommentResponseDto.ReCommentDto> recommentResultList = new ArrayList<>();
for(DiaryComment re: recommentList){
DiaryCommentResponseDto.ReCommentDto result = DiaryCommentResponseDto.ReCommentDto.builder()
.recommentId(re.getId())
.content(re.getDiaryCommentContent())
.user(MemberResponseDto.MemberSortDto.builder().memberId(re.getMember().getMemberId()).profileImgURL(re.getMember().getProfileImg()).name(re.getMember().getName()).nickname(re.getMember().getNickname()).build())
.createdDate(re.getCreatedAt())
.build();
recommentResultList.add(result);
}
DiaryCommentResponseDto.CommentDto comment = DiaryCommentResponseDto.CommentDto.builder()
.commentId(c.getId())
.content(c.getDiaryCommentContent())
.recommentCount(reCommentCount)
.recomments(recommentResultList)
.user(MemberResponseDto.MemberSortDto.builder().memberId(member.getMemberId()).profileImgURL(member.getProfileImg()).name(member.getName()).nickname(member.getNickname()).build())
.createdDate(c.getCreatedAt())
.build();
Expand All @@ -115,14 +124,6 @@ public List<DiaryCommentResponseDto.CommentDto> getCommentList(List<DiaryComment
return list;
}

public List<DiaryComment> findAllByDiaryAndComment(Long diaryId, Long commentId){
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(()-> new NotFoundException("Diary를 찾을 수 없습니다."));
DiaryComment parent = diaryCommentRepository.findByIdAndDiary(commentId, diary);
List<DiaryComment> reCommentList = diaryCommentRepository.findAllByDiaryAndParent(diary, parent);
return reCommentList;
}

@Transactional
public void deleteRecomment(Long diaryId, Long commentId, Long recommentId){
Diary diary = diaryRepository.findById(diaryId)
Expand Down

0 comments on commit 6ba1b22

Please sign in to comment.