Skip to content

Commit 24fb51c

Browse files
committed
refactor(ReservationRepository): [9단계] 예약 쿼리 수정
1 parent 7e4316d commit 24fb51c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/main/java/roomescape/model/reservation/ReservationRepository.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.jdbc.core.JdbcTemplate;
44
import org.springframework.jdbc.core.RowMapper;
55
import org.springframework.stereotype.Repository;
6+
import roomescape.model.time.Time;
67

78
import java.sql.ResultSet;
89
import java.sql.SQLException;
@@ -17,15 +18,35 @@ public ReservationRepository(JdbcTemplate jdbcTemplate) {
1718
}
1819

1920
public List<Reservation> findAll() {
20-
String sql = "SELECT * FROM reservation";
21+
String sql = """
22+
SELECT
23+
r.id as reservation_id,
24+
r.name,
25+
r.date,
26+
t.id as time_id,
27+
t.time as time_value
28+
FROM reservation as r
29+
INNER JOIN time as t ON r.time_id = t.id
30+
""";
31+
2132
return jdbcTemplate.query(sql, new ReservationRowMapper());
2233
}
2334

2435
private static class ReservationRowMapper implements RowMapper<Reservation> {
2536
@Override
2637
public Reservation mapRow(ResultSet rs, int rowNum) throws SQLException {
27-
Reservation reservation = new Reservation(rs.getString("name"), rs.getString("date"), rs.getString("time"));
28-
reservation.setId(rs.getLong("id"));
38+
39+
Long timeId = rs.getLong("time_id");
40+
String timeValue = rs.getString("time_value");
41+
42+
Time time = new Time(timeValue);
43+
44+
Reservation reservation = new Reservation(
45+
rs.getString("name"),
46+
rs.getString("date"),
47+
time
48+
);
49+
reservation.setId(rs.getLong("reservation_id"));
2950
return reservation;
3051
}
3152
}

0 commit comments

Comments
 (0)