diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java index e87850d98..e481b9d8d 100644 --- a/src/main/java/roomescape/controller/ReservationController.java +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -1,38 +1,24 @@ package roomescape.controller; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import roomescape.exception.NotFoundReservationException; import roomescape.model.Reservation; -import roomescape.model.ReservationRequest; -import roomescape.model.Time; +import roomescape.model.ReservationRequest;; +import roomescape.service.ReservationService; import java.net.URI; -import java.sql.PreparedStatement; import java.util.List; @Controller public class ReservationController { - private final JdbcTemplate jdbcTemplate; + private final ReservationService reservationService; - public ReservationController(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; + public ReservationController(ReservationService reservationService) { + this.reservationService = reservationService; } - private final RowMapper reservationRowMapper = (rs, rowNum) -> new Reservation( - rs.getLong("reservation_id"), - rs.getString("name"), - rs.getString("date"), - new Time(rs.getLong("time_id"), rs.getString("time_value")) - ); - // 예약 관리 페이지 @GetMapping("/reservation") public String reservationPage() { @@ -43,18 +29,7 @@ public String reservationPage() { @GetMapping("/reservations") @ResponseBody public ResponseEntity> getReservations() { - String sql = """ - SELECT - r.id as reservation_id, - r.name, - r.date, - t.id as time_id, - t.time as time_value - FROM reservation as r - INNER JOIN time as t - ON r.time_id = t.id - """; - List reservations = jdbcTemplate.query(sql, reservationRowMapper); + List reservations = reservationService.getReservations(); return ResponseEntity.ok(reservations); } @@ -62,42 +37,14 @@ public ResponseEntity> getReservations() { @PostMapping("/reservations") @ResponseBody public ResponseEntity addReservation(@RequestBody ReservationRequest request) { - request.validateAndSetTimeId(jdbcTemplate); - - // 예약 추가 - String sql = "INSERT INTO reservation (name, date, time_id) VALUES (?, ?, ?)"; - KeyHolder keyHolder = new GeneratedKeyHolder(); - - jdbcTemplate.update(connection -> { - PreparedStatement ps = connection.prepareStatement(sql, new String[]{"id"}); - ps.setString(1, request.getName()); - ps.setString(2, request.getDate()); - ps.setLong(3, request.getTimeId()); - return ps; - }, keyHolder); - - Long id = keyHolder.getKey().longValue(); - - String timeQuery = "SELECT time FROM time WHERE id = ?"; - String timeValue = jdbcTemplate.queryForObject(timeQuery, String.class, request.getTime()); - - Reservation newReservation = new Reservation(id, request.getName(), request.getDate(), - new Time(request.getTimeId(), timeValue)); - - - return ResponseEntity.created(URI.create("/reservations/" + id)).body(newReservation); + Reservation newReservation = reservationService.addReservation(request); + return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())).body(newReservation); } // 예약 삭제 @DeleteMapping("/reservations/{id}") public ResponseEntity deleteReservation(@PathVariable Long id) { - String sql = "DELETE FROM reservation WHERE id = ?"; - int rowsAffected = jdbcTemplate.update(sql, id); - - if (rowsAffected > 0) { - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } - - throw new NotFoundReservationException("삭제할 예약이 없습니다."); + reservationService.deleteReservation(id); + return ResponseEntity.noContent().build(); } } diff --git a/src/main/java/roomescape/controller/TimeController.java b/src/main/java/roomescape/controller/TimeController.java index 82110d677..c9c9d1f14 100644 --- a/src/main/java/roomescape/controller/TimeController.java +++ b/src/main/java/roomescape/controller/TimeController.java @@ -1,34 +1,23 @@ package roomescape.controller; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import roomescape.model.Time; +import roomescape.service.TimeService; import java.net.URI; -import java.sql.PreparedStatement; import java.util.List; @Controller public class TimeController { - private final JdbcTemplate jdbcTemplate; + private final TimeService timeService; - public TimeController(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; + public TimeController(TimeService timeService) { + this.timeService = timeService; } - private final RowMapper