diff --git a/src/main/java/roomescape/controller/PageController.java b/src/main/java/roomescape/controller/PageController.java new file mode 100644 index 00000000..0aa3145a --- /dev/null +++ b/src/main/java/roomescape/controller/PageController.java @@ -0,0 +1,17 @@ +package roomescape.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class PageController { + @GetMapping + public String home() { + return "admin/index"; + } + + @GetMapping("/admin/reservation") + public String reservation() { + return "admin/reservation-legacy"; + } +} diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java new file mode 100644 index 00000000..e4d3fb88 --- /dev/null +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -0,0 +1,35 @@ +package roomescape.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import roomescape.model.Reservation; +import roomescape.service.ReservationService; + +import java.sql.PreparedStatement; +import java.util.List; + +@RestController +@RequestMapping("/reservations") +public class ReservationController { + private final ReservationService reservationService; + + @Autowired + public ReservationController(ReservationService reservationService) { + this.reservationService = reservationService; + } + + @PostMapping + public Long insert(@RequestBody Reservation reservation) { + return reservationService.addReservation(reservation); + } + + @GetMapping + public List read() { + return reservationService.lookUpReservation(); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) { + reservationService.deleteReservation(id); + } +} diff --git a/src/main/java/roomescape/controller/ReservationTimeController.java b/src/main/java/roomescape/controller/ReservationTimeController.java new file mode 100644 index 00000000..81245efc --- /dev/null +++ b/src/main/java/roomescape/controller/ReservationTimeController.java @@ -0,0 +1,35 @@ +package roomescape.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import roomescape.model.ReservationTime; +import roomescape.service.ReservationTimeService; + +import java.util.List; + +@RestController +@RequestMapping("/times") +public class ReservationTimeController { + + private ReservationTimeService reservationTimeService; + + @Autowired + public ReservationTimeController(ReservationTimeService reservationTimeService) { + this.reservationTimeService = reservationTimeService; + } + + @PostMapping + public ReservationTime insert(@RequestBody String startAt) { + return reservationTimeService.addReservation(startAt); + } + + @GetMapping + public List read() { + return reservationTimeService.lookUpReservationTime(); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) { + reservationTimeService.deleteReservation(id); + } +} diff --git a/src/main/java/roomescape/model/Reservation.java b/src/main/java/roomescape/model/Reservation.java new file mode 100644 index 00000000..b583cdae --- /dev/null +++ b/src/main/java/roomescape/model/Reservation.java @@ -0,0 +1,33 @@ +package roomescape.model; + +import java.util.concurrent.atomic.AtomicLong; + +public class Reservation { + private Long id; + private String name; + private String date; + private Long timeId; + + public Reservation(Long id, String name, String date, Long timeId) { + this.id = id; + this.name = name; + this.date = date; + this.timeId = timeId; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDate() { + return date; + } + + public Long getTimeId() { + return timeId; + } +} diff --git a/src/main/java/roomescape/model/ReservationTime.java b/src/main/java/roomescape/model/ReservationTime.java new file mode 100644 index 00000000..dbf4077f --- /dev/null +++ b/src/main/java/roomescape/model/ReservationTime.java @@ -0,0 +1,24 @@ +package roomescape.model; + +public class ReservationTime { + private Long id; + private String startAt; + + public ReservationTime(String startAt) { + this(0L, startAt); + } + + public ReservationTime(Long id, String startAt) { + this.id = id; + this.startAt = startAt; + } + + public Long getId() { + return id; + } + + public String getStartAt() { + return startAt; + } + +} diff --git a/src/main/java/roomescape/repository/ReservationRepository.java b/src/main/java/roomescape/repository/ReservationRepository.java new file mode 100644 index 00000000..f6684cc2 --- /dev/null +++ b/src/main/java/roomescape/repository/ReservationRepository.java @@ -0,0 +1,62 @@ +package roomescape.repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; +import roomescape.model.Reservation; + +import javax.sql.DataSource; +import java.sql.PreparedStatement; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Repository +public class ReservationRepository { + private final JdbcTemplate jdbcTemplate; + private final SimpleJdbcInsert jdbcInsert; + private final RowMapper rowMapper = (resultSet, rowNum) -> new Reservation( + resultSet.getLong("id"), + resultSet.getString("name"), + resultSet.getString("date"), + resultSet.getLong("time_id") + ); + + @Autowired + public ReservationRepository(SimpleJdbcInsert jdbcInsert, JdbcTemplate jdbcTemplate, DataSource dataSource) { + this.jdbcTemplate = jdbcTemplate; + this.jdbcInsert = new SimpleJdbcInsert(dataSource) + .withTableName("reservation") + .usingGeneratedKeyColumns("id"); + } + + public Long save(Reservation reservation) { + Map params = new HashMap<>(); + params.put("id", reservation.getId()); + params.put("name", reservation.getName()); + params.put("date", reservation.getDate()); + params.put("time_id", reservation.getTimeId()); + + return jdbcInsert.executeAndReturnKey(params).longValue(); + } + + public List readAll() { + String sql = "SELECT \n" + + "r.id as reservation_id, \n" + + "r.name as reservation_name, \n" + + "r.date as reservation_date, \n" + + "t.id as time_id, \n" + + "t.start_at as time_start_at \n" + + "FROM reservation as r inner join reservation_time as t on r.time_id = t.id"; + return jdbcTemplate.query(sql, rowMapper); + } + + public void deleteById(Long id) { + String sql = "DELETE FROM reservation WHERE id = ?"; + jdbcTemplate.update(sql, id); + } +} diff --git a/src/main/java/roomescape/repository/ReservationTimeRepository.java b/src/main/java/roomescape/repository/ReservationTimeRepository.java new file mode 100644 index 00000000..91d6072b --- /dev/null +++ b/src/main/java/roomescape/repository/ReservationTimeRepository.java @@ -0,0 +1,50 @@ +package roomescape.repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import roomescape.model.ReservationTime; + +import javax.sql.DataSource; +import java.sql.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReservationTimeRepository { + + private final JdbcTemplate jdbcTemplate; + private final SimpleJdbcInsert jdbcInsert; + + private final RowMapper rowMapper = (resultSet, rowNum) -> new ReservationTime( + resultSet.getLong("id"), + resultSet.getString("start_at") + ); + + @Autowired + public ReservationTimeRepository(JdbcTemplate jdbcTemplate, SimpleJdbcInsert jdbcInsert, DataSource dataSource) { + this.jdbcTemplate = jdbcTemplate; + this.jdbcInsert = new SimpleJdbcInsert(dataSource) + .withTableName("reservation_time") + .usingGeneratedKeyColumns("id"); + } + + public ReservationTime save(String startAt) { + Map params = new HashMap<>(); + params.put("startAt", startAt); + return new ReservationTime(jdbcInsert.executeAndReturnKey(params).longValue(), startAt); + } + + public List readAll() { + String sql = "SELECT * FROM reservation_time"; + return jdbcTemplate.query(sql, rowMapper); + } + + public void deleteById(Long id) { + String sql = "DELETE FROM reservation_time WHERE id = ?"; + jdbcTemplate.update(sql, id); + } +} diff --git a/src/main/java/roomescape/service/ReservationService.java b/src/main/java/roomescape/service/ReservationService.java new file mode 100644 index 00000000..d326a4b7 --- /dev/null +++ b/src/main/java/roomescape/service/ReservationService.java @@ -0,0 +1,30 @@ +package roomescape.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import roomescape.model.Reservation; +import roomescape.repository.ReservationRepository; + +import java.util.List; + +@Service +public class ReservationService { + private final ReservationRepository reservationRepository; + + @Autowired + public ReservationService(ReservationRepository reservationRepository) { + this.reservationRepository = reservationRepository; + } + + public Long addReservation(Reservation reservation) { + return reservationRepository.save(reservation); + } + + public List lookUpReservation() { + return reservationRepository.readAll(); + } + + public void deleteReservation(Long id) { + reservationRepository.deleteById(id); + } +} diff --git a/src/main/java/roomescape/service/ReservationTimeService.java b/src/main/java/roomescape/service/ReservationTimeService.java new file mode 100644 index 00000000..8db0aeca --- /dev/null +++ b/src/main/java/roomescape/service/ReservationTimeService.java @@ -0,0 +1,32 @@ +package roomescape.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import roomescape.model.ReservationTime; +import roomescape.repository.ReservationRepository; +import roomescape.repository.ReservationTimeRepository; + +import java.util.List; + +@Service +public class ReservationTimeService { + + private final ReservationTimeRepository reservationTimeRepository; + + @Autowired + public ReservationTimeService(ReservationTimeRepository reservationTimeRepository) { + this.reservationTimeRepository = reservationTimeRepository; + } + + public ReservationTime addReservation(String startAt) { + return reservationTimeRepository.save(startAt); + } + + public List lookUpReservationTime() { + return reservationTimeRepository.readAll(); + } + + public void deleteReservation(Long id) { + reservationTimeRepository.deleteById(id); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29b..476363a8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.h2.console.enabled=true +spring.h2.console.path=/h2-console +spring.datasource.url=jdbc:h2:mem:database +spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 00000000..5571e7d1 --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,16 @@ +CREATE TABLE reservation_time +( + id BIGINT NOT NULL AUTO_INCREMENT, + start_at VARCHAR(255) NOT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE reservation +( + id BIGINT NOT NULL AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + date VARCHAR(255) NOT NULL, + time_id BIGINT, + PRIMARY KEY (id), + FOREIGN KEY (time_id) REFERENCES reservation_time (id) +); \ No newline at end of file