Skip to content
Merged

feat #115

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
60 changes: 60 additions & 0 deletions src/main/java/com/example/prdoit/controller/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.prdoit.service.comment.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand Down Expand Up @@ -80,4 +81,63 @@ public ResponseEntity<String> updateReply(@RequestBody CommentReplyPatchDto comm
return ResponseEntity.internalServerError().body("대댓글 수정에 실패했습니다.");
}
}

@GetMapping("/community/{communityId}")
public ResponseEntity<Object> getCommunityComments(@PathVariable String communityId) {
try {
return ResponseEntity.ok(commentService.getCommunityCommentList(communityId));
} catch (CustomException e){
return ResponseEntity.badRequest().body("댓글 조회에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("댓글 조회에 실패했습니다.");
}
}

@PostMapping("/community")
public ResponseEntity<Object> createCommunityComment(@RequestBody CommunityCommentPostDto communityCommentPostDto) {
try {
commentService.postCommunityComment(communityCommentPostDto);
return ResponseEntity.ok("댓글이 성공적으로 작성되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("댓글 작성에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("댓글 작성에 실패했습니다.");
}
}

@PostMapping("/community/reply")
public ResponseEntity<Object> createCommunityReply(@RequestBody CommunityCommentReplyPostDto communityCommentReplyPostDto) {
try {
commentService.postCommunityCommentReply(communityCommentReplyPostDto);
return ResponseEntity.ok("대댓글이 성공적으로 작성되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("대댓글 작성에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("대댓글 작성에 실패했습니다.");
}
}

@PatchMapping("/community")
public ResponseEntity<String> updateCommunityComment(@RequestBody CommunityCommentPatchDto communityCommentPatchDto) {
try {
commentService.patchCommunityComment(communityCommentPatchDto);
return ResponseEntity.ok("댓글이 성공적으로 수정되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("댓글 수정에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("댓글 수정에 실패했습니다.");
}
}

@PatchMapping("/community/reply")
public ResponseEntity<String> updateCommunityReply(@RequestBody CommunityCommentReplyPatchDto communityCommentReplyPatchDto) {
try {
commentService.patchCommunityCommentReply(communityCommentReplyPatchDto);
return ResponseEntity.ok("대댓글이 성공적으로 수정되었습니다!");
} catch (CustomException e) {
return ResponseEntity.badRequest().body("대댓글 수정에 실패했습니다.");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("대댓글 수정에 실패했습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.prdoit.dto.comment;

import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommunityCommentPatchDto {

private int commentId;
private String commentContent;
private LocalDateTime commentDate;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.prdoit.dto.comment;

import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommunityCommentPostDto {

private String communityId;
private String commentContent;
private String userId;
private LocalDateTime commentDate;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.prdoit.dto.comment;

import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommunityCommentReplyPatchDto {

private int commentReplyId;
private String commentReplyContent;
private LocalDateTime commentReplyDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.prdoit.dto.comment;

import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommunityCommentReplyPostDto {

private int commentId;
private String commentReplyContent;
private String userId;
private LocalDateTime commentReplyDate;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.prdoit.dto.comment;


import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommunityCommentReplyResponseDto {

private int commentReplyId;
private String commentReplyContent;
private String commentReplyNickname;
private String userId;
private LocalDateTime commentReplyDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.prdoit.dto.comment;

import lombok.*;

import java.time.LocalDateTime;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommunityCommentResponseDto {

private int commentId;
private String commentContent;
private String userId;
private String commentNickname;
private LocalDateTime commentDate;
private List<CommunityCommentReplyResponseDto> communityCommentReplyResponseDtoList;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public interface CommentService {
void patchComment(CommentPatchDto commentPatchDto);

void patchCommentReply(CommentReplyPatchDto commentReplyPatchDto);

List<CommunityCommentResponseDto> getCommunityCommentList(String communityId);

void postCommunityComment(CommunityCommentPostDto communityCommentPostDto);

void postCommunityCommentReply(CommunityCommentReplyPostDto communityCommentReplyPostDto);

void patchCommunityComment(CommunityCommentPatchDto communityCommentPatchDto);

void patchCommunityCommentReply(CommunityCommentReplyPatchDto communityCommentReplyPatchDto);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CommentServiceImpl implements CommentService {
private final ContentTableRepository contentTableRepository;
private final NotificationTableRepository notificationTableRepository;
private final IdTableRepository idTableRepository;
private final CommunityTableRepository communityTableRepository;

// 1. 댓글 목록 조회 (GET)
@Override
Expand Down Expand Up @@ -156,4 +157,135 @@ public void patchCommentReply(CommentReplyPatchDto commentReplyPatchDto) {
throw new CustomException("대댓글 수정 중 오류가 발생했습니다.");
}
}

@Override
public List<CommunityCommentResponseDto> getCommunityCommentList(String communityId) {
log.info("[getCommunityCommentList] 커뮤니티 댓글 목록 조회 시작 - Community ID: {}", communityId);

CommunityTable communityTable = communityTableRepository.findById(communityId)
.orElseThrow(() -> new CustomException("해당 커뮤니티가 없습니다. ID = " + communityId));

try{
return communityTable.getCommentTable().stream().map(comment -> CommunityCommentResponseDto.builder()
.commentId(comment.getCommentId())
.commentContent(comment.getCommentContent())
.userId(comment.getContentId().getUserId().getId())
.commentNickname(comment.getCommentNickname())
.commentDate(comment.getCommentDate())
.communityCommentReplyResponseDtoList(comment.getCommentCommentTable().stream()
.map(reply -> CommunityCommentReplyResponseDto.builder()
.commentReplyId(reply.getCommentCommentId())
.commentReplyContent(reply.getCommentCommentContent())
.commentReplyNickname(reply.getUserNickname())
.commentReplyDate(reply.getCommentCommentDate())
.userId(reply.getCommentId().getContentId().getUserId().getId())
.build())
.collect(Collectors.toList()))
.build()).collect(Collectors.toList());
} catch (Exception e){
log.error("[getCommunityCommentList] 커뮤니티 댓글 목록 조회 중 오류 발생 - {}", e.getMessage());
throw new CustomException("커뮤니티 댓글 목록 조회 중 오류가 발생했습니다.");
}
}

@Override
public void postCommunityComment(CommunityCommentPostDto communityCommentPostDto) {
log.info("[postCommunityComment] 커뮤니티 댓글 등록 시작 - Community ID: {}", communityCommentPostDto.getCommunityId());

CommunityTable communityTable = communityTableRepository.findById(communityCommentPostDto.getCommunityId())
.orElseThrow(() -> new CustomException("해당 커뮤니티가 없습니다."));

IdTable idTable = idTableRepository.findById(communityCommentPostDto.getUserId()).orElseThrow(()-> new CustomException("해당 아이디가 존재하지 않습니다."));

try {
commentTableRepository.save(CommentTable.builder()
.commentContent(communityCommentPostDto.getCommentContent())
.commentNickname(idTable.getNickname())
.commentDate(communityCommentPostDto.getCommentDate())
.communityId(communityTable)
.build());

notificationTableRepository.save(NotificationTable.builder()
.notificationContent("커뮤니티 댓글이 작성되었습니다.")
.contentId(communityTable.getCommunityId())
.userId(communityTable.getId())
.isRead(0)
.isContent(1)
.build());

log.info("[postCommunityComment] 커뮤니티 댓글 등록을 완료했습니다.");
} catch (Exception e){
log.error("[postCommunityComment] 커뮤니티 댓글 등록 중 오류 발생 - {}", e.getMessage());
throw new CustomException("커뮤니티 댓글 등록 중 오류가 발생했습니다.");
}
}

@Override
public void postCommunityCommentReply(CommunityCommentReplyPostDto communityCommentReplyPostDto) {
log.info("[postCommunityCommentReply] 커뮤니티 대댓글 등록 시작");

CommentTable parentComment = commentTableRepository.findById(communityCommentReplyPostDto.getCommentId())
.orElseThrow(() -> new CustomException("해당 댓글이 없습니다."));

IdTable idTable = idTableRepository.findById(communityCommentReplyPostDto.getUserId()).orElseThrow(()->new CustomException("해당아이디가 존재하지 않습니다."));
try {
commentCommentTableRepository.save(CommentCommentTable.builder()
.commentCommentContent(communityCommentReplyPostDto.getCommentReplyContent())
.userNickname(idTable.getNickname())
.commentCommentDate(communityCommentReplyPostDto.getCommentReplyDate())
.commentId(parentComment)
.build());

notificationTableRepository.save(NotificationTable.builder()
.notificationContent("커뮤니티 대댓글이 작성되었습니다.")
.contentId(parentComment.getCommunityId().getCommunityId())
.userId(idTableRepository.findByNickname(parentComment.getCommentNickname()))
.isRead(0)
.isContent(1)
.build());

log.info("[postCommunityCommentReply] 커뮤니티 대댓글 등록을 완료했습니다.");
} catch (Exception e){
log.error("[postCommunityCommentReply] 커뮤니티 대댓글 등록 중 오류 발생 - {}", e.getMessage());
throw new CustomException("커뮤니티 대댓글 등록 중 오류가 발생했습니다.");
}
}

@Override
public void patchCommunityComment(CommunityCommentPatchDto communityCommentPatchDto) {
log.info("[patchCommunityComment] 커뮤니티 댓글 수정 시작 - Comment ID: {}", communityCommentPatchDto.getCommentId());

CommentTable comment = commentTableRepository.findById(communityCommentPatchDto.getCommentId())
.orElseThrow(() -> new CustomException("해당 댓글이 없습니다."));

try{
comment.setCommentContent(communityCommentPatchDto.getCommentContent());
comment.setCommentDate(communityCommentPatchDto.getCommentDate());
commentTableRepository.save(comment);

log.info("[patchCommunityComment] 커뮤니티 댓글 수정이 완료되었습니다.");
} catch (Exception e){
log.error("[patchCommunityComment] 커뮤니티 댓글 수정 중 오류 발생 - {}", e.getMessage());
throw new CustomException("커뮤니티 댓글 수정 중 오류가 발생했습니다.");
}
}

@Override
public void patchCommunityCommentReply(CommunityCommentReplyPatchDto communityCommentReplyPatchDto) {
log.info("[patchCommunityCommentReply] 커뮤니티 대댓글 수정 시작 - CommentReply ID: {}", communityCommentReplyPatchDto.getCommentReplyId());

CommentCommentTable reply = commentCommentTableRepository.findById(communityCommentReplyPatchDto.getCommentReplyId())
.orElseThrow(() -> new CustomException("해당 대댓글이 없습니다."));

try{
reply.setCommentCommentContent(communityCommentReplyPatchDto.getCommentReplyContent());
reply.setCommentCommentDate(communityCommentReplyPatchDto.getCommentReplyDate());
commentCommentTableRepository.save(reply);

log.info("[patchCommunityCommentReply] 커뮤니티 대댓글 수정이 완료되었습니다.");
} catch (Exception e){
log.error("[patchCommunityCommentReply] 커뮤니티 대댓글 수정 중 오류 발생 - {}", e.getMessage());
throw new CustomException("커뮤니티 대댓글 수정 중 오류가 발생했습니다.");
}
}
}