Skip to content

Commit 156607a

Browse files
committed
feat(TimeController): [8단계] 시간 조회 API 생성
1 parent 2d600ab commit 156607a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main/java/roomescape/controller/TimeController.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
import org.springframework.http.HttpStatus;
44
import org.springframework.http.ResponseEntity;
55
import org.springframework.jdbc.core.JdbcTemplate;
6+
import org.springframework.jdbc.core.RowMapper;
67
import org.springframework.jdbc.support.GeneratedKeyHolder;
78
import org.springframework.jdbc.support.KeyHolder;
89
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.GetMapping;
911
import org.springframework.web.bind.annotation.PostMapping;
1012
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.ResponseBody;
1114
import roomescape.model.time.Time;
1215
import roomescape.model.time.TimeRequest;
1316

1417
import java.sql.PreparedStatement;
18+
import java.sql.ResultSet;
19+
import java.sql.SQLException;
20+
import java.util.List;
1521

1622
@Controller
1723
public class TimeController {
@@ -20,6 +26,21 @@ public class TimeController {
2026
public TimeController(JdbcTemplate jdbcTemplate) {
2127
this.jdbcTemplate = jdbcTemplate;
2228
}
29+
@GetMapping("/times")
30+
@ResponseBody
31+
public List<Time> getTimes() {
32+
String sql = "SELECT * FROM time";
33+
return jdbcTemplate.query(sql, new TimeRowMapper());
34+
}
35+
36+
private static class TimeRowMapper implements RowMapper<Time> {
37+
@Override
38+
public Time mapRow(ResultSet rs, int rowNum) throws SQLException {
39+
Time time = new Time(rs.getString("time"));
40+
time.setId(rs.getLong("id"));
41+
return time;
42+
}
43+
}
2344

2445
@PostMapping("/times")
2546
public ResponseEntity<Time> createTimes(@RequestBody TimeRequest timeRequest) {

0 commit comments

Comments
 (0)