-
Notifications
You must be signed in to change notification settings - Fork 13
[Spring Core] 김학인 과제 제출합니다. #10
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
75b1384
8bf4897
35b2d50
90d7114
064bf47
e96817e
3996d4c
fb4f666
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.example.demo.AOPexception; | ||
|
|
||
| import com.example.demo.AOPexception.Exception.*; | ||
| import org.apache.coyote.BadRequestException; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
|
||
| @RestControllerAdvice | ||
| public class ExceptHandler { | ||
| @ExceptionHandler | ||
| public ResponseEntity<ErrorResponse> handleException(GetNotFoundException e) { | ||
| return new ResponseEntity<>(new ErrorResponse(e.getMessage()),HttpStatus.NOT_FOUND); | ||
| } | ||
| @ExceptionHandler | ||
| public ResponseEntity<ErrorResponse> handleException(PutDuplicatedException e) { | ||
| return new ResponseEntity<>(new ErrorResponse(e.getMessage()),HttpStatus.CONFLICT); | ||
| } | ||
| @ExceptionHandler | ||
| public ResponseEntity<ErrorResponse> handleException(PutNotFoundException e) { | ||
| return new ResponseEntity<>(new ErrorResponse(e.getMessage()),HttpStatus.BAD_REQUEST); | ||
| } | ||
| @ExceptionHandler | ||
| public ResponseEntity<ErrorResponse> handleException(PostIllegalArgumemtException e) { | ||
| return new ResponseEntity<>(new ErrorResponse(e.getMessage()),HttpStatus.BAD_REQUEST); | ||
| } | ||
| @ExceptionHandler | ||
| public ResponseEntity<ErrorResponse> handleException(PostNotFoundException e) { | ||
| return new ResponseEntity<>(new ErrorResponse(e.getMessage()),HttpStatus.BAD_REQUEST); | ||
| } | ||
| @ExceptionHandler | ||
| public ResponseEntity<ErrorResponse> handleException(DeleteExistedExcepton e) { | ||
| return new ResponseEntity<>(new ErrorResponse(e.getMessage()),HttpStatus.BAD_REQUEST); | ||
| } | ||
|
|
||
|
Comment on lines
+26
to
+42
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. 이 친구들은 bad request라고 생각한 이유가 있나요? |
||
| static class ErrorResponse { | ||
| String message; | ||
|
|
||
| ErrorResponse(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.AOPexception.Exception; | ||
|
|
||
| public class DeleteExistedExcepton extends RuntimeException{ | ||
| public DeleteExistedExcepton(String msg) | ||
| { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.AOPexception.Exception; | ||
|
|
||
| public class GetNotFoundException extends RuntimeException{ | ||
| public GetNotFoundException(String msg) | ||
| { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.AOPexception.Exception; | ||
|
|
||
| public class PostIllegalArgumemtException extends RuntimeException{ | ||
| public PostIllegalArgumemtException(String msg) | ||
| { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.AOPexception.Exception; | ||
|
|
||
| public class PostNotFoundException extends RuntimeException{ | ||
| public PostNotFoundException(String msg) | ||
| { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.AOPexception.Exception; | ||
|
|
||
| public class PutDuplicatedException extends RuntimeException{ | ||
| public PutDuplicatedException(String msg) | ||
| { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.AOPexception.Exception; | ||
|
|
||
| public class PutNotFoundException extends RuntimeException{ | ||
| public PutNotFoundException(String msg) | ||
| { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
|
|
||
| @RestController | ||
| public class BoardController { | ||
|
|
||
| // 과제 수행함 | ||
| private final BoardService boardService; | ||
|
|
||
| public BoardController(BoardService boardService) { | ||
|
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. 단일 정보를 가져오는 api들이 boards라는 이름을 갖고 있는 것이 적절할까요? |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,50 +21,71 @@ public ArticleRepositoryJdbc(JdbcTemplate jdbcTemplate) { | |
| } | ||
|
|
||
| private static final RowMapper<Article> articleRowMapper = (rs, rowNum) -> new Article( | ||
| rs.getLong("id"), | ||
| rs.getLong("author_id"), | ||
| rs.getLong("board_id"), | ||
| rs.getString("title"), | ||
| rs.getString("content"), | ||
| rs.getTimestamp("created_date").toLocalDateTime(), | ||
| rs.getTimestamp("modified_date").toLocalDateTime() | ||
| rs.getLong("id"), | ||
| rs.getLong("author_id"), | ||
| rs.getLong("board_id"), | ||
| rs.getString("title"), | ||
| rs.getString("content"), | ||
| rs.getTimestamp("created_date").toLocalDateTime(), | ||
| rs.getTimestamp("modified_date").toLocalDateTime() | ||
| ); | ||
|
|
||
| @Override | ||
| public List<Article> findAll() { | ||
| return jdbcTemplate.query(""" | ||
| SELECT id, board_id, author_id, title, content, created_date, modified_date | ||
| FROM article | ||
| """, articleRowMapper); | ||
| SELECT id, board_id, author_id, title, content, created_date, modified_date | ||
| FROM article | ||
| """, articleRowMapper); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Article> findAllByBoardId(Long boardId) { | ||
| return jdbcTemplate.query(""" | ||
| SELECT id, board_id, author_id, title, content, created_date, modified_date | ||
| FROM article | ||
| WHERE board_id = ? | ||
| """, articleRowMapper, boardId); | ||
| try { | ||
| return jdbcTemplate.query(""" | ||
| SELECT id, board_id, author_id, title, content, created_date, modified_date | ||
| FROM article | ||
| WHERE board_id = ? | ||
| """, articleRowMapper, boardId); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Article not found"); | ||
| } | ||
|
||
| } | ||
| @Override | ||
| public List<Article> findAllByMemberId(Long memberId) { | ||
| try { | ||
| return jdbcTemplate.query(""" | ||
| SELECT id, board_id, author_id, title, content, created_date, modified_date | ||
| FROM article | ||
| WHERE author_id = ? | ||
| """, articleRowMapper, memberId); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Article not found"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Article findById(Long id) { | ||
| return jdbcTemplate.queryForObject(""" | ||
| SELECT id, board_id, author_id, title, content, created_date, modified_date | ||
| FROM article | ||
| WHERE id = ? | ||
| """, articleRowMapper, id); | ||
| try { | ||
| return jdbcTemplate.queryForObject(""" | ||
|
|
||
| SELECT id, board_id, author_id, title, content, created_date, modified_date | ||
| FROM article | ||
| WHERE id = ? | ||
| """, articleRowMapper, id); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Article not found"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Article insert(Article article) { | ||
| KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
| jdbcTemplate.update(con -> { | ||
| PreparedStatement ps = con.prepareStatement(""" | ||
| INSERT INTO article (board_id, author_id, title, content) | ||
| VALUES (?, ?, ?, ?) | ||
| """, | ||
| new String[]{"id"}); | ||
| INSERT INTO article (board_id, author_id, title, content) | ||
| VALUES (?, ?, ?, ?) | ||
| """, | ||
| new String[]{"id"}); | ||
| ps.setLong(1, article.getBoardId()); | ||
| ps.setLong(2, article.getAuthorId()); | ||
| ps.setString(3, article.getTitle()); | ||
|
|
@@ -77,23 +98,23 @@ INSERT INTO article (board_id, author_id, title, content) | |
| @Override | ||
| public Article update(Article article) { | ||
| jdbcTemplate.update(""" | ||
| UPDATE article | ||
| SET board_id = ?, title = ?, content = ? | ||
| WHERE id = ? | ||
| """, | ||
| article.getBoardId(), | ||
| article.getTitle(), | ||
| article.getContent(), | ||
| article.getId() | ||
| UPDATE article | ||
| SET board_id = ?, title = ?, content = ? | ||
| WHERE id = ? | ||
| """, | ||
| article.getBoardId(), | ||
| article.getTitle(), | ||
| article.getContent(), | ||
| article.getId() | ||
| ); | ||
| return findById(article.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteById(Long id) { | ||
| jdbcTemplate.update(""" | ||
| DELETE FROM article | ||
| WHERE id = ? | ||
| """, id); | ||
| DELETE FROM article | ||
| WHERE id = ? | ||
| """, id); | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
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.
얘는 httpstatus가 conflict가 적절한 것이 맞을까요?