Skip to content
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

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,4 +55,12 @@ public ResponseEntity<Object> deleteComment(@AuthedUser User user, @RequestParam
commentService.deleteComment(user,commentId);
return ResponseEntity.ok().body("OK");
}
@PostMapping("/modify")
Copy link
Contributor

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의 행위를 구분하는것을 볼 수 있습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다.. 근데 PostMapping PutMapping 이 여러개 있는경우에는 따로 URL에 써줘야 하는거 같은데 그 경우는 어떤 식으로 표기해줘야하나요?!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여러개인 경우엔 URL로 구분해줘야 하는 것 맞고, api에 맞게 적절하게 네이밍 지어주면 됩니다!

@Operation(summary = "댓글 수정 API")
public ResponseEntity<Object> modifyComment(@Valid @RequestBody ModifyCommentRequest request, Errors errors){
Copy link
Contributor

@hwangjokim hwangjokim Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] 이것 String으로 "OK' 리턴하게 되어있는데, Object 말고 String으로 쓰면 안돌아가나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해보겠읍니다..! 근데 전반적으로 이 컨트롤러의 동작 방식이 이해가 잘 안되는데요
이게 프론트에서 요청을 dto에 담아서 Controller 로 날리는건가요? 그런 후에 service에서 메소드 실행을 하고 프론트에 return을 ResponeEntitiy에 담아 보내는 과정이 맞는지 굼금합니다

Copy link
Contributor

@hwangjokim hwangjokim Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 컨트롤러는 DTO 필드 스키마에 맞게, JSON 형태로 API를 호출합니다. 이것을 컨트롤러가 받습니다
  2. JSON 형태로 날아온 것들, @RequestBody 가 붙은 DTO와 필드를 비교해 일치하면 객체로 만들어줍니다.
  3. 서비스 메서드를 통해 로직을 수행하고, Return값을 받습니다.
  4. 프론트에 "OK" 라는 문자열과 함께, HTTP STATUS CODE 200을 리턴합니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 컨트롤러는 DTO 필드 스키마에 맞게, JSON 형태로 API를 호출합니다. 이것을 컨트롤러가 받습니다
  2. JSON 형태로 날아온 것들, @RequestBody 가 붙은 DTO와 필드를 비교해 일치하면 객체로 만들어줍니다.
  3. 서비스 메서드를 통해 로직을 수행하고, Return값을 받습니다.
  4. 프론트에 "OK" 라는 문자열과 함께, HTTP STATUS CODE 200을 리턴합니다

지식 +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
Expand Up @@ -39,4 +39,11 @@ public Comment(Solution solution, User user, String content, LocalDateTime creat
this.content = content;
this.createdAt = createdAt;
}

public void setContent(String content) {
hwangjokim marked this conversation as resolved.
Show resolved Hide resolved
this.content = content;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
}
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;
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[major] 그리고, 업데이트는 save가 아니라 JPA에 있는 Dirty Checking이라는 기능을 적극 이용하는 코드를 쓰면 좋을 것 같아요.
Dirty Checking으로 Update하는것의 개념은..
https://jojoldu.tistory.com/415
https://everydayyy.tistory.com/157

내부 용어가 좀 어려울 수 있는데, 이해 안되는 것 바로바로 질문해주세요


}

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

public interface ProblemRepository extends JpaRepository<Problem, Long> {
Page<Problem> findAllByStudyGroup(StudyGroup studyGroup, Pageable pageable);
Problem getById(Long id);
List<Problem> findAllByNumber(Integer Number);
List<Problem> findAllByStudyGroupAndEndDate(StudyGroup studyGroup, LocalDate endDate);
@Query("SELECT COUNT(p) FROM Problem p WHERE p.studyGroup.id = :groupId")
Expand Down