Skip to content

Commit

Permalink
feat: 후기 댓글 작성 기능 추가
Browse files Browse the repository at this point in the history
후기 댓글 작성 기능 추가함
  • Loading branch information
chaeyoungeee committed Jul 30, 2024
1 parent 491af09 commit 4406b21
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/likelion/MZConnent/api/review/CommentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package likelion.MZConnent.api.review;

import likelion.MZConnent.dto.review.request.SaveCommentRequest;
import likelion.MZConnent.dto.review.response.SaveCommentResponse;
import likelion.MZConnent.jwt.principle.UserPrinciple;
import likelion.MZConnent.service.review.CommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequiredArgsConstructor
@Slf4j
public class CommentController {
private final CommentService commentService;

@PostMapping("/api/reviews/{reviewId}/comments")
public ResponseEntity<SaveCommentResponse> saveComment(@RequestBody Map<String, String> request, @PathVariable("reviewId") Long reviewId, @AuthenticationPrincipal UserPrinciple userPrinciple) {
SaveCommentResponse comment = commentService.saveComment(userPrinciple.getEmail(), reviewId, request.get("content"));

log.info("댓글 작성: {}", comment);

return ResponseEntity.ok(comment);
}

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

import likelion.MZConnent.domain.member.Member;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -12,6 +13,7 @@ public class MemberProfileDto {
private String username;
private String profileImageUrl = ""; // TODO: 기본 이미지로 초기화

@Builder
public MemberProfileDto(Member member) {
this.userId = member.getId();
this.username = member.getUsername();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package likelion.MZConnent.dto.review.request;

import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.review.ReviewComment;
import likelion.MZConnent.dto.review.response.SaveCommentResponse;
import lombok.*;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class SaveCommentRequest {
private Long reviewId;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package likelion.MZConnent.dto.review.response;

import likelion.MZConnent.domain.review.ReviewComment;
import likelion.MZConnent.dto.member.MemberProfileDto;
import lombok.*;

@Getter
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class SaveCommentResponse {
private Long commentId;
private Long reviewId;
private MemberProfileDto commenter;
private String content;


@Builder
public SaveCommentResponse(ReviewComment comment) {
this.commentId = comment.getCommentId();
this.reviewId = comment.getReview().getReviewId();
this.commenter = MemberProfileDto.builder()
.member(comment.getMember()).build();
this.content = comment.getContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package likelion.MZConnent.service.review;

import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.review.Review;
import likelion.MZConnent.domain.review.ReviewComment;
import likelion.MZConnent.dto.culture.request.CreateCultureRequest;
import likelion.MZConnent.dto.review.request.SaveCommentRequest;
import likelion.MZConnent.dto.review.response.SaveCommentResponse;
import likelion.MZConnent.repository.member.MemberRepository;
import likelion.MZConnent.repository.review.ReviewCommentRepository;
import likelion.MZConnent.repository.review.ReviewRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
@RequiredArgsConstructor
public class CommentService {
private final MemberRepository memberRepository;
private final ReviewCommentRepository reviewCommentRepository;
private final ReviewRepository reviewRepository;

@Transactional
public SaveCommentResponse saveComment(String email, Long reviewId, String content) {
Member member = findMemberByEmail(email);
Review review = findReviewById(reviewId);

ReviewComment comment = ReviewComment.builder()
.content(content)
.createdDate(LocalDateTime.now())
.member(member)
.review(review)
.build();

reviewCommentRepository.save(comment);

return SaveCommentResponse.builder().comment(comment).build();
}

private Review findReviewById(Long reviewId ) {
return reviewRepository.findById(reviewId).orElseThrow(() -> {
log.info("후기가 존재하지 않음.");
return new IllegalArgumentException("후기가 존재하지 않습니다.");
});
}

private Member findMemberByEmail(String email) {
return memberRepository.findByEmail(email).orElseThrow(() -> {
log.info("회원이 존재하지 않음.");
return new IllegalArgumentException("회원이 존재하지 않습니다.");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import static org.junit.jupiter.api.Assertions.*;
class CommentServiceTest {

}

0 comments on commit 4406b21

Please sign in to comment.