Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
48 changes: 48 additions & 0 deletions src/main/java/com/example/demo/AOPexception/ExceptHandler.java
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);
}
Comment on lines +22 to +25

Choose a reason for hiding this comment

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

얘는 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

Choose a reason for hiding this comment

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

이 친구들은 bad request라고 생각한 이유가 있나요?
왜 get에서 나오는 not found는 status가 not found이고 put이나 post할 때 나오는 not found는 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
Expand Up @@ -18,7 +18,7 @@

@RestController
public class BoardController {

// 과제 수행함
private final BoardService boardService;

public BoardController(BoardService boardService) {

Choose a reason for hiding this comment

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

단일 정보를 가져오는 api들이 boards라는 이름을 갖고 있는 것이 적절할까요?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface ArticleRepository {

List<Article> findAllByBoardId(Long boardId);

List<Article> findAllByMemberId(Long memberId);

Article findById(Long id);

Article insert(Article article);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Choose a reason for hiding this comment

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

모든 익셉션이 반드시 "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());
Expand All @@ -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.

Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ public List<Board> findAll() {

@Override
public Board findById(Long id) {
return jdbcTemplate.queryForObject("""
SELECT id, name
try {
return jdbcTemplate.queryForObject("""

SELECT id, name
FROM board
WHERE id = ?
""", boardRowMapper, id);
} catch (Exception e) {
throw new RuntimeException("Board not found");
}
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface MemberRepository {

Member findById(Long id);

Member findByEmail(String email);

Member insert(Member member);

Member update(Member member);
Expand Down
Loading