Skip to content

Commit

Permalink
⚡Feat: 댓글 관련 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
eunxeum committed Nov 23, 2024
1 parent fed1790 commit 0c0f02a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import univ.yesummit.domain.comment.api.dto.request.CommentSaveReqDto;
import univ.yesummit.domain.comment.api.dto.request.CommentUpdateReqDto;
import univ.yesummit.domain.comment.application.CommentService;
import univ.yesummit.global.resolver.LoginUser;
import univ.yesummit.global.resolver.User;

@RestController
@RequiredArgsConstructor
Expand All @@ -23,11 +25,11 @@ public class CommentController {
@Operation(summary = "댓글 등록", description = "댓글을 등록합니다.")
@ApiResponse(responseCode = "200", description = "댓글 등록 성공")
@ApiResponse(responseCode = "401", description = "인증 실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN")))
@PostMapping("/")
public ResponseEntity<String> commentSave(@AuthenticationPrincipal String email,
@PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> commentSave(@User LoginUser loginUser,
@RequestBody CommentSaveReqDto commentSaveReqDto) {

commentService.commentSave(email, commentSaveReqDto);
commentService.commentSave(loginUser.getMemberId(), commentSaveReqDto);

String responseMessage = String.format("%d 게시글에 댓글이 등록되었습니다.", commentSaveReqDto.boardId());
return ResponseEntity.status(HttpStatus.CREATED).body(responseMessage);
Expand All @@ -36,12 +38,12 @@ public ResponseEntity<String> commentSave(@AuthenticationPrincipal String email,
@Operation(summary = "댓글 수정", description = "댓글을 수정합니다.")
@ApiResponse(responseCode = "200", description = "댓글 수정 성공")
@ApiResponse(responseCode = "401", description = "인증 실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN")))
@PutMapping("/{commentId}")
public ResponseEntity<String> commentUpdate(@AuthenticationPrincipal String email,
@PutMapping(value = "/{commentId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> commentUpdate(@User LoginUser loginUser,
@PathVariable("commentId") Long commentId,
@RequestBody CommentUpdateReqDto commentUpdateReqDto) {

commentService.commentUpdate(email, commentId, commentUpdateReqDto);
commentService.commentUpdate(loginUser.getMemberId(), commentId, commentUpdateReqDto);

String responseMessage = String.format("%d 게시글의 댓글이 수정돠었습니다.", commentId);
return ResponseEntity.status(HttpStatus.OK).body(responseMessage);
Expand All @@ -50,10 +52,10 @@ public ResponseEntity<String> commentUpdate(@AuthenticationPrincipal String emai
@Operation(summary = "댓글 삭제", description = "댓글을 삭제합니다.")
@ApiResponse(responseCode = "200", description = "댓글 삭제 성공")
@ApiResponse(responseCode = "401", description = "인증 실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN")))
@DeleteMapping("/{commentId}")
public ResponseEntity<String> commentDelete(@AuthenticationPrincipal String email,
@DeleteMapping(value = "/{commentId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> commentDelete(@User LoginUser loginUser,
@PathVariable("commentId") Long commentId) {
commentService.commentDelete(email, commentId);
commentService.commentDelete(loginUser.getMemberId(), commentId);

String responseMessage = String.format("%d 게시글의 댓글이 삭제되었습니다.", commentId);
return ResponseEntity.status(HttpStatus.OK).body(responseMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class CommentService {

// 댓글 저장
@Transactional
public void commentSave(String email, CommentSaveReqDto commentSaveReqDto) {
Member member = memberRepository.findByEmail(email)
public void commentSave(Long id, CommentSaveReqDto commentSaveReqDto) {
Member member = memberRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다."));

Board board = boardRepository.findById(commentSaveReqDto.boardId())
Expand All @@ -36,8 +36,8 @@ public void commentSave(String email, CommentSaveReqDto commentSaveReqDto) {

// 댓글 수정
@Transactional
public CommentInfoResDto commentUpdate(String email, Long commentId, CommentUpdateReqDto commentUpdateReqDto){
Member member = memberRepository.findByEmail(email)
public CommentInfoResDto commentUpdate(Long id, Long commentId, CommentUpdateReqDto commentUpdateReqDto){
Member member = memberRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다."));

Comment comment = commentRepository.findById(commentId)
Expand All @@ -52,8 +52,8 @@ public CommentInfoResDto commentUpdate(String email, Long commentId, CommentUpda

// 댓글 삭제
@Transactional
public void commentDelete(String email, Long commentId) {
Member member = memberRepository.findByEmail(email)
public void commentDelete(Long id, Long commentId) {
Member member = memberRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다."));

Comment comment = commentRepository.findById(commentId)
Expand Down

0 comments on commit 0c0f02a

Please sign in to comment.