Conversation
Martinel2
left a comment
There was a problem hiding this comment.
2주차 수고하셨습니다!! 테스트 코드까지 아주 잘 구성하셨습니다!
도전해볼 점이 있다면, 테스트를 더욱 작게 쪼개어 하나의 기능마다 하나의 테스트를 할 수 있게 만들면 추후에 코드 리팩토링을 하더라도 바로바로 테스트할 수 있어서 좋습니다.
또한, 이러한 작은 테스트들을 먼저 작성해두고, 이 테스트를 통과하도록 코드를 짜는 TDD 방식을 사용해보시는 것도 추천드려요
| @PatchMapping("/{commentId}") | ||
| public ResponseEntity<Comment> updateComment( | ||
| @PathVariable Long commentId, | ||
| @RequestBody Map<String, String> request |
There was a problem hiding this comment.
Map을 DTO로 만들어서 사용하면 코드를 더욱 유연하게 작성할 수 있을 것 같아요!
| } | ||
|
|
||
| @PostMapping | ||
| public Comment createComment(@PathVariable Long postId, @RequestBody Comment comment) { |
There was a problem hiding this comment.
RequestBody로 Comment를 바로 받게 되면, 필요없는 정보까지 받아내야해서 낭비가 될 수 있어요 (ex createAt은 사용자가 주어야하는 정보가 아니라, db가 자동으로 저장할 수 있는 정보) DTO를 만들어서 필요한 정보만을 받을 수 있도록 하면 좋을 것 같아요
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/post/{postId}/comment") |
There was a problem hiding this comment.
postId를 해당 컨트롤러 내의 모든 코드에서 사용할까요? 예를 들어, 댓글을 삭제할 때도 postId가 필요할까요?
| @JoinColumn( | ||
| name="post_id", | ||
| nullable = false, | ||
| foreignKey = @ForeignKey(name = "fk_diary_user_id_ref_user_id") | ||
| ) |
| nullable = false, | ||
| foreignKey = @ForeignKey(name = "fk_diary_user_id_ref_user_id") | ||
| ) | ||
| @JsonBackReference |
There was a problem hiding this comment.
위에서 Entity를 직접 사용하는 것에서 Dto를 사용하는 것으로 변경하면 해당 직렬화 어노테이션은 필요가 없어질 것입니당!!
| import java.util.Optional; | ||
|
|
||
| @Service | ||
| public class PostService { |
There was a problem hiding this comment.
아래 코드들은 모두 DB쪽을 건드리는 코드이기 때문에 @transactional 어노테이션을 사용하여 트랜잭션을 설정하는 것이 좋아요! 트랜잭션을 설정해야하는 이유는 무엇일까요?
변경점 👍
setter, optional 등 보안 및 캡슐화 취약점 보완
테스트 코드 작성
테스트 💻
SpringBootTest, MockMvc을 활용하여 테스트 진행하였음.
스크린샷 🖼
테스트 결과
