-
Notifications
You must be signed in to change notification settings - Fork 132
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
base: haeyoon1
Are you sure you want to change the base?
Changes from 5 commits
ad9fc14
7dfd8c5
3452ba4
7b379b3
dec3a7a
bad771c
9f4ec62
01807a8
a474d12
24dbc31
2e97e74
d097ab9
b68ea08
cd7122b
88f9813
80500cb
fb70989
6d9acfd
672d581
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 |
---|---|---|
@@ -1,5 +1,52 @@ | ||
package roomescape.controller; | ||
|
||
public class ReservationTimeController { | ||
|
||
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
import roomescape.entity.Reservation; | ||
import roomescape.entity.Time; | ||
|
||
import java.util.List; | ||
|
||
|
@@ -15,21 +16,28 @@ public ReservationDao(JdbcTemplate jdbcTemplate) { | |
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Reservation> findAll(){ | ||
String sql = "SELECT id, name, date, time FROM reservation"; | ||
|
||
return jdbcTemplate.query( | ||
sql, (resultSet, rowNum) -> new Reservation( | ||
resultSet.getLong("id"), | ||
resultSet.getString("name"), | ||
resultSet.getString("date"), | ||
resultSet.getTimestamp("time").toLocalDateTime() | ||
)); | ||
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()); | ||
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); | ||
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를 반환하는 방식을 활용해보세요!
|
||
|
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
package roomescape.dto; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class TimeRequestDto { | ||
public LocalDateTime time; | ||
private String time; | ||
|
||
public TimeRequestDto(LocalDateTime time) { | ||
public TimeRequestDto() {} | ||
|
||
public TimeRequestDto(String time) { | ||
this.time = time; | ||
} | ||
|
||
public LocalDateTime getTime() { | ||
public String getTime() { | ||
return time; | ||
} | ||
|
||
public LocalTime toLocalTime() { //string -> localTime | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); | ||
return LocalTime.parse(time, formatter); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
package roomescape.controller;public class TimeResponseDto { | ||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||
package roomescape.entity; | ||||||
|
||||||
import java.time.LocalTime; | ||||||
|
||||||
public class Time { | ||||||
public Long id; | ||||||
public String time; | ||||||
|
||||||
public Time(Long id, String time) { | ||||||
this.id = id; | ||||||
this.time = time; | ||||||
} | ||||||
|
||||||
public Time(String time) { | ||||||
this(null, time); | ||||||
} | ||||||
|
||||||
public Long getId() { | ||||||
return id; | ||||||
} | ||||||
|
||||||
public String getTime() { | ||||||
return time; | ||||||
} | ||||||
|
||||||
public LocalTime getTimeASALocalTime(){ //string->LocalTime | ||||||
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. 메서드 네이밍 컨벤션에 맞춰주세요!
Suggested change
|
||||||
return LocalTime.parse(time); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package roomescape.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import roomescape.dao.ReservationDao; | ||
import roomescape.dto.ReservationRequestDto; | ||
import roomescape.dto.ReservationResponseDto; | ||
import roomescape.entity.Reservation; | ||
import roomescape.entity.Time; | ||
import roomescape.repository.ReservationRepository; | ||
import roomescape.repository.TimeRepository; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
@@ -30,7 +33,7 @@ public ReservationResponseDto createReservation(ReservationRequestDto requestDto | |
Reservation reservation = new Reservation( | ||
requestDto.getName(), | ||
requestDto.getDate(), | ||
requestDto.getTime() | ||
requestDto.getStringTimeAsTime() | ||
); | ||
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. Dto -> Entity로의 변환 작업을 dto에 편의 메서드를 추가해볼 수도 있을 것 같아요. class ReservationRequestDto {
// ...
public Reservation toReservation() {
// 변환 로직
}
} Dto 객체에 변환 로직을 추가하여 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. ReservationService 클래스의 createReservation 메서드에
이렇게 수정했는데 처음에 코멘트 남겨주신대로 dto에 변환 로직을 만들어서 작성하는 것이 나을까요? |
||
Reservation savedReservation = reservationRepository.insert(reservation); | ||
return toResponseDto(savedReservation); | ||
|
@@ -45,7 +48,7 @@ private ReservationResponseDto toResponseDto(Reservation reservation) { | |
reservation.getId(), | ||
reservation.getName(), | ||
reservation.getDate(), | ||
reservation.getTime() | ||
reservation.getTime().getTimeASALocalTime() | ||
); | ||
} | ||
} |
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는 전체적으로 깔끔하네요!👍👍