-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/likelion/MZConnent/api/review/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/likelion/MZConnent/dto/review/request/SaveCommentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/likelion/MZConnent/dto/review/response/SaveCommentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/likelion/MZConnent/service/review/CommentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("회원이 존재하지 않습니다."); | ||
}); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/test/java/likelion/MZConnent/service/review/CommentServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
class CommentServiceTest { | ||
|
||
} |