-
Notifications
You must be signed in to change notification settings - Fork 134
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
[Spring Core] 남해윤 미션 제출합니다. #391
Open
haeyoon1
wants to merge
19
commits into
next-step:haeyoon1
Choose a base branch
from
haeyoon1:haeyoon-core
base: haeyoon1
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 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ad9fc14
feat: 홈 화면 생성, 예약 조회
haeyoon1 7dfd8c5
feat: 예약 추가/취소
haeyoon1 3452ba4
feat: 예외 처리
haeyoon1 7b379b3
feat: 5단계 - 데이터베이스 적용하기
haeyoon1 dec3a7a
feat: 6단계 - 데이터 조회하기
haeyoon1 bad771c
feat: 7단계 - 데이터 추가/삭제하기
haeyoon1 9f4ec62
fix: 구조 변경
haeyoon1 01807a8
test: 테스트 코드 추가
haeyoon1 a474d12
feat: 시간 관리 기능 추가
haeyoon1 24dbc31
refactor: time 문자형 LocalTime으로 변경
haeyoon1 2e97e74
refactor: 예약 클래스 수정
haeyoon1 d097ab9
refactor: 기존 코드 수정
haeyoon1 b68ea08
test: 10단계 test추가
haeyoon1 cd7122b
refactor: 계층화 리팩터링
haeyoon1 88f9813
error: 예약 추가 기능 오류 수정
haeyoon1 80500cb
error: 예약 추가 기능 오류 수정
haeyoon1 fb70989
Merge remote-tracking branch 'origin/haeyoon-core' into haeyoon-core
haeyoon1 6d9acfd
refactor: new-reservation.js 코드 수정
haeyoon1 672d581
refactor: reservation time 객체 수정
haeyoon1 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 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 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,14 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Controller | ||
public class HomeController { | ||
|
||
@RequestMapping("/") | ||
public String home() { | ||
return "home"; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/roomescape/controller/ReservationController.java
This file contains 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,58 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
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.ResponseBody; | ||
import roomescape.dto.ReservationRequestDto; | ||
import roomescape.dto.ReservationResponseDto; | ||
import roomescape.service.ReservationService; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@Controller | ||
public class ReservationController { | ||
|
||
private final ReservationService reservationService; | ||
|
||
public ReservationController(ReservationService reservationService) { | ||
this.reservationService = reservationService; | ||
} | ||
|
||
// 홈화면 | ||
@GetMapping("/reservation") | ||
public String reservationPage() { | ||
return "new-reservation"; | ||
} | ||
|
||
//예약 조회 | ||
@ResponseBody | ||
@GetMapping("/reservations") | ||
public ResponseEntity<List<ReservationResponseDto>> list() { | ||
List<ReservationResponseDto> reservations = reservationService.getAllReservations(); | ||
return ResponseEntity.ok(reservations); | ||
} | ||
|
||
//예약 추가 | ||
@ResponseBody | ||
@PostMapping("/reservations") | ||
public ResponseEntity<ReservationResponseDto> create(@RequestBody ReservationRequestDto newReservationDto) { | ||
|
||
ReservationResponseDto reservation = reservationService.createReservation(newReservationDto); | ||
|
||
return ResponseEntity.created(URI.create("/reservations/" + reservation.getId())) | ||
.body(reservation); | ||
} | ||
|
||
//예약 삭제 | ||
@DeleteMapping("/reservations/{id}") | ||
public ResponseEntity<Void> delete(@PathVariable Long id) { | ||
reservationService.deleteReservation(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
This file contains 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,52 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
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 roomescape.dto.TimeRequestDto; | ||
import roomescape.dto.TimeResponseDto; | ||
import roomescape.service.TimeService; | ||
|
||
import java.net.URI; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Controller | ||
public class TimeController { | ||
|
||
private final TimeService timeService; | ||
|
||
public TimeController(TimeService timeService) { | ||
this.timeService = timeService; | ||
} | ||
|
||
// 홈화면 | ||
@GetMapping("/time") | ||
public String reservationPage() { | ||
return "time"; | ||
} | ||
|
||
@PostMapping("/times") //시간 추가 | ||
public ResponseEntity<TimeResponseDto> createTime(@RequestBody TimeRequestDto requestDto){ | ||
TimeResponseDto newTime = timeService.createTime(requestDto); | ||
URI location = URI.create("/times/" + newTime.getId()); | ||
|
||
return ResponseEntity.created(location) | ||
.body(newTime); | ||
} | ||
@GetMapping("/times") //시간 조회 | ||
public ResponseEntity<List<TimeResponseDto>> findTimes(){ | ||
List<TimeResponseDto> timeList = timeService.findAllTimes(); | ||
return ResponseEntity.ok(timeList); | ||
} | ||
|
||
@DeleteMapping("/times/{id}") // 시간 삭제 | ||
public ResponseEntity<TimeResponseDto> deleteTime(@PathVariable Long id){ | ||
timeService.deleteTime(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
This file contains 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,53 @@ | ||
package roomescape.dao; | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
import roomescape.entity.Reservation; | ||
import roomescape.entity.Time; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class ReservationDao { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public ReservationDao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Reservation> findAll() { | ||
String sql = """ | ||
SELECT r.id AS reservation_id, r.name, r.date, t.id AS time_id, t.time AS time_value | ||
FROM reservation r | ||
INNER JOIN time t ON r.time_id = t.id | ||
"""; | ||
|
||
return jdbcTemplate.query(sql, (rs, rowNum) -> new Reservation( | ||
rs.getLong("reservation_id"), | ||
rs.getString("name"), | ||
rs.getString("date"), | ||
new Time( | ||
rs.getLong("time_id"), | ||
rs.getString("time_value") | ||
) | ||
)); | ||
} | ||
|
||
|
||
public Reservation insert(Reservation reservation) { | ||
String sql = "INSERT INTO reservation(name, date, time) VALUES (?, ?, ?)"; | ||
jdbcTemplate.update(sql, reservation.getName(), reservation.getDate(), reservation.getTime().getId()); | ||
|
||
String query = "SELECT id FROM reservation ORDER BY id DESC LIMIT 1"; | ||
Long id = jdbcTemplate.queryForObject(query, Long.class); | ||
|
||
return new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime()); | ||
} | ||
|
||
public void delete(Long id) { | ||
String sql = "DELETE FROM reservation WHERE id = ?"; | ||
jdbcTemplate.update(sql, id); | ||
} | ||
|
||
} |
This file contains 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,39 @@ | ||
package roomescape.dao; | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
import roomescape.entity.Time; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class TimeDao { | ||
public final JdbcTemplate jdbcTemplate; | ||
|
||
public TimeDao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Time> findAll() { | ||
String sql = "SELECT id, time FROM time"; | ||
return jdbcTemplate.query(sql, (rs, rowNum) -> new Time( | ||
rs.getLong("id"), | ||
rs.getString("time") | ||
)); | ||
} | ||
|
||
public Time insert(Time time) { | ||
String sql = "INSERT INTO time(time) VALUES (?)"; | ||
jdbcTemplate.update(sql, time.getTime()); | ||
|
||
String query = "SELECT id FROM time ORDER BY id DESC LIMIT 1"; | ||
Long id = jdbcTemplate.queryForObject(query, Long.class); | ||
|
||
return new Time(id, time.getTime()); | ||
} | ||
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. 방금 저장한 시간의 id를 조회하는 것 같아요.
저장과 동시에 DB에서 생성된 id를 반환하는 방식을 활용해보세요!
|
||
|
||
public void delete(Long id) { | ||
String sql = "DELETE FROM time WHERE id = ?"; | ||
jdbcTemplate.update(sql, id); | ||
} | ||
} |
This file contains 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.dto; | ||
|
||
import roomescape.entity.Time; | ||
|
||
public class ReservationRequestDto { | ||
private String name; | ||
private String date; | ||
private String time; | ||
|
||
public ReservationRequestDto(String name, String date, String time) { | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public String getTime() { | ||
return time; | ||
} | ||
|
||
public Time getStringTimeAsTime(){ //string -> time | ||
return new Time(time); | ||
} | ||
} |
This file contains 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,35 @@ | ||
package roomescape.dto; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
|
||
public class ReservationResponseDto { | ||
private Long id; | ||
private String name; | ||
private String date; | ||
private LocalTime time; | ||
|
||
public ReservationResponseDto(Long id, String name, String date, LocalTime 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 LocalTime getTime() { | ||
return time; | ||
} | ||
} |
This file contains 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,23 @@ | ||
package roomescape.dto; | ||
|
||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class TimeRequestDto { | ||
private String time; | ||
|
||
public TimeRequestDto() {} | ||
|
||
public TimeRequestDto(String time) { | ||
this.time = time; | ||
} | ||
|
||
public String getTime() { | ||
return time; | ||
} | ||
|
||
public LocalTime toLocalTime() { //string -> localTime | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); | ||
return LocalTime.parse(time, formatter); | ||
} | ||
} |
This file contains 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,22 @@ | ||
package roomescape.dto; | ||
|
||
import java.time.LocalTime; | ||
|
||
public class TimeResponseDto { | ||
|
||
public Long id; | ||
public String time; | ||
|
||
public TimeResponseDto(Long id, LocalTime time) { | ||
this.id = id; | ||
this.time = time.toString(); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTime() { | ||
return time; | ||
} | ||
} |
This file contains 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,41 @@ | ||
package roomescape.entity; | ||
|
||
import java.time.LocalTime; | ||
|
||
public class Reservation { | ||
private Long id; | ||
private String name; | ||
private String date; | ||
private Time time; | ||
|
||
public Reservation(Long id, String name, String date, Time time) { | ||
this.id = id; | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public Reservation(String name, String date, Time time) { | ||
this(null, name, date, time); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public Time getTime() { | ||
return time; | ||
} | ||
|
||
public LocalTime getTimeAsLocalTime() { | ||
return time.getTimeASALocalTime(); | ||
} | ||
} |
Oops, something went wrong.
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.
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.
Controller는 전체적으로 깔끔하네요!👍👍