From cd321f3c44d238580cb0c9833760befa98067bbe Mon Sep 17 00:00:00 2001 From: juanxiu <2316007@ewhain.net> Date: Sat, 15 Jun 2024 00:53:05 +0900 Subject: [PATCH 1/2] wip --- build.gradle | 6 + .../Repository/ReservationRepository.java | 66 ++++++ .../roomescape/Repository/TimeRepository.java | 11 + .../controller/ReservationController.java | 63 +++++ .../controller/RoomEscapeController.java | 23 ++ .../roomescape/controller/TimeController.java | 49 ++++ .../java/roomescape/domain/Reservation.java | 39 ++++ src/main/java/roomescape/domain/Time.java | 34 +++ .../service/ReservationService.java | 41 ++++ src/main/resources/application.properties | 3 + src/main/resources/schema.sql | 17 ++ src/test/java/roomescape/MissionStepTest.java | 218 ++++++++++++++++++ 12 files changed, 570 insertions(+) create mode 100644 src/main/java/roomescape/Repository/ReservationRepository.java create mode 100644 src/main/java/roomescape/Repository/TimeRepository.java create mode 100644 src/main/java/roomescape/controller/ReservationController.java create mode 100644 src/main/java/roomescape/controller/RoomEscapeController.java create mode 100644 src/main/java/roomescape/controller/TimeController.java create mode 100644 src/main/java/roomescape/domain/Reservation.java create mode 100644 src/main/java/roomescape/domain/Time.java create mode 100644 src/main/java/roomescape/service/ReservationService.java create mode 100644 src/main/resources/schema.sql diff --git a/build.gradle b/build.gradle index 57267157c..8540414d2 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-jdbc' + runtimeOnly 'com.h2database:h2' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + + } test { diff --git a/src/main/java/roomescape/Repository/ReservationRepository.java b/src/main/java/roomescape/Repository/ReservationRepository.java new file mode 100644 index 000000000..deeca1391 --- /dev/null +++ b/src/main/java/roomescape/Repository/ReservationRepository.java @@ -0,0 +1,66 @@ +package roomescape.Repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; +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; +import roomescape.domain.Reservation; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Time; +import java.util.List; + +@Repository +public class ReservationRepository { + + @Autowired + private JdbcTemplate jdbcTemplate; + + private final RowMapper reservationMapper = (ResultSet rs, int rowNum) -> { + Long id = rs.getLong("reservation_id"); + String name = rs.getString("name"); + String date = rs.getString("date"); + Time time = rs.getTime("time_value"); + return new Reservation(id, name, date, time); + }; + + public List findAll() { + String query = "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"; + return jdbcTemplate.query(query, reservationMapper); + } + + public Long save(Reservation reservation) { + String query = "INSERT INTO reservations (name, date, time_id) VALUES (?, ?, ?)"; + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcTemplate.update(connection -> { + PreparedStatement ps = connection.prepareStatement(query, new String[]{"id"}); + ps.setString(1, reservation.getName()); + ps.setString(2, reservation.getDate()); + ps.setLong(3, reservation.getTime().getTime()); + return ps; + }, keyHolder); + + return keyHolder.getKey().longValue(); + } + + public void delete(Long reservationId) { + String query = "DELETE FROM reservations WHERE id = ?"; + jdbcTemplate.update(query, reservationId); + } + + public Reservation findById(Long id) { + String query = "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 " + + "WHERE r.id = ?"; + try { + return jdbcTemplate.queryForObject(query, new Object[]{id}, reservationMapper); + } catch (EmptyResultDataAccessException e) { + return null; + } + } +} diff --git a/src/main/java/roomescape/Repository/TimeRepository.java b/src/main/java/roomescape/Repository/TimeRepository.java new file mode 100644 index 000000000..a04e48205 --- /dev/null +++ b/src/main/java/roomescape/Repository/TimeRepository.java @@ -0,0 +1,11 @@ +package roomescape.Repository; +import org.springframework.stereotype.Repository; +import roomescape.domain.Time; +import org.springframework.data.jpa.repository.JpaRepository; + +@Repository +public interface TimeRepository extends JpaRepository { +// JPA?? + +} + diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java new file mode 100644 index 000000000..3b971f73b --- /dev/null +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -0,0 +1,63 @@ +package roomescape.controller; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.*; +import roomescape.Repository.ReservationRepository; +import roomescape.domain.Reservation; +import roomescape.service.ReservationService; + +import java.net.URI; +import java.util.List; + +@RestController +@RequestMapping("/reservations") +public class ReservationController { + + private final ReservationService reservationService; + + public ReservationController(final ReservationService reservationService) { + this.reservationService = reservationService; + } + + // 예약 조회 + @GetMapping + @ResponseBody + public ResponseEntity>getReservations() { + List reservations = reservationService.getAllReservation(); + return ResponseEntity.ok(reservations); + } + + @GetMapping("/{id}") + @ResponseBody + public ResponseEntity getReservation(@PathVariable Long id) { + return ResponseEntity.ok(reservationService.getReservation(id)); + } + + // 예약 추가 + @PostMapping + @ResponseBody + public ResponseEntity addReservation(@RequestBody Reservation reservation){ + + Long generatedID = reservationService.createReservation(reservation); + URI uri = URI.create("/reservations/" + generatedID); + reservation.setId(generatedID); + return ResponseEntity.created(uri).body(reservation); + } + + + // 예약 삭제 + @DeleteMapping("/{id}") + public ResponseEntity deleteReservation(@PathVariable Long id) { + boolean isDeleted = reservationService.deleteReservation(id); + + if (isDeleted) { + return ResponseEntity.status(HttpStatus.OK).body("Reservation deleted successfully."); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Reservation not found."); + } + } +} \ No newline at end of file diff --git a/src/main/java/roomescape/controller/RoomEscapeController.java b/src/main/java/roomescape/controller/RoomEscapeController.java new file mode 100644 index 000000000..5c038c10e --- /dev/null +++ b/src/main/java/roomescape/controller/RoomEscapeController.java @@ -0,0 +1,23 @@ +package roomescape.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class RoomEscapeController { + + @GetMapping("/reservation") + public String reservation() { + return "new-reservation"; + } + + @GetMapping + public String index() { + return "home"; + } + + @GetMapping("/time") + public String time() { + return "time"; + } +} diff --git a/src/main/java/roomescape/controller/TimeController.java b/src/main/java/roomescape/controller/TimeController.java new file mode 100644 index 000000000..7abe088df --- /dev/null +++ b/src/main/java/roomescape/controller/TimeController.java @@ -0,0 +1,49 @@ +package roomescape.controller; + +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import roomescape.Repository.ReservationRepository; +import roomescape.Repository.TimeRepository; +import roomescape.domain.Reservation; +import roomescape.domain.Time; + +import java.net.URI; +import java.util.List; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/times") +public class TimeController { + + private final TimeRepository timeRepository; + + @Autowired + public TimeController(TimeRepository timeRepository) { + this.timeRepository = timeRepository; + } + + // 시간데이터 조회 + @GetMapping + public ResponseEntity>getTime(){ + List