diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java index 58ed6e559..e481b9d8d 100644 --- a/src/main/java/roomescape/controller/ReservationController.java +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -1,85 +1,50 @@ 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.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("id"), - rs.getString("name"), - rs.getString("date"), - rs.getString("time") - ); - // 예약 관리 페이지 @GetMapping("/reservation") public String reservationPage() { - return "reservation"; + return "new-reservation"; } // 예약 조회 @GetMapping("/reservations") @ResponseBody - public List getReservations() { - String sql = "SELECT id, name, date, time FROM reservation"; - return jdbcTemplate.query(sql, reservationRowMapper); + public ResponseEntity> getReservations() { + List reservations = reservationService.getReservations(); + return ResponseEntity.ok(reservations); } // 예약 추가 @PostMapping("/reservations") + @ResponseBody public ResponseEntity addReservation(@RequestBody ReservationRequest request) { - request.validate(); - - Reservation newReservation = new Reservation(null, request.getName(), request.getDate(), request.getTime()); - - String sql = "INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)"; - KeyHolder keyHolder = new GeneratedKeyHolder(); - - jdbcTemplate.update(connection -> { - PreparedStatement ps = connection.prepareStatement(sql, new String[] {"id"}); - ps.setString(1, newReservation.getName()); - ps.setString(2, newReservation.getDate()); - ps.setString(3, newReservation.getTime()); - return ps; - }, keyHolder); - - Long id = keyHolder.getKey().longValue(); - newReservation.setId(id); - - 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 new file mode 100644 index 000000000..c9c9d1f14 --- /dev/null +++ b/src/main/java/roomescape/controller/TimeController.java @@ -0,0 +1,50 @@ +package roomescape.controller; + +import org.springframework.http.ResponseEntity; +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.util.List; + +@Controller +public class TimeController { + + private final TimeService timeService; + + public TimeController(TimeService timeService) { + this.timeService = timeService; + } + + // 시간 관리 페이지 + @GetMapping("/time") + public String reservationPage() { + return "time"; + } + + // 시간 조회 + @GetMapping("/times") + @ResponseBody + public ResponseEntity> getTimes() { + List