diff --git a/build.gradle b/build.gradle index 57267157c..e4a27b7a9 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,12 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.rest-assured:rest-assured:5.3.1' + + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + + implementation 'org.springframework.boot:spring-boot-starter-jdbc' + runtimeOnly 'com.h2database:h2' } test { diff --git a/src/main/java/roomescape/controller/HomeController.java b/src/main/java/roomescape/controller/HomeController.java new file mode 100644 index 000000000..6efe2ab58 --- /dev/null +++ b/src/main/java/roomescape/controller/HomeController.java @@ -0,0 +1,14 @@ +package roomescape.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class HomeController { + + @RequestMapping("/") + public String home() { + return "home"; + } + +} diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java new file mode 100644 index 000000000..a690a2c26 --- /dev/null +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -0,0 +1,58 @@ +package roomescape.controller; + +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +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.ResponseBody; +import roomescape.dto.ReservationRequestDto; +import roomescape.dto.ReservationResponseDto; +import roomescape.service.ReservationService; + +import java.net.URI; +import java.util.List; + +@Controller +public class ReservationController { + + private final ReservationService reservationService; + + public ReservationController(ReservationService reservationService) { + this.reservationService = reservationService; + } + + // 홈화면 + @GetMapping("/reservation") + public String reservationPage() { + return "new-reservation"; + } + + //예약 조회 + @ResponseBody + @GetMapping("/reservations") + public ResponseEntity> list() { + List reservations = reservationService.getAllReservations(); + return ResponseEntity.ok(reservations); + } + + //예약 추가 + @ResponseBody + @PostMapping("/reservations") + public ResponseEntity create(@RequestBody ReservationRequestDto newReservationDto) { + + ReservationResponseDto reservation = reservationService.createReservation(newReservationDto); + + return ResponseEntity.created(URI.create("/reservations/" + reservation.getId())) + .body(reservation); + } + + //예약 삭제 + @DeleteMapping("/reservations/{id}") + public ResponseEntity delete(@PathVariable Long id) { + 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..6f8dfbcc4 --- /dev/null +++ b/src/main/java/roomescape/controller/TimeController.java @@ -0,0 +1,52 @@ +package roomescape.controller; + +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +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 roomescape.dto.TimeRequestDto; +import roomescape.dto.TimeResponseDto; +import roomescape.service.TimeService; + +import java.net.URI; +import java.time.LocalDateTime; +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"; + } + + @PostMapping("/times") //시간 추가 + public ResponseEntity createTime(@RequestBody TimeRequestDto requestDto){ + TimeResponseDto newTime = timeService.createTime(requestDto); + URI location = URI.create("/times/" + newTime.getId()); + + return ResponseEntity.created(location) + .body(newTime); + } + @GetMapping("/times") //시간 조회 + public ResponseEntity> findTimes(){ + List timeList = timeService.findAllTimes(); + return ResponseEntity.ok(timeList); + } + + @DeleteMapping("/times/{id}") // 시간 삭제 + public ResponseEntity deleteTime(@PathVariable Long id){ + timeService.deleteTime(id); + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/roomescape/dao/ReservationDao.java b/src/main/java/roomescape/dao/ReservationDao.java new file mode 100644 index 000000000..6db61bf73 --- /dev/null +++ b/src/main/java/roomescape/dao/ReservationDao.java @@ -0,0 +1,54 @@ +package roomescape.dao; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import roomescape.entity.Reservation; +import roomescape.entity.Time; + +import java.util.List; + +@Repository +public class ReservationDao { + + private final JdbcTemplate jdbcTemplate; + + public ReservationDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List findAll() { + String sql = """ + SELECT r.id AS reservation_id, r.name, r.date, t.id AS time_id, t.time AS time_value + FROM reservation r + INNER JOIN time t ON r.time_id = t.id + """; + + return jdbcTemplate.query(sql, (rs, rowNum) -> new Reservation( + rs.getLong("reservation_id"), + rs.getString("name"), + rs.getString("date"), + new Time( + rs.getLong("time_id"), + rs.getString("time_value") + ) + )); + } + + + public Reservation insert(Reservation reservation) { + + String sql = "INSERT INTO reservation(name, date, time_id) VALUES (?, ?, ?)"; + jdbcTemplate.update(sql, reservation.getName(), reservation.getDate(), reservation.getTime().getId()); + + String query = "SELECT id FROM reservation ORDER BY id DESC LIMIT 1"; + Long id = jdbcTemplate.queryForObject(query, Long.class); + + return new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime()); + } + + public void delete(Long id) { + String sql = "DELETE FROM reservation WHERE id = ?"; + jdbcTemplate.update(sql, id); + } + +} diff --git a/src/main/java/roomescape/dao/TimeDao.java b/src/main/java/roomescape/dao/TimeDao.java new file mode 100644 index 000000000..a02cfaaa0 --- /dev/null +++ b/src/main/java/roomescape/dao/TimeDao.java @@ -0,0 +1,48 @@ +package roomescape.dao; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; +import roomescape.entity.Time; + +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.util.List; + +@Repository +public class TimeDao { + public final JdbcTemplate jdbcTemplate; + + public TimeDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public List