-
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.
Merge pull request #72 from LIKELION-TEAM4-HACKATHON/feature/review-d…
…etail
- Loading branch information
Showing
9 changed files
with
195 additions
and
15 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
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); | ||
} | ||
|
||
@DeleteMapping("/api/reviews/{reviewId}/comments/{commentId}") | ||
public ResponseEntity<Map<String, String>> deleteComment(@PathVariable("commentId") Long commentId, @AuthenticationPrincipal UserPrinciple userPrinciple) { | ||
commentService.deleteComment(userPrinciple.getEmail(), commentId); | ||
return ResponseEntity.ok(Map.of("message", "댓글 삭제 성공")); | ||
} | ||
|
||
} |
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
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(); | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
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; | ||
import java.util.Optional; | ||
|
||
@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(); | ||
} | ||
|
||
// 후기 댓글 삭제 | ||
@Transactional | ||
public void deleteComment(String email, Long commentId) { | ||
ReviewComment comment = findCommentById(commentId); | ||
Member member = findMemberByEmail(email); | ||
Review review = findReviewById(comment.getReview().getReviewId()); | ||
|
||
member.getReviewComments().remove(comment); | ||
review.getReviewComments().remove(comment); | ||
reviewCommentRepository.delete(comment); | ||
} | ||
|
||
private ReviewComment findCommentById(Long commentId) { | ||
return reviewCommentRepository.findById(commentId).orElseThrow(() -> { | ||
log.info("해당 후기 댓글이 존재하지 않음."); | ||
return new IllegalArgumentException("해당 후기 댓글이 존재하지 않습니다."); | ||
}); | ||
} | ||
|
||
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 { | ||
|
||
} |