-
Notifications
You must be signed in to change notification settings - Fork 169
[Spring MVC] 김남우 미션제출합니다 #522
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
Open
corjqnrl
wants to merge
9
commits into
next-step:corjqnrl
Choose a base branch
from
corjqnrl:step5,6,7
base: corjqnrl
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5beef06
1단계 - 홈 화면
corjqnrl bb1e64c
2단계 - 예약 조회
corjqnrl 4bd639c
type 변경
corjqnrl 713b296
model에 리스트 담아 전달
corjqnrl 245e5b8
3단계 예약 추가 / 예약 취소
corjqnrl 4a40f49
4단계 예외 처리
corjqnrl eeb20f6
feat: 5단계 데이터베이스 적용하기
corjqnrl b83ca13
feat: 6단계 데이터 조회하기
corjqnrl a6a1cd8
feat: 7단계 데이터 추가/삭제 하기
corjqnrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package roomescape; | ||
|
|
||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
|
|
||
| @Controller | ||
| public class RoomescapeController { | ||
| @GetMapping("/") | ||
| public String home() { | ||
| return "home"; | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/main/java/roomescape/controller/ReservationController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // ReservationController.java | ||
| package roomescape.controller; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
|
|
||
| import roomescape.model.Reservation; | ||
| import roomescape.exception.BadRequestReservationException; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.util.*; | ||
|
|
||
| @RestController // RestController로 변경 | ||
| @RequestMapping("/reservations") | ||
| public class ReservationController { | ||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| public ReservationController(DataSource dataSource, JdbcTemplate jdbcTemplate) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<Reservation> getReservation() { | ||
| String sql = "select id, name, date, time from reservation"; | ||
| List<Reservation> reservations = jdbcTemplate.query( | ||
| sql, (resultSet, rowNum) -> { | ||
| Reservation reservation = new Reservation( | ||
| resultSet.getLong("id"), | ||
| resultSet.getString("name"), | ||
| resultSet.getString("date"), | ||
| resultSet.getString("time") | ||
| ); | ||
| return reservation; | ||
| } | ||
| ); | ||
| return reservations; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<Reservation> addReservation(@RequestBody Reservation newReservation) { | ||
| // 예외 조건 추가 | ||
| if (newReservation.getName() == null || newReservation.getName().isBlank() || | ||
| newReservation.getDate() == null || newReservation.getDate().isBlank() || | ||
| newReservation.getTime() == null || newReservation.getTime().isBlank()) { | ||
| throw new BadRequestReservationException("Required fields are missing."); | ||
| } | ||
|
|
||
| String sql = "INSERT INTO reservation(name, date, time) VALUES (?, ?, ?)"; | ||
|
|
||
| int insertCount = jdbcTemplate.update( | ||
| sql, | ||
| newReservation.getName(), | ||
| newReservation.getDate(), | ||
| newReservation.getTime() | ||
| ); | ||
|
|
||
| if (insertCount <= 0) { | ||
| throw new RuntimeException("Failed to insert reservation"); | ||
| } | ||
|
|
||
| Long generatedId = jdbcTemplate.queryForObject("SELECT MAX(id) FROM reservation", Long.class); | ||
|
|
||
| Reservation reservation = new Reservation( | ||
| generatedId, | ||
| newReservation.getName(), | ||
| newReservation.getDate(), | ||
| newReservation.getTime() | ||
| ); | ||
|
|
||
| return ResponseEntity.status(HttpStatus.CREATED) | ||
| .header("Location", "/reservations/" + reservation.getId()) | ||
| .body(reservation); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Void> deleteReservation(@PathVariable Long id) { | ||
| int deletedCount = jdbcTemplate.update("DELETE FROM reservation WHERE id = ?", id); if (deletedCount > 0) { | ||
| return ResponseEntity.noContent().build(); | ||
| } else { | ||
| throw new BadRequestReservationException("Reservation not found."); | ||
| } | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
src/main/java/roomescape/exception/BadRequestReservationException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package roomescape.exception; | ||
|
|
||
| public class BadRequestReservationException extends RuntimeException { | ||
| public BadRequestReservationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/roomescape/exception/ReservationExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package roomescape.exception; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
|
||
| @RestControllerAdvice | ||
| public class ReservationExceptionHandler { | ||
| @ExceptionHandler(BadRequestReservationException.class) | ||
| public ResponseEntity<Void> handleBadRequest(BadRequestReservationException e) { | ||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package roomescape.model; | ||
|
|
||
| public class Reservation { | ||
| private Long id; | ||
| private String name; | ||
| private String date; | ||
| private String time; | ||
|
|
||
| public Reservation(Long id, String name, String date, String time) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.date = date; | ||
| this.time = time; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getDate() { | ||
| return date; | ||
| } | ||
|
|
||
| public String getTime() { | ||
| return time; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # h2-console 활성화 여부 | ||
| spring.h2.console.enabled=true | ||
| #db url | ||
| spring.datasource.url=jdbc:h2:mem:database |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| CREATE TABLE reservation | ||
| ( | ||
| id BIGINT NOT NULL AUTO_INCREMENT, | ||
| name VARCHAR(255) NOT NULL, | ||
| date VARCHAR(255) NOT NULL, | ||
| time VARCHAR(255) NOT NULL, | ||
| PRIMARY KEY (id) | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
현재 SELECT 구문이 원자성을 보장하는지 고민해봐야할 것 같습니다. 만약 두 개의 INSERT 문이 연속적으로 실행되고, 그 다음에 SELECT 로 조회한다면 먼저 들어간 레코드의 id가 하나 증가한 채로 조회될 것 같아요. 또는 삭제된 레코드 때문에 MAX가 실제 생성된 ID와 다를 수 있겠네요. 트랜잭션의 특징을 더 알고 싶다면 이 글을 보는 것도 좋을 것 같아요!
그리고 JDBC 의 keyholder 를 사용해서 리팩터링하는 게 좋을 것 같습니다. 이 글을 참고해보실래요?