-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat/댓글 수정API 추가 #25
feat/댓글 수정API 추가 #25
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import com.gamzabat.algohub.feature.comment.dto.CreateCommentRequest; | ||
import com.gamzabat.algohub.feature.comment.dto.GetCommentResponse; | ||
import com.gamzabat.algohub.feature.comment.dto.ModifyCommentRequest; | ||
import com.gamzabat.algohub.feature.comment.service.CommentService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.Errors; | ||
|
@@ -54,4 +55,12 @@ public ResponseEntity<Object> deleteComment(@AuthedUser User user, @RequestParam | |
commentService.deleteComment(user,commentId); | ||
return ResponseEntity.ok().body("OK"); | ||
} | ||
@PostMapping("/modify") | ||
@Operation(summary = "댓글 수정 API") | ||
public ResponseEntity<Object> modifyComment(@Valid @RequestBody ModifyCommentRequest request, Errors errors){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [q] 이것 String으로 "OK' 리턴하게 되어있는데, Object 말고 String으로 쓰면 안돌아가나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해보겠읍니다..! 근데 전반적으로 이 컨트롤러의 동작 방식이 이해가 잘 안되는데요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
지식 +1 감사합니다 |
||
if(errors.hasErrors()) | ||
throw new RequestException("수정 요청이 올바르지 않습니다",errors); | ||
commentService.modifyComment(request); | ||
return ResponseEntity.ok().body("OK"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.gamzabat.algohub.feature.comment.dto; | ||
|
||
import com.gamzabat.algohub.feature.user.domain.User; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record ModifyCommentRequest(Long commentId, | ||
String content | ||
) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package com.gamzabat.algohub.feature.comment.service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import com.gamzabat.algohub.exception.UserValidationException; | ||
import com.gamzabat.algohub.feature.comment.domain.Comment; | ||
import com.gamzabat.algohub.feature.comment.dto.GetCommentResponse; | ||
import com.gamzabat.algohub.feature.comment.dto.ModifyCommentRequest; | ||
import com.gamzabat.algohub.feature.comment.exception.CommentValidationException; | ||
import com.gamzabat.algohub.feature.comment.exception.SolutionValidationException; | ||
import org.springframework.http.HttpStatus; | ||
|
@@ -105,4 +108,15 @@ private Solution checkSolutionValidation(User user, Long solutionId) { | |
|
||
return solution; | ||
} | ||
@Transactional | ||
public void modifyComment(ModifyCommentRequest request) { | ||
Comment comment = commentRepository.findById(request.commentId()) | ||
hwangjokim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.orElseThrow(() -> new CommentValidationException(HttpStatus.NOT_FOUND.value(), "존재하지 않는 댓글 입니다.")); | ||
|
||
hwangjokim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
comment.setContent(request.content()); | ||
comment.setCreatedAt(LocalDateTime.now()); | ||
commentRepository.save(comment); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [major] 그리고, 업데이트는 save가 아니라 JPA에 있는 Dirty Checking이라는 기능을 적극 이용하는 코드를 쓰면 좋을 것 같아요. 내부 용어가 좀 어려울 수 있는데, 이해 안되는 것 바로바로 질문해주세요 |
||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[blocker] RestFul API를 위해, "행위"를 URL에 묘사하지 말고, "자원"을 URL에 표기한뒤 HTTP METHOD를 통해 행위를 구분해주세요.
이 경우엔 기본 url이
/api/comment
니까, 그냥@PutMapping
만 달아주는게 예시입니다.다른 API도 보면, URL엔 자원을 표기하고 Post, Get, Delete, Put같은 Method를 통해 API의 행위를 구분하는것을 볼 수 있습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확인했습니다.. 근데 PostMapping PutMapping 이 여러개 있는경우에는 따로 URL에 써줘야 하는거 같은데 그 경우는 어떤 식으로 표기해줘야하나요?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여러개인 경우엔 URL로 구분해줘야 하는 것 맞고, api에 맞게 적절하게 네이밍 지어주면 됩니다!