diff --git a/src/main/java/roomescape/AdminController.java b/src/main/java/roomescape/AdminController.java new file mode 100644 index 0000000000..8792519f1f --- /dev/null +++ b/src/main/java/roomescape/AdminController.java @@ -0,0 +1,21 @@ +package roomescape; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/admin") +public class AdminController { + + @GetMapping + public String showMainPage() { + return "index"; + } + + @GetMapping("/reservation") + public String showReservationPage() { + return "admin/reservation-legacy"; + } + +} diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/Reservation.java new file mode 100644 index 0000000000..d217b4bc4e --- /dev/null +++ b/src/main/java/roomescape/Reservation.java @@ -0,0 +1,13 @@ +package roomescape; + +public record Reservation( + Long id, + String name, + String date, + String time) { + + public Reservation toEntity(Long id) { + return new Reservation(id, this.name, this.date, this.time); + } + +} diff --git a/src/main/java/roomescape/ReservationController.java b/src/main/java/roomescape/ReservationController.java new file mode 100644 index 0000000000..eb1a454385 --- /dev/null +++ b/src/main/java/roomescape/ReservationController.java @@ -0,0 +1,43 @@ +package roomescape; + +import java.net.URI; +import java.util.List; +import org.springframework.http.ResponseEntity; +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 org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/reservations") +public class ReservationController { + + private final ReservationRepository reservationRepository; + + public ReservationController(ReservationRepository reservationRepository) { + this.reservationRepository = reservationRepository; + } + + @GetMapping + public ResponseEntity> listReservations() { + return ResponseEntity.ok(reservationRepository.findAll()); + } + + @PostMapping + public ResponseEntity createReservation(@RequestBody Reservation reservation) { + Reservation newReservation = reservationRepository.create(reservation); + + URI location = URI.create("/reservations/" + newReservation.id()); + return ResponseEntity.created(location).body(newReservation); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteReservation(@PathVariable Long id) { + reservationRepository.deleteById(id); + return ResponseEntity.noContent().build(); + } + +} diff --git a/src/main/java/roomescape/ReservationRepository.java b/src/main/java/roomescape/ReservationRepository.java new file mode 100644 index 0000000000..ace3da1ad2 --- /dev/null +++ b/src/main/java/roomescape/ReservationRepository.java @@ -0,0 +1,65 @@ +package roomescape; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; +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.Repository; + +@Repository +public class ReservationRepository { + + private static final String TABLE_NAME = "reservation"; + private static final RowMapper ROW_MAPPER = (resultSet, rowNum) -> new Reservation( + resultSet.getLong("id"), + resultSet.getString("name"), + resultSet.getString("date"), + resultSet.getString("time")); + + private final JdbcTemplate jdbcTemplate; + + public ReservationRepository(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List findAll() { + return jdbcTemplate.query("SELECT id, name, date, time FROM %s".formatted(TABLE_NAME), ROW_MAPPER); + } + + public Reservation create(Reservation reservation) { + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcTemplate.update(connection -> insertQuery(connection, reservation), keyHolder); + + Long id = keyHolder.getKey().longValue(); + return reservation.toEntity(id); + } + + private PreparedStatement insertQuery(Connection connection, Reservation reservation) throws SQLException { + PreparedStatement preparedStatement = connection.prepareStatement( + "INSERT INTO %s (name, date, time) VALUES (?, ?, ?)".formatted(TABLE_NAME), new String[]{"id"}); + preparedStatement.setString(1, reservation.name()); + preparedStatement.setString(2, reservation.date()); + preparedStatement.setString(3, reservation.time()); + return preparedStatement; + } + + public void deleteById(Long id) { + Reservation foundReservation = findById(id); + jdbcTemplate.update("DELETE FROM %s WHERE id = ?".formatted(TABLE_NAME), foundReservation.id()); + } + + private Reservation findById(Long id) { + Reservation reservation = jdbcTemplate.queryForObject( + "SELECT id, name, date, time FROM %s WHERE id = ?".formatted(TABLE_NAME), ROW_MAPPER, id); + + if (reservation == null) { + throw new IllegalStateException("해당 예약이 없습니다."); + } + return reservation; + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..b63619d739 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.h2.console.enabled=true +spring.h2.console.path=/h2-console +spring.datasource.url=jdbc:h2:mem:database diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 0000000000..8d9ab2754f --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,8 @@ +CREATE TABLE reservation +( + id BIGINT NOT NULL AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + date VARCHAR(255) NOT NULL, + time VARCHAR(255) NOT NULL, + PRIMARY KEY (id) +); diff --git a/src/main/resources/templates/admin/reservation-legacy.html b/src/main/resources/templates/admin/reservation-legacy.html index 320ef3c702..e7d1a35e4d 100644 --- a/src/main/resources/templates/admin/reservation-legacy.html +++ b/src/main/resources/templates/admin/reservation-legacy.html @@ -22,10 +22,10 @@ diff --git a/src/main/resources/templates/admin/reservation.html b/src/main/resources/templates/admin/reservation.html index f31714eb74..9660ce2134 100644 --- a/src/main/resources/templates/admin/reservation.html +++ b/src/main/resources/templates/admin/reservation.html @@ -22,10 +22,10 @@ diff --git a/src/main/resources/templates/admin/index.html b/src/main/resources/templates/index.html similarity index 86% rename from src/main/resources/templates/admin/index.html rename to src/main/resources/templates/index.html index 417102cf1c..1bd929cfc4 100644 --- a/src/main/resources/templates/admin/index.html +++ b/src/main/resources/templates/index.html @@ -11,7 +11,7 @@