[임현희_BackEnd] 11주차 과제 제출합니다.#19
Conversation
/introduce?name=이름 으로 요청했을때 안녕하세요 제 이름은 {이름}입니다! 텍스트가 반환되도록 하기
|
|
||
| public String getContent() { return content; } | ||
| public void setContent(String content) { this.content = content; } | ||
| } No newline at end of file |
There was a problem hiding this comment.
빨간색 경고 같은 표시가 왜 뜬걸까요?
다음 자료 확인하시고 적용해주세요!
| this.content = content; | ||
| } | ||
|
|
||
| public Long getPostId() { return postId; } |
There was a problem hiding this comment.
setter는 보통 사용을 지양한답니다! 그 이유를 한번 찾아보시면 좋을 것 같아요
| public Long getPostId() { return postId; } | ||
| public void setPostId(Long postId) { this.postId = postId; } | ||
|
|
||
| public String getTitle() { return title; } | ||
| public void setTitle(String title) { this.title = title; } | ||
|
|
||
| public String getContent() { return content; } | ||
| public void setContent(String content) { this.content = content; } | ||
| } No newline at end of file |
| private String title; | ||
| private String content; | ||
|
|
||
| public Article() {} |
There was a problem hiding this comment.
지금과 같은 경우에서는 아니지만 자바는 생성자를 정의하지 않았을 때 기본 생성자를 자동으로 추가해준답니다!
There was a problem hiding this comment.
이런 entity들은 반드시 기본 생성자가 있어야 합니다! 그 이유도 나중에 찾아보시면 좋을 것 같아요
| public Article() {} | ||
|
|
||
| public Article(Long postId, String title, String content) { | ||
| this.postId = postId; | ||
| this.title = title; | ||
| this.content = content; | ||
| } |
There was a problem hiding this comment.
역시 lombok을 사용하면
각각 @All/NoArgsConstructor로 할 수 있겠죠?
|
|
||
| @GetMapping("/json") | ||
| public Person getJson() { | ||
| return new Person("임현희", 22); | ||
| } | ||
|
|
||
| static class Person { |
There was a problem hiding this comment.
이렇게 내부 클래스로 정의 하면 재사용성, 테스트성 매우 낮을 것 같은데 이렇게 하신 이유가 있을까요?
There was a problem hiding this comment.
구글링해서 찾아본 예시에서 이렇게 나와있어서 그대로 해봤습니닷..ㅠ 다음부터 고려해서 해보겠습니다
| @PutMapping("/{id}") | ||
| public ResponseEntity<Article> updateArticle(@PathVariable Long id, @RequestBody Article updatedData) { | ||
| Article article = articleStore.get(id); | ||
| if (article == null) { | ||
| return ResponseEntity.notFound().build(); | ||
| } | ||
| article.setTitle(updatedData.getTitle()); | ||
| article.setContent(updatedData.getContent()); | ||
| return ResponseEntity.ok(article); | ||
| } |
There was a problem hiding this comment.
컨트롤러에 이렇게 모든 코드가 다 있으면 보기가 힘들겠죠? 그래서 MVC라는 패턴을 사용하는 거랍니다!
| public class ArticleController { | ||
|
|
||
| private final Map<Long, Article> articleStore = new HashMap<>(); | ||
| private long nextId = 1; |
There was a problem hiding this comment.
여기서는 AtomicLong이라는 것을 쓰는게 좋을 것 같아요 AtomicLong과 Long의 차이는 뭐고 AtomicLong을 쓰면 장점이 뭘까요? 한번 알아보시면 좋을 것 같아요
| public ResponseEntity<Article> createArticle(@RequestBody Article article) { | ||
| article.setPostId(nextId++); | ||
| articleStore.put(article.getPostId(), article); | ||
| return ResponseEntity.ok(article); |
There was a problem hiding this comment.
여기서 200을 반환하는게 적절할까요? 다른 어떤 상태를 반환 할 수 있을까요?
| @PutMapping("/{id}") | ||
| public ResponseEntity<Article> updateArticle(@PathVariable Long id, @RequestBody Article updatedData) { | ||
| Article article = articleStore.get(id); | ||
| if (article == null) { | ||
| return ResponseEntity.notFound().build(); | ||
| } | ||
| article.setTitle(updatedData.getTitle()); | ||
| article.setContent(updatedData.getContent()); | ||
| return ResponseEntity.ok(article); | ||
| } |
dradnats1012
left a comment
There was a problem hiding this comment.
누락된 기능들이 있는것 같아서 해당 기능들 추가해주세요!
그리고 패키지 구조를 분리해보면 좋을 것 같아요!
| compileOnly 'org.projectlombok:lombok:1.18.28' | ||
| annotationProcessor 'org.projectlombok:lombok:1.18.28' |
| @Getter | ||
| @Setter |
There was a problem hiding this comment.
사용하는건 좋지만 이렇게 되면 사용하지 않는 getter나 setter 메서드가 생길수도 있습니다!
부작용이 있다는걸 인지하면 좋을 것 같아요
| public ArticleController(ArticleService articleService) { | ||
| this.articleService = articleService; | ||
| } |
There was a problem hiding this comment.
생성자 주입 방법을 사용하셨는데 해당 방법의 장점은 무엇일까요?
또 다른 주입 방법은 뭐가 있는지 알아보면 좋을 것 같아요~!
There was a problem hiding this comment.
생성자에서만 값을 설정해서 안정성이 높다고 알고 있습니다.
| } | ||
|
|
||
| @PostMapping | ||
| public void create(@RequestBody Article article) { |
There was a problem hiding this comment.
RequestDTO 에 대해 알아보면 좋을 것 같아요!
적용도 한번 해보세용
| public void delete(@PathVariable Long id) { | ||
| articleService.deleteArticle(id); | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
EOF에 대해 알아보면 좋을 것 같아요!
참고 : https://velog.io/@junho5336/No-newline-at-end-of-file
| this.jdbc = jdbc; | ||
| } | ||
|
|
||
| public List<ArticleDto> findAllByBoardId(Long boardId) { |
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor |
There was a problem hiding this comment.
모두 필요한 어노테이션일까요?
각각의 역할에 대해 알아보고 꼭 필요한것만 적용하면 좋을 것 같아요!
| public List<ArticleDto> getAllByBoardId(Long boardId) { | ||
| return articleDao.findAllByBoardId(boardId); | ||
| } |
There was a problem hiding this comment.
boardId 기준으로 가져오는 기능 말고도 다른 CRUD 메서드들도 구현해주세요!
No description provided.