Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Expand Down
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.context.annotation.EnableAspectJAutoProxy;
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);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/demo/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy
@SpringBootApplication
public class DemoApplication {

Expand Down
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
17 changes: 15 additions & 2 deletions src/main/java/com/example/demo/domain/Article.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.example.demo.domain;

import java.time.LocalDateTime;
import jakarta.persistence.*;

import java.time.LocalDateTime;
@Entity
@Table(name = "article")
public class Article {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "author_id")
private Long authorId;
@Column(name = "board_id")
private Long boardId;
private String title;
private String content;
@Column(name = "created_date")
private LocalDateTime createdAt;
@Column(name = "modified_date")
private LocalDateTime modifiedAt;

Choose a reason for hiding this comment

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

현재 수준에서는 트집 잡기 일 수 있는데 보통은 가독성을 위해서 한칸씩 띄워서 써요~


public Article(
Expand Down Expand Up @@ -40,6 +48,11 @@ public Article(Long authorId, Long boardId, String title, String content) {
this.modifiedAt = LocalDateTime.now();
}

public Article() {

}


Comment on lines +57 to +61

Choose a reason for hiding this comment

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

이 친구는 엔티티마다 추가한 이유가 있을까요?
과연 여기서 필요한게 맞을까요?

public void update(Long boardId, String title, String description) {
this.boardId = boardId;
this.title = title;
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/example/demo/domain/Board.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.example.demo.domain;

public class Board {
import jakarta.persistence.*;

@Entity
@Table(name = "board")
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

Expand All @@ -14,6 +19,10 @@ public Board(String name) {
this.name = name;
}

public Board() {

}

public Long getId() {
return id;
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/example/demo/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.example.demo.domain;

public class Member {
import jakarta.persistence.*;

@Entity
@Table(name = "member")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
Expand All @@ -20,6 +25,10 @@ public Member(String name, String email, String password) {
this.password = password;
}

public Member() {

}

public void update(String name, String email) {
this.name = name;
this.email = email;
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 @@ -11,7 +11,6 @@

import com.example.demo.domain.Article;

@Repository
public class ArticleRepositoryJdbc implements ArticleRepository {

private final JdbcTemplate jdbcTemplate;
Expand All @@ -21,50 +20,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 +97,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);
}
}
Loading