From d5890c77e0fceb45d28068e32b6c9f903f794a35 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 26 Oct 2024 22:10:09 +0900 Subject: [PATCH 01/29] =?UTF-8?q?build:=20Spring=20web=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 57267157c..74d35aaaf 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter' + implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.rest-assured:rest-assured:5.3.1' } From 92c31db6cb4a2aa12bfa27c5fa7971ccddfc53a2 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 26 Oct 2024 22:10:46 +0900 Subject: [PATCH 02/29] =?UTF-8?q?feat:=20=EC=9D=BC=EB=8B=A8=EA=B3=84=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=ED=86=B5=EA=B3=BC=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/roomescape/HomeController.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/roomescape/HomeController.java diff --git a/src/main/java/roomescape/HomeController.java b/src/main/java/roomescape/HomeController.java new file mode 100644 index 000000000..dc1510a29 --- /dev/null +++ b/src/main/java/roomescape/HomeController.java @@ -0,0 +1,14 @@ +package roomescape; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HomeController { + + @GetMapping("/") + public String index() { + return "hi"; + } + +} From f60ca1f1f70525b2615b8c40cbb52b12f56d0514 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 26 Oct 2024 22:15:12 +0900 Subject: [PATCH 03/29] =?UTF-8?q?build:=20=ED=83=80=EC=9E=84=EB=A6=AC?= =?UTF-8?q?=ED=94=84=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 74d35aaaf..40a581b15 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.rest-assured:rest-assured:5.3.1' } From 89c9355dc2999b20ca87328c8bfc738c738d6cd6 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 26 Oct 2024 22:15:31 +0900 Subject: [PATCH 04/29] =?UTF-8?q?feat:=20=EC=96=B4=EB=93=9C=EB=AF=BC=20?= =?UTF-8?q?=ED=99=88=20=ED=8E=98=EC=9D=B4=EC=A7=80=20=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/roomescape/HomeController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/roomescape/HomeController.java b/src/main/java/roomescape/HomeController.java index dc1510a29..3c6eb06a1 100644 --- a/src/main/java/roomescape/HomeController.java +++ b/src/main/java/roomescape/HomeController.java @@ -1,14 +1,14 @@ package roomescape; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; -@RestController +@Controller public class HomeController { @GetMapping("/") public String index() { - return "hi"; + return "home"; } } From bc14e15f0cb97609d2e9e22d7769cc56df997fac Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 26 Oct 2024 22:20:08 +0900 Subject: [PATCH 05/29] =?UTF-8?q?feat:=20=EC=98=88=EC=95=BD=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=ED=8E=98=EC=9D=B4=EC=A7=80=20index?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/roomescape/ReservationController.java | 13 +++++++++++++ src/test/java/roomescape/MissionStepTest.java | 8 ++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/main/java/roomescape/ReservationController.java diff --git a/src/main/java/roomescape/ReservationController.java b/src/main/java/roomescape/ReservationController.java new file mode 100644 index 000000000..cda48bd6b --- /dev/null +++ b/src/main/java/roomescape/ReservationController.java @@ -0,0 +1,13 @@ +package roomescape; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class ReservationController { + + @GetMapping("/reservation") + public String index() { + return "reservation"; + } +} diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index cf4efbe91..671ddbc0f 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -16,4 +16,12 @@ public class MissionStepTest { .then().log().all() .statusCode(200); } + + @Test + void 이단계() { + RestAssured.given().log().all() + .when().get("/reservation") + .then().log().all() + .statusCode(200); + } } From aa534585c841f186ae8f68f61ab6b456eb89f98c Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 26 Oct 2024 23:44:09 +0900 Subject: [PATCH 06/29] =?UTF-8?q?feat:=20=EC=98=88=EC=95=BD=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/roomescape/Reservation.java | 32 +++++++++++++++++++ .../roomescape/ReservationApiController.java | 21 ++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/roomescape/Reservation.java create mode 100644 src/main/java/roomescape/ReservationApiController.java diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/Reservation.java new file mode 100644 index 000000000..e976047ba --- /dev/null +++ b/src/main/java/roomescape/Reservation.java @@ -0,0 +1,32 @@ +package roomescape; + +public class Reservation { + private Long id; + private String name; + private String date; + private String time; + + public Reservation(final Long id, final String name, final String date, final String time) { + this.id = id; + this.name = name; + this.date = date; + this.time = time; + } + + public Long getId() { + return id; + } + + public String getTime() { + return time; + } + + + public String getName() { + return name; + } + + public String getDate() { + return date; + } +} diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/ReservationApiController.java new file mode 100644 index 000000000..b3c4e0e97 --- /dev/null +++ b/src/main/java/roomescape/ReservationApiController.java @@ -0,0 +1,21 @@ +package roomescape; + +import java.util.ArrayList; +import java.util.List; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ReservationApiController { + + private final List reservations = new ArrayList<>(); + + @GetMapping("/reservations") + public ResponseEntity> getReservations() { + this.reservations.add(new Reservation(1L, "브라운", "2023-01-01", "10:00")); + this.reservations.add(new Reservation(2L, "브라운", "2023-01-02", "11:00")); + + return ResponseEntity.ok().body(this.reservations); + } +} From e9f15556e8f3d88166def801defa72f21cb6d64d Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 26 Oct 2024 23:51:03 +0900 Subject: [PATCH 07/29] =?UTF-8?q?test:=20read=20api=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/roomescape/MissionStepTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index 671ddbc0f..4b543d744 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -1,5 +1,7 @@ package roomescape; +import static org.hamcrest.Matchers.is; + import io.restassured.RestAssured; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -23,5 +25,11 @@ public class MissionStepTest { .when().get("/reservation") .then().log().all() .statusCode(200); + + RestAssured.given().log().all() + .when().get("/reservations") + .then().log().all() + .statusCode(200) + .body("size()", is(2)); } } From dfc3f82e69d1190f8403b9c6cefa9dc101209f9d Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sun, 27 Oct 2024 10:28:27 +0900 Subject: [PATCH 08/29] =?UTF-8?q?feat:=20=EC=98=88=EC=95=BD=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80,=20=EC=82=AD=EC=A0=9C=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/roomescape/Reservation.java | 4 ++ .../roomescape/ReservationApiController.java | 30 ++++++++++++-- src/test/java/roomescape/MissionStepTest.java | 39 ++++++++++++++++++- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/Reservation.java index e976047ba..b62414ef1 100644 --- a/src/main/java/roomescape/Reservation.java +++ b/src/main/java/roomescape/Reservation.java @@ -29,4 +29,8 @@ public String getName() { public String getDate() { return date; } + + public static Reservation toEntity(Reservation reservation, Long id) { + return new Reservation(id, reservation.name, reservation.date, reservation.time); + } } diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/ReservationApiController.java index b3c4e0e97..73b055510 100644 --- a/src/main/java/roomescape/ReservationApiController.java +++ b/src/main/java/roomescape/ReservationApiController.java @@ -1,21 +1,45 @@ package roomescape; +import java.net.URI; import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicLong; 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.RestController; @RestController public class ReservationApiController { private final List reservations = new ArrayList<>(); + private AtomicLong index = new AtomicLong(1); @GetMapping("/reservations") public ResponseEntity> getReservations() { - this.reservations.add(new Reservation(1L, "브라운", "2023-01-01", "10:00")); - this.reservations.add(new Reservation(2L, "브라운", "2023-01-02", "11:00")); - return ResponseEntity.ok().body(this.reservations); } + + @PostMapping("/reservations") + public ResponseEntity create(@RequestBody Reservation reservation) { + Reservation newReservation = Reservation.toEntity(reservation, index.getAndIncrement()); + reservations.add(newReservation); + return ResponseEntity.created(URI.create("/reservations/" + this.reservations.size())) + .body(newReservation); + } + + @DeleteMapping("/reservations/{id}") + public ResponseEntity delete(@PathVariable Long id) { + Reservation targetReservation = reservations.stream() + .filter(target -> Objects.equals(target.getId(), id)) + .findFirst() + .orElseThrow(RuntimeException::new); + + reservations.remove(targetReservation); + return ResponseEntity.noContent().build(); + } } diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index 4b543d744..731dd21c7 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -3,6 +3,9 @@ import static org.hamcrest.Matchers.is; import io.restassured.RestAssured; +import io.restassured.http.ContentType; +import java.util.HashMap; +import java.util.Map; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; @@ -30,6 +33,40 @@ public class MissionStepTest { .when().get("/reservations") .then().log().all() .statusCode(200) - .body("size()", is(2)); + .body("size()", is(0)); + } + + @Test + void 삼단계() { + Map params = new HashMap<>(); + params.put("name", "브라운"); + params.put("date", "2023-08-05"); + params.put("time", "15:40"); + + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .body(params) + .when().post("/reservations") + .then().log().all() + .statusCode(201) + .header("Location", "/reservations/1") + .body("id", is(1)); + + RestAssured.given().log().all() + .when().get("/reservations") + .then().log().all() + .statusCode(200) + .body("size()", is(1)); + + RestAssured.given().log().all() + .when().delete("/reservations/1") + .then().log().all() + .statusCode(204); + + RestAssured.given().log().all() + .when().get("/reservations") + .then().log().all() + .statusCode(200) + .body("size()", is(0)); } } From 156e220033f86a744791dbe7a066bc4992931a9f Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sun, 27 Oct 2024 10:52:24 +0900 Subject: [PATCH 09/29] =?UTF-8?q?feat:=20=EC=98=88=EC=95=BD=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80,=20=EC=B7=A8=EC=86=8C=20=EC=8B=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roomescape/ReservationApiController.java | 6 ++++- .../exceptions/BadRequestException.java | 12 ++++++++++ src/test/java/roomescape/MissionStepTest.java | 22 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/main/java/roomescape/exceptions/BadRequestException.java diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/ReservationApiController.java index 73b055510..adadf6fbc 100644 --- a/src/main/java/roomescape/ReservationApiController.java +++ b/src/main/java/roomescape/ReservationApiController.java @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import roomescape.exceptions.BadRequestException; @RestController public class ReservationApiController { @@ -26,6 +27,9 @@ public ResponseEntity> getReservations() { @PostMapping("/reservations") public ResponseEntity create(@RequestBody Reservation reservation) { + if (reservation.getDate().isEmpty() || reservation.getTime().isEmpty() || reservation.getName().isEmpty()) { + throw new BadRequestException("올바르지 않은 입력입니다."); + } Reservation newReservation = Reservation.toEntity(reservation, index.getAndIncrement()); reservations.add(newReservation); return ResponseEntity.created(URI.create("/reservations/" + this.reservations.size())) @@ -37,7 +41,7 @@ public ResponseEntity delete(@PathVariable Long id) { Reservation targetReservation = reservations.stream() .filter(target -> Objects.equals(target.getId(), id)) .findFirst() - .orElseThrow(RuntimeException::new); + .orElseThrow(()-> new BadRequestException("존재하지 않는 예약입니다.")); reservations.remove(targetReservation); return ResponseEntity.noContent().build(); diff --git a/src/main/java/roomescape/exceptions/BadRequestException.java b/src/main/java/roomescape/exceptions/BadRequestException.java new file mode 100644 index 000000000..fbdf13407 --- /dev/null +++ b/src/main/java/roomescape/exceptions/BadRequestException.java @@ -0,0 +1,12 @@ +package roomescape.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.BAD_REQUEST) +public class BadRequestException extends RuntimeException{ + + public BadRequestException(String message) { + super(message); + } +} diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index 731dd21c7..35066219a 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -69,4 +69,26 @@ public class MissionStepTest { .statusCode(200) .body("size()", is(0)); } + + @Test + void 사단계() { + Map params = new HashMap<>(); + params.put("name", "브라운"); + params.put("date", ""); + params.put("time", ""); + + // 필요한 인자가 없는 경우 + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .body(params) + .when().post("/reservations") + .then().log().all() + .statusCode(400); + + // 삭제할 예약이 없는 경우 + RestAssured.given().log().all() + .when().delete("/reservations/1") + .then().log().all() + .statusCode(400); + } } From 8963ac098f2facd71224aa8b6ffa0eba3e99c56d Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sun, 27 Oct 2024 17:10:28 +0900 Subject: [PATCH 10/29] =?UTF-8?q?build:=20jdbc,=20h2=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.gradle b/build.gradle index 40a581b15..6ae29239e 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,9 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.rest-assured:rest-assured:5.3.1' + // DB + implementation 'org.springframework.boot:spring-boot-starter-jdbc' + runtimeOnly 'com.h2database:h2' } test { From df35ea52e9905f9358b2c590c2077ba98c7bdb4f Mon Sep 17 00:00:00 2001 From: bingle625 Date: Wed, 30 Oct 2024 17:18:18 +0900 Subject: [PATCH 11/29] =?UTF-8?q?build:=20h2=20=EC=9D=98=EC=A1=B4=EC=84=B1?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.properties | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb..a68f6a662 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1,4 @@ +# h2-console ??? ?? +spring.h2.console.enabled=true +# db url +spring.datasource.url=jdbc:h2:mem:test \ No newline at end of file From 13e16ce86210a6d2ef37534f70c17807e83f77ae Mon Sep 17 00:00:00 2001 From: bingle625 Date: Thu, 31 Oct 2024 21:57:11 +0900 Subject: [PATCH 12/29] add: jdbc test --- src/main/java/jdbctest/Customer.java | 25 +++++++++++ .../java/jdbctest/CustomerController.java | 41 +++++++++++++++++++ src/main/java/jdbctest/DemoApplication.java | 35 ++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/main/java/jdbctest/Customer.java create mode 100644 src/main/java/jdbctest/CustomerController.java create mode 100644 src/main/java/jdbctest/DemoApplication.java diff --git a/src/main/java/jdbctest/Customer.java b/src/main/java/jdbctest/Customer.java new file mode 100644 index 000000000..ae2eb9bad --- /dev/null +++ b/src/main/java/jdbctest/Customer.java @@ -0,0 +1,25 @@ +package jdbctest; + +public class Customer { + private long id; + private String firstName; + private String lastName; + + public Customer(long id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public long getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } +} diff --git a/src/main/java/jdbctest/CustomerController.java b/src/main/java/jdbctest/CustomerController.java new file mode 100644 index 000000000..7e981b263 --- /dev/null +++ b/src/main/java/jdbctest/CustomerController.java @@ -0,0 +1,41 @@ +package jdbctest; + +import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class CustomerController { + private JdbcTemplate jdbcTemplate; + + public CustomerController(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @PostMapping("/customers") + public ResponseEntity save(@RequestBody Customer customer) { + String sql = "INSERT INTO customers(first_name, last_name) VALUES (?,?)"; + jdbcTemplate.update(sql, customer.getFirstName(), customer.getLastName()); + return ResponseEntity.ok().build(); + } + + @GetMapping("/customers") + public ResponseEntity> list() { + String sql = "select id, first_name, last_name from customers"; + List customers = jdbcTemplate.query( + sql, (resultSet, rowNum) -> { + Customer customer = new Customer( + resultSet.getLong("id"), + resultSet.getString("first_name"), + resultSet.getString("last_name") + ); + return customer; + }); + return ResponseEntity.ok().body(customers); + } +} diff --git a/src/main/java/jdbctest/DemoApplication.java b/src/main/java/jdbctest/DemoApplication.java new file mode 100644 index 000000000..2aa213315 --- /dev/null +++ b/src/main/java/jdbctest/DemoApplication.java @@ -0,0 +1,35 @@ +package jdbctest; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.jdbc.core.JdbcTemplate; + +@SpringBootApplication +public class DemoApplication implements CommandLineRunner { + + public static void main(String args[]) { + SpringApplication.run(DemoApplication.class, args); + } + + @Autowired + JdbcTemplate jdbcTemplate; + + @Override + public void run(String... strings) throws Exception { + + jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); + jdbcTemplate.execute("CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); + + List splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() + .map(name -> name.split(" ")) + .collect(Collectors.toList()); + + jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); + + } +} From aa595075e75deed894b8397c863dae691fc7a48a Mon Sep 17 00:00:00 2001 From: bingle625 Date: Thu, 31 Oct 2024 22:20:47 +0900 Subject: [PATCH 13/29] =?UTF-8?q?test:=205=EB=8B=A8=EA=B3=84=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 예약 DB schema.sql 추가 --- build.gradle | 2 ++ src/main/resources/application.properties | 2 +- src/main/resources/schema.sql | 10 ++++++++++ src/test/java/roomescape/MissionStepTest.java | 20 +++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/schema.sql diff --git a/build.gradle b/build.gradle index 6ae29239e..e564076e4 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,8 @@ dependencies { // DB implementation 'org.springframework.boot:spring-boot-starter-jdbc' runtimeOnly 'com.h2database:h2' + + } test { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a68f6a662..2cd755b96 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ # h2-console ??? ?? spring.h2.console.enabled=true # db url -spring.datasource.url=jdbc:h2:mem:test \ No newline at end of file +spring.datasource.url=jdbc:h2:mem:database \ 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 000000000..0b51dbe8a --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,10 @@ +DROP TABLE IF EXISTS reservation; + +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/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index 35066219a..da54d4a59 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -1,19 +1,27 @@ package roomescape; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.is; import io.restassured.RestAssured; import io.restassured.http.ContentType; +import java.sql.Connection; +import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.annotation.DirtiesContext; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) public class MissionStepTest { + @Autowired + private JdbcTemplate jdbcTemplate; + @Test void 일단계() { RestAssured.given().log().all() @@ -91,4 +99,16 @@ public class MissionStepTest { .then().log().all() .statusCode(400); } + + @Test + void 오단계() { + try (Connection connection = jdbcTemplate.getDataSource().getConnection()) { + assertThat(connection).isNotNull(); + assertThat(connection.getCatalog()).isEqualTo("DATABASE"); + assertThat(connection.getMetaData().getTables(null, null, "RESERVATION", null).next()).isTrue(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } From 2e8d0576f92410d463ede9000a1e01adbdf7709f Mon Sep 17 00:00:00 2001 From: bingle625 Date: Thu, 31 Oct 2024 22:23:17 +0900 Subject: [PATCH 14/29] =?UTF-8?q?test:=206=EB=8B=A8=EA=B3=84=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 예약 조회 테스트 --- src/test/java/roomescape/MissionStepTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index da54d4a59..167b235ae 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -8,6 +8,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -111,4 +112,20 @@ public class MissionStepTest { } } + @Test + void 육단계() { + jdbcTemplate.update("INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)", "브라운", "2023-08-05", "15:40"); + + List reservations = RestAssured.given().log().all() + .when().get("/reservations") + .then().log().all() + .statusCode(200).extract() + .jsonPath().getList(".", Reservation.class); + + Integer count = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class); + + assertThat(reservations.size()).isEqualTo(count); + } + + } From 09f939fd10679f92df55e43d7d5e1e4679ff5c9d Mon Sep 17 00:00:00 2001 From: bingle625 Date: Thu, 31 Oct 2024 22:28:22 +0900 Subject: [PATCH 15/29] =?UTF-8?q?feat:=20=EC=98=88=EC=95=BD=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EB=8D=B0=EC=9D=B4=ED=84=B0?= =?UTF-8?q?=EB=B2=A0=EC=9D=B4=EC=8A=A4=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/roomescape/ReservationApiController.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/ReservationApiController.java index adadf6fbc..a05315925 100644 --- a/src/main/java/roomescape/ReservationApiController.java +++ b/src/main/java/roomescape/ReservationApiController.java @@ -5,7 +5,9 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -20,14 +22,21 @@ public class ReservationApiController { private final List reservations = new ArrayList<>(); private AtomicLong index = new AtomicLong(1); + @Autowired + private JdbcTemplate jdbcTemplate; + @GetMapping("/reservations") public ResponseEntity> getReservations() { - return ResponseEntity.ok().body(this.reservations); + List reservations = jdbcTemplate.query("select * from reservation", + ((rs, rowNum) -> new Reservation(rs.getLong("id"), rs.getString("name"), + rs.getString("date"), rs.getString("time")))); + return ResponseEntity.ok().body(reservations); } @PostMapping("/reservations") public ResponseEntity create(@RequestBody Reservation reservation) { - if (reservation.getDate().isEmpty() || reservation.getTime().isEmpty() || reservation.getName().isEmpty()) { + if (reservation.getDate().isEmpty() || reservation.getTime().isEmpty() || reservation.getName() + .isEmpty()) { throw new BadRequestException("올바르지 않은 입력입니다."); } Reservation newReservation = Reservation.toEntity(reservation, index.getAndIncrement()); @@ -41,7 +50,7 @@ public ResponseEntity delete(@PathVariable Long id) { Reservation targetReservation = reservations.stream() .filter(target -> Objects.equals(target.getId(), id)) .findFirst() - .orElseThrow(()-> new BadRequestException("존재하지 않는 예약입니다.")); + .orElseThrow(() -> new BadRequestException("존재하지 않는 예약입니다.")); reservations.remove(targetReservation); return ResponseEntity.noContent().build(); From 4aec219118c6739fec4fb0ef2cb3417470aa5582 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Thu, 31 Oct 2024 23:10:59 +0900 Subject: [PATCH 16/29] =?UTF-8?q?feat:=20=EC=98=88=EC=95=BD=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80/=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4=EC=8A=A4=20=EC=97=B0?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/roomescape/Reservation.java | 5 +++ .../roomescape/ReservationApiController.java | 41 ++++++++++++------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/Reservation.java index b62414ef1..14ccacc7d 100644 --- a/src/main/java/roomescape/Reservation.java +++ b/src/main/java/roomescape/Reservation.java @@ -1,6 +1,7 @@ package roomescape; public class Reservation { + private Long id; private String name; private String date; @@ -33,4 +34,8 @@ public String getDate() { public static Reservation toEntity(Reservation reservation, Long id) { return new Reservation(id, reservation.name, reservation.date, reservation.time); } + + public void setId(final Long id) { + this.id = id; + } } diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/ReservationApiController.java index a05315925..6c0ba5428 100644 --- a/src/main/java/roomescape/ReservationApiController.java +++ b/src/main/java/roomescape/ReservationApiController.java @@ -2,12 +2,15 @@ import java.net.URI; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -19,9 +22,6 @@ @RestController public class ReservationApiController { - private final List reservations = new ArrayList<>(); - private AtomicLong index = new AtomicLong(1); - @Autowired private JdbcTemplate jdbcTemplate; @@ -35,24 +35,37 @@ public ResponseEntity> getReservations() { @PostMapping("/reservations") public ResponseEntity create(@RequestBody Reservation reservation) { - if (reservation.getDate().isEmpty() || reservation.getTime().isEmpty() || reservation.getName() - .isEmpty()) { + if (Objects.isNull(reservation.getDate()) || reservation.getDate().isEmpty() || + Objects.isNull(reservation.getTime()) || reservation.getTime().isEmpty() || + Objects.isNull(reservation.getName()) || reservation.getName().isEmpty()) { throw new BadRequestException("올바르지 않은 입력입니다."); } - Reservation newReservation = Reservation.toEntity(reservation, index.getAndIncrement()); - reservations.add(newReservation); - return ResponseEntity.created(URI.create("/reservations/" + this.reservations.size())) - .body(newReservation); + + SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName( + "reservation").usingGeneratedKeyColumns("id"); + + Map map = new HashMap<>(); + map.put("name", reservation.getName()); + map.put("date", reservation.getDate()); + map.put("time", reservation.getTime()); + Number newId = simpleJdbcInsert.executeAndReturnKey(map); + reservation.setId(newId.longValue()); + + return ResponseEntity.created(URI.create("/reservations/" + newId.longValue())) + .body(reservation); } @DeleteMapping("/reservations/{id}") public ResponseEntity delete(@PathVariable Long id) { - Reservation targetReservation = reservations.stream() - .filter(target -> Objects.equals(target.getId(), id)) - .findFirst() - .orElseThrow(() -> new BadRequestException("존재하지 않는 예약입니다.")); + int count = this.jdbcTemplate.queryForObject( + "select count(*) from reservation where id = ?", new Object[]{id}, int.class); + + if (count == 0) { + throw new BadRequestException("존재하지 않는 예약입니다."); + } + + this.jdbcTemplate.update("delete from reservation where id = ?", id); - reservations.remove(targetReservation); return ResponseEntity.noContent().build(); } } From d7133d8985ee890037064af425d253a9e477d270 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Fri, 1 Nov 2024 22:38:19 +0900 Subject: [PATCH 17/29] add: repository --- build.gradle | 1 + src/main/java/roomescape/Reservation.java | 12 ++++++++++++ src/main/java/roomescape/ReservationInterface.java | 7 +++++++ 3 files changed, 20 insertions(+) create mode 100644 src/main/java/roomescape/ReservationInterface.java diff --git a/build.gradle b/build.gradle index e564076e4..47b598cc6 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.rest-assured:rest-assured:5.3.1' // DB diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/Reservation.java index 14ccacc7d..0022317da 100644 --- a/src/main/java/roomescape/Reservation.java +++ b/src/main/java/roomescape/Reservation.java @@ -1,8 +1,20 @@ package roomescape; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +@Entity public class Reservation { + protected Reservation() { + } + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + private String name; private String date; private String time; diff --git a/src/main/java/roomescape/ReservationInterface.java b/src/main/java/roomescape/ReservationInterface.java new file mode 100644 index 000000000..654d6cbc1 --- /dev/null +++ b/src/main/java/roomescape/ReservationInterface.java @@ -0,0 +1,7 @@ +package roomescape; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ReservationInterface extends JpaRepository { + Reservation findByName(String name); +} From d41fef94cda6163132ac9203d017d70bef1733bd Mon Sep 17 00:00:00 2001 From: bingle625 Date: Fri, 1 Nov 2024 22:47:01 +0900 Subject: [PATCH 18/29] =?UTF-8?q?feat:=20jpaRepository=20=EC=9D=B4?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/roomescape/Reservation.java | 10 ++++++++-- .../roomescape/ReservationApiController.java | 17 ++++++----------- ...nterface.java => ReservationRepository.java} | 2 +- src/test/java/roomescape/MissionStepTest.java | 6 +++++- 4 files changed, 20 insertions(+), 15 deletions(-) rename src/main/java/roomescape/{ReservationInterface.java => ReservationRepository.java} (60%) diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/Reservation.java index 0022317da..fa01de5d5 100644 --- a/src/main/java/roomescape/Reservation.java +++ b/src/main/java/roomescape/Reservation.java @@ -19,6 +19,12 @@ protected Reservation() { private String date; private String time; + public Reservation(final String name, final String date, final String time) { + this.name = name; + this.date = date; + this.time = time; + } + public Reservation(final Long id, final String name, final String date, final String time) { this.id = id; this.name = name; @@ -43,8 +49,8 @@ public String getDate() { return date; } - public static Reservation toEntity(Reservation reservation, Long id) { - return new Reservation(id, reservation.name, reservation.date, reservation.time); + public static Reservation toEntity(Reservation reservation) { + return new Reservation(reservation.name, reservation.date, reservation.time); } public void setId(final Long id) { diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/ReservationApiController.java index 6c0ba5428..ff724e55d 100644 --- a/src/main/java/roomescape/ReservationApiController.java +++ b/src/main/java/roomescape/ReservationApiController.java @@ -25,6 +25,9 @@ public class ReservationApiController { @Autowired private JdbcTemplate jdbcTemplate; + @Autowired + private ReservationRepository reservationRepository; + @GetMapping("/reservations") public ResponseEntity> getReservations() { List reservations = jdbcTemplate.query("select * from reservation", @@ -41,18 +44,10 @@ public ResponseEntity create(@RequestBody Reservation reservation) throw new BadRequestException("올바르지 않은 입력입니다."); } - SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName( - "reservation").usingGeneratedKeyColumns("id"); - - Map map = new HashMap<>(); - map.put("name", reservation.getName()); - map.put("date", reservation.getDate()); - map.put("time", reservation.getTime()); - Number newId = simpleJdbcInsert.executeAndReturnKey(map); - reservation.setId(newId.longValue()); + Reservation newReservation = this.reservationRepository.save(reservation); - return ResponseEntity.created(URI.create("/reservations/" + newId.longValue())) - .body(reservation); + return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())) + .body(newReservation); } @DeleteMapping("/reservations/{id}") diff --git a/src/main/java/roomescape/ReservationInterface.java b/src/main/java/roomescape/ReservationRepository.java similarity index 60% rename from src/main/java/roomescape/ReservationInterface.java rename to src/main/java/roomescape/ReservationRepository.java index 654d6cbc1..4a753b0f5 100644 --- a/src/main/java/roomescape/ReservationInterface.java +++ b/src/main/java/roomescape/ReservationRepository.java @@ -2,6 +2,6 @@ import org.springframework.data.jpa.repository.JpaRepository; -public interface ReservationInterface extends JpaRepository { +public interface ReservationRepository extends JpaRepository { Reservation findByName(String name); } diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index 167b235ae..a31a6c62a 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -23,6 +23,9 @@ public class MissionStepTest { @Autowired private JdbcTemplate jdbcTemplate; + @Autowired + private ReservationRepository reservationRepository; + @Test void 일단계() { RestAssured.given().log().all() @@ -114,8 +117,9 @@ public class MissionStepTest { @Test void 육단계() { - jdbcTemplate.update("INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)", "브라운", "2023-08-05", "15:40"); +// jdbcTemplate.update("INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)", "브라운", "2023-08-05", "15:40"); + reservationRepository.save(new Reservation("브라운", "2023-08-05", "15:40")); List reservations = RestAssured.given().log().all() .when().get("/reservations") .then().log().all() From 8b7ffcda2dca077f066b8c376ac5b4b85f4997ee Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 16 Nov 2024 19:04:02 +0900 Subject: [PATCH 19/29] =?UTF-8?q?test:=20Hello=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20Spring=20Bean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Hello/Hello.java | 30 ++++++++++++ src/main/java/Hello/Printer.java | 8 ++++ src/main/java/Hello/StringPrinter.java | 5 ++ src/test/java/Hello/HelloTest.java | 65 ++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/main/java/Hello/Hello.java create mode 100644 src/main/java/Hello/Printer.java create mode 100644 src/main/java/Hello/StringPrinter.java create mode 100644 src/test/java/Hello/HelloTest.java diff --git a/src/main/java/Hello/Hello.java b/src/main/java/Hello/Hello.java new file mode 100644 index 000000000..90590d808 --- /dev/null +++ b/src/main/java/Hello/Hello.java @@ -0,0 +1,30 @@ +package Hello; + +public class Hello { + String name; + Printer printer; + + public Hello() { + } + + public Hello(String name, Printer printer) { + this.name = name; + this.printer = printer; + } + + public void setName(String name) { + this.name = name; + } + + public void setPrinter(Printer printer) { + this.printer = printer; + } + + public String sayHello() { + return "Hello " + this.name; + } + + public void print() { + this.printer.print(sayHello()); + } +} diff --git a/src/main/java/Hello/Printer.java b/src/main/java/Hello/Printer.java new file mode 100644 index 000000000..19f7cbc82 --- /dev/null +++ b/src/main/java/Hello/Printer.java @@ -0,0 +1,8 @@ +package Hello; + +public class Printer { + + public void print(String data) { + System.out.println(data); + } +} diff --git a/src/main/java/Hello/StringPrinter.java b/src/main/java/Hello/StringPrinter.java new file mode 100644 index 000000000..fe2267f20 --- /dev/null +++ b/src/main/java/Hello/StringPrinter.java @@ -0,0 +1,5 @@ +package Hello; + +public class StringPrinter extends Printer{ + +} diff --git a/src/test/java/Hello/HelloTest.java b/src/test/java/Hello/HelloTest.java new file mode 100644 index 000000000..16bc748c7 --- /dev/null +++ b/src/test/java/Hello/HelloTest.java @@ -0,0 +1,65 @@ +package Hello; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.RuntimeBeanReference; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.support.StaticApplicationContext; +import org.springframework.test.annotation.DirtiesContext; + +@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) +public class HelloTest { + @Test + void test1() { + // IoC 컨테이너 생성 + StaticApplicationContext context = new StaticApplicationContext(); + // Hello 클래스를 싱글톤 빈으로 컨테이너에 등록 + context.registerSingleton("hello", Hello.class); + + Hello hello = context.getBean("hello", Hello.class); + assertThat(hello).isNotNull(); + } + + @Test + void test2() { + StaticApplicationContext context = new StaticApplicationContext(); + context.registerSingleton("hello1", Hello.class); + + Hello hello1 = context.getBean("hello1", Hello.class); + assertThat(hello1).isNotNull(); + + // 빈 메정보를 담은 오프젝트 생성 + BeanDefinition helloDef = new RootBeanDefinition(Hello.class); + // 빈 프로퍼티 설정 + helloDef.getPropertyValues().addPropertyValue("name", "Spring"); + // 빈 메타정보를 hello2 라는 이름으로 컨테이너에 등록 + context.registerBeanDefinition("hello2", helloDef); + + Hello hello2 = context.getBean("hello2", Hello.class); + assertThat(hello2).isNotNull(); + assertThat(hello2.sayHello()).isEqualTo("Hello Spring"); + + assertThat(hello1).isNotSameAs(hello2); + assertThat(context.getBeanFactory().getBeanDefinitionCount()).isEqualTo(2); + } + + @Test + void test3() { + StaticApplicationContext context = new StaticApplicationContext(); + context.registerBeanDefinition("printer", new RootBeanDefinition(StringPrinter.class)); + + BeanDefinition helloDef = new RootBeanDefinition(Hello.class); + helloDef.getPropertyValues().addPropertyValue("name", "Spring"); + // 아이디가 printer 인 빈을 찾아서 printer 프로퍼티에 DI + helloDef.getPropertyValues().addPropertyValue("prin ter", new RuntimeBeanReference("printer")); + context.registerBeanDefinition("hello", helloDef); + + Hello hello = context.getBean("hello", Hello.class); + hello.print(); + + assertThat(context.getBean("printer").toString()).isEqualTo("Hello Spring"); + } +} From 251ca70a6d682fb8cb201f5857e50569446f1498 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 16 Nov 2024 19:04:02 +0900 Subject: [PATCH 20/29] =?UTF-8?q?test:=20Hello=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20Spring=20Bean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Hello/Hello.java | 30 ++++++++++++ src/main/java/Hello/Printer.java | 8 ++++ src/main/java/Hello/StringPrinter.java | 5 ++ src/test/java/Hello/HelloTest.java | 65 ++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/main/java/Hello/Hello.java create mode 100644 src/main/java/Hello/Printer.java create mode 100644 src/main/java/Hello/StringPrinter.java create mode 100644 src/test/java/Hello/HelloTest.java diff --git a/src/main/java/Hello/Hello.java b/src/main/java/Hello/Hello.java new file mode 100644 index 000000000..90590d808 --- /dev/null +++ b/src/main/java/Hello/Hello.java @@ -0,0 +1,30 @@ +package Hello; + +public class Hello { + String name; + Printer printer; + + public Hello() { + } + + public Hello(String name, Printer printer) { + this.name = name; + this.printer = printer; + } + + public void setName(String name) { + this.name = name; + } + + public void setPrinter(Printer printer) { + this.printer = printer; + } + + public String sayHello() { + return "Hello " + this.name; + } + + public void print() { + this.printer.print(sayHello()); + } +} diff --git a/src/main/java/Hello/Printer.java b/src/main/java/Hello/Printer.java new file mode 100644 index 000000000..19f7cbc82 --- /dev/null +++ b/src/main/java/Hello/Printer.java @@ -0,0 +1,8 @@ +package Hello; + +public class Printer { + + public void print(String data) { + System.out.println(data); + } +} diff --git a/src/main/java/Hello/StringPrinter.java b/src/main/java/Hello/StringPrinter.java new file mode 100644 index 000000000..fe2267f20 --- /dev/null +++ b/src/main/java/Hello/StringPrinter.java @@ -0,0 +1,5 @@ +package Hello; + +public class StringPrinter extends Printer{ + +} diff --git a/src/test/java/Hello/HelloTest.java b/src/test/java/Hello/HelloTest.java new file mode 100644 index 000000000..16bc748c7 --- /dev/null +++ b/src/test/java/Hello/HelloTest.java @@ -0,0 +1,65 @@ +package Hello; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.RuntimeBeanReference; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.support.StaticApplicationContext; +import org.springframework.test.annotation.DirtiesContext; + +@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) +public class HelloTest { + @Test + void test1() { + // IoC 컨테이너 생성 + StaticApplicationContext context = new StaticApplicationContext(); + // Hello 클래스를 싱글톤 빈으로 컨테이너에 등록 + context.registerSingleton("hello", Hello.class); + + Hello hello = context.getBean("hello", Hello.class); + assertThat(hello).isNotNull(); + } + + @Test + void test2() { + StaticApplicationContext context = new StaticApplicationContext(); + context.registerSingleton("hello1", Hello.class); + + Hello hello1 = context.getBean("hello1", Hello.class); + assertThat(hello1).isNotNull(); + + // 빈 메정보를 담은 오프젝트 생성 + BeanDefinition helloDef = new RootBeanDefinition(Hello.class); + // 빈 프로퍼티 설정 + helloDef.getPropertyValues().addPropertyValue("name", "Spring"); + // 빈 메타정보를 hello2 라는 이름으로 컨테이너에 등록 + context.registerBeanDefinition("hello2", helloDef); + + Hello hello2 = context.getBean("hello2", Hello.class); + assertThat(hello2).isNotNull(); + assertThat(hello2.sayHello()).isEqualTo("Hello Spring"); + + assertThat(hello1).isNotSameAs(hello2); + assertThat(context.getBeanFactory().getBeanDefinitionCount()).isEqualTo(2); + } + + @Test + void test3() { + StaticApplicationContext context = new StaticApplicationContext(); + context.registerBeanDefinition("printer", new RootBeanDefinition(StringPrinter.class)); + + BeanDefinition helloDef = new RootBeanDefinition(Hello.class); + helloDef.getPropertyValues().addPropertyValue("name", "Spring"); + // 아이디가 printer 인 빈을 찾아서 printer 프로퍼티에 DI + helloDef.getPropertyValues().addPropertyValue("prin ter", new RuntimeBeanReference("printer")); + context.registerBeanDefinition("hello", helloDef); + + Hello hello = context.getBean("hello", Hello.class); + hello.print(); + + assertThat(context.getBean("printer").toString()).isEqualTo("Hello Spring"); + } +} From 2662b29484f9de833ea0214b8829b53f7ccfe199 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 16 Nov 2024 19:14:54 +0900 Subject: [PATCH 21/29] =?UTF-8?q?feat:=20delete=20=EA=B8=B0=EB=8A=A5=20jpa?= =?UTF-8?q?=20repository=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roomescape/ReservationApiController.java | 13 ++++------- src/test/java/roomescape/MissionStepTest.java | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/ReservationApiController.java index ff724e55d..4e530db50 100644 --- a/src/main/java/roomescape/ReservationApiController.java +++ b/src/main/java/roomescape/ReservationApiController.java @@ -44,7 +44,7 @@ public ResponseEntity create(@RequestBody Reservation reservation) throw new BadRequestException("올바르지 않은 입력입니다."); } - Reservation newReservation = this.reservationRepository.save(reservation); + Reservation newReservation = this.reservationRepository.save(reservation); return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())) .body(newReservation); @@ -52,14 +52,9 @@ public ResponseEntity create(@RequestBody Reservation reservation) @DeleteMapping("/reservations/{id}") public ResponseEntity delete(@PathVariable Long id) { - int count = this.jdbcTemplate.queryForObject( - "select count(*) from reservation where id = ?", new Object[]{id}, int.class); - - if (count == 0) { - throw new BadRequestException("존재하지 않는 예약입니다."); - } - - this.jdbcTemplate.update("delete from reservation where id = ?", id); + Reservation reservation = this.reservationRepository.findById(id) + .orElseThrow(() -> new BadRequestException("존재하지 않는 예약입니다.")); + this.reservationRepository.delete(reservation); return ResponseEntity.noContent().build(); } diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index a31a6c62a..d966d340c 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -131,5 +131,28 @@ public class MissionStepTest { assertThat(reservations.size()).isEqualTo(count); } + @Test + void 팔단계() { + Map params = new HashMap<>(); + params.put("time", "10:00"); + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .body(params) + .when().post("/times") + .then().log().all() + .statusCode(201) + .header("Location", "/times/1"); + + RestAssured.given().log().all() + .when().get("/times") + .then().log().all() + .statusCode(200) + .body("size()", is(1)); + + RestAssured.given().log().all() + .when().delete("/times/1") + .then().log().all() + .statusCode(204); + } } From cfc3c26538cd4d4dd85d4d5e9cce5dfa9337c747 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 16 Nov 2024 19:18:00 +0900 Subject: [PATCH 22/29] =?UTF-8?q?modify:=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=EB=B3=84=EB=A1=9C=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=20=EB=B0=8F=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20jdbcTemplate=20=EC=9D=98=EC=A1=B4=EC=84=B1?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Reservation}/Reservation.java | 2 +- .../ReservationApiController.java | 14 +---- .../Reservation}/ReservationController.java | 2 +- .../Reservation}/ReservationRepository.java | 2 +- .../Domains/Time/TimeApiController.java | 58 +++++++++++++++++++ src/test/java/roomescape/MissionStepTest.java | 2 + 6 files changed, 65 insertions(+), 15 deletions(-) rename src/main/java/roomescape/{ => Domains/Reservation}/Reservation.java (96%) rename src/main/java/roomescape/{ => Domains/Reservation}/ReservationApiController.java (80%) rename src/main/java/roomescape/{ => Domains/Reservation}/ReservationController.java (86%) rename src/main/java/roomescape/{ => Domains/Reservation}/ReservationRepository.java (82%) create mode 100644 src/main/java/roomescape/Domains/Time/TimeApiController.java diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/Domains/Reservation/Reservation.java similarity index 96% rename from src/main/java/roomescape/Reservation.java rename to src/main/java/roomescape/Domains/Reservation/Reservation.java index fa01de5d5..8aa564f97 100644 --- a/src/main/java/roomescape/Reservation.java +++ b/src/main/java/roomescape/Domains/Reservation/Reservation.java @@ -1,4 +1,4 @@ -package roomescape; +package roomescape.Domains.Reservation; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; diff --git a/src/main/java/roomescape/ReservationApiController.java b/src/main/java/roomescape/Domains/Reservation/ReservationApiController.java similarity index 80% rename from src/main/java/roomescape/ReservationApiController.java rename to src/main/java/roomescape/Domains/Reservation/ReservationApiController.java index 4e530db50..dc6a243f5 100644 --- a/src/main/java/roomescape/ReservationApiController.java +++ b/src/main/java/roomescape/Domains/Reservation/ReservationApiController.java @@ -1,16 +1,11 @@ -package roomescape; +package roomescape.Domains.Reservation; import java.net.URI; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.concurrent.atomic.AtomicLong; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -22,17 +17,12 @@ @RestController public class ReservationApiController { - @Autowired - private JdbcTemplate jdbcTemplate; - @Autowired private ReservationRepository reservationRepository; @GetMapping("/reservations") public ResponseEntity> getReservations() { - List reservations = jdbcTemplate.query("select * from reservation", - ((rs, rowNum) -> new Reservation(rs.getLong("id"), rs.getString("name"), - rs.getString("date"), rs.getString("time")))); + List reservations = this.reservationRepository.findAll(); return ResponseEntity.ok().body(reservations); } diff --git a/src/main/java/roomescape/ReservationController.java b/src/main/java/roomescape/Domains/Reservation/ReservationController.java similarity index 86% rename from src/main/java/roomescape/ReservationController.java rename to src/main/java/roomescape/Domains/Reservation/ReservationController.java index cda48bd6b..55918bbd2 100644 --- a/src/main/java/roomescape/ReservationController.java +++ b/src/main/java/roomescape/Domains/Reservation/ReservationController.java @@ -1,4 +1,4 @@ -package roomescape; +package roomescape.Domains.Reservation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; diff --git a/src/main/java/roomescape/ReservationRepository.java b/src/main/java/roomescape/Domains/Reservation/ReservationRepository.java similarity index 82% rename from src/main/java/roomescape/ReservationRepository.java rename to src/main/java/roomescape/Domains/Reservation/ReservationRepository.java index 4a753b0f5..f29689e4f 100644 --- a/src/main/java/roomescape/ReservationRepository.java +++ b/src/main/java/roomescape/Domains/Reservation/ReservationRepository.java @@ -1,4 +1,4 @@ -package roomescape; +package roomescape.Domains.Reservation; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/src/main/java/roomescape/Domains/Time/TimeApiController.java b/src/main/java/roomescape/Domains/Time/TimeApiController.java new file mode 100644 index 000000000..69ef5d04b --- /dev/null +++ b/src/main/java/roomescape/Domains/Time/TimeApiController.java @@ -0,0 +1,58 @@ +package roomescape.Domains.Time; + +import java.net.URI; +import java.util.List; +import java.util.Objects; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; +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.RestController; +import roomescape.Domains.Reservation.Reservation; +import roomescape.Domains.Reservation.ReservationRepository; +import roomescape.exceptions.BadRequestException; + +@RestController +public class TimeApiController { + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Autowired + private ReservationRepository reservationRepository; + + @GetMapping("/reservations") + public ResponseEntity> getReservations() { + List reservations = jdbcTemplate.query("select * from reservation", + ((rs, rowNum) -> new Reservation(rs.getLong("id"), rs.getString("name"), + rs.getString("date"), rs.getString("time")))); + return ResponseEntity.ok().body(reservations); + } + + @PostMapping("/reservations") + public ResponseEntity create(@RequestBody Reservation reservation) { + if (Objects.isNull(reservation.getDate()) || reservation.getDate().isEmpty() || + Objects.isNull(reservation.getTime()) || reservation.getTime().isEmpty() || + Objects.isNull(reservation.getName()) || reservation.getName().isEmpty()) { + throw new BadRequestException("올바르지 않은 입력입니다."); + } + + Reservation newReservation = this.reservationRepository.save(reservation); + + return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())) + .body(newReservation); + } + + @DeleteMapping("/reservations/{id}") + public ResponseEntity delete(@PathVariable Long id) { + Reservation reservation = this.reservationRepository.findById(id) + .orElseThrow(() -> new BadRequestException("존재하지 않는 예약입니다.")); + this.reservationRepository.delete(reservation); + + return ResponseEntity.noContent().build(); + } +} diff --git a/src/test/java/roomescape/MissionStepTest.java b/src/test/java/roomescape/MissionStepTest.java index d966d340c..eb6237842 100644 --- a/src/test/java/roomescape/MissionStepTest.java +++ b/src/test/java/roomescape/MissionStepTest.java @@ -15,6 +15,8 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.annotation.DirtiesContext; +import roomescape.Domains.Reservation.Reservation; +import roomescape.Domains.Reservation.ReservationRepository; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) From 610e556dba0f83c021f39d52597fcff24c0a2fbf Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 16 Nov 2024 19:31:04 +0900 Subject: [PATCH 23/29] =?UTF-8?q?feat:=20Time=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0,=20=EC=8A=A4=EC=BC=80=EB=A7=88=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20=EB=B0=8F=20TimeApiController=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/roomescape/Domains/Time/Time.java | 19 ++++++++++ .../Domains/Time/TimeApiController.java | 36 ------------------- src/main/resources/schema.sql | 8 +++++ 3 files changed, 27 insertions(+), 36 deletions(-) create mode 100644 src/main/java/roomescape/Domains/Time/Time.java diff --git a/src/main/java/roomescape/Domains/Time/Time.java b/src/main/java/roomescape/Domains/Time/Time.java new file mode 100644 index 000000000..eec8b033e --- /dev/null +++ b/src/main/java/roomescape/Domains/Time/Time.java @@ -0,0 +1,19 @@ +package roomescape.Domains.Time; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +@Entity +public class Time { + + public Time() { + } + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String time; +} diff --git a/src/main/java/roomescape/Domains/Time/TimeApiController.java b/src/main/java/roomescape/Domains/Time/TimeApiController.java index 69ef5d04b..91269b328 100644 --- a/src/main/java/roomescape/Domains/Time/TimeApiController.java +++ b/src/main/java/roomescape/Domains/Time/TimeApiController.java @@ -19,40 +19,4 @@ @RestController public class TimeApiController { - @Autowired - private JdbcTemplate jdbcTemplate; - - @Autowired - private ReservationRepository reservationRepository; - - @GetMapping("/reservations") - public ResponseEntity> getReservations() { - List reservations = jdbcTemplate.query("select * from reservation", - ((rs, rowNum) -> new Reservation(rs.getLong("id"), rs.getString("name"), - rs.getString("date"), rs.getString("time")))); - return ResponseEntity.ok().body(reservations); - } - - @PostMapping("/reservations") - public ResponseEntity create(@RequestBody Reservation reservation) { - if (Objects.isNull(reservation.getDate()) || reservation.getDate().isEmpty() || - Objects.isNull(reservation.getTime()) || reservation.getTime().isEmpty() || - Objects.isNull(reservation.getName()) || reservation.getName().isEmpty()) { - throw new BadRequestException("올바르지 않은 입력입니다."); - } - - Reservation newReservation = this.reservationRepository.save(reservation); - - return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())) - .body(newReservation); - } - - @DeleteMapping("/reservations/{id}") - public ResponseEntity delete(@PathVariable Long id) { - Reservation reservation = this.reservationRepository.findById(id) - .orElseThrow(() -> new BadRequestException("존재하지 않는 예약입니다.")); - this.reservationRepository.delete(reservation); - - return ResponseEntity.noContent().build(); - } } diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 0b51dbe8a..cf8c11594 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -8,3 +8,11 @@ CREATE TABLE reservation time VARCHAR(255) NOT NULL, PRIMARY KEY (id) ); + +CREATE TABLE time +( + id BIGINT NOT NULL AUTO_INCREMENT, + time VARCHAR(255) NOT NULL, + PRIMARY KEY (id) +); + From 23180821ce402d79eaaec7081531adce3b9b6ea0 Mon Sep 17 00:00:00 2001 From: bingle625 Date: Sat, 16 Nov 2024 19:48:16 +0900 Subject: [PATCH 24/29] =?UTF-8?q?feat:=20Time=20store,=20index,=20delete?= =?UTF-8?q?=20api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - store시 RequestBody를 Map으로 받아봄 --- .../Reservation/ReservationApiController.java | 1 - .../java/roomescape/Domains/Time/Time.java | 4 ++ .../Domains/Time/TimeApiController.java | 39 +++++++++++++++++-- .../Domains/Time/TimeRepository.java | 8 ++++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/main/java/roomescape/Domains/Time/TimeRepository.java diff --git a/src/main/java/roomescape/Domains/Reservation/ReservationApiController.java b/src/main/java/roomescape/Domains/Reservation/ReservationApiController.java index dc6a243f5..5732cb544 100644 --- a/src/main/java/roomescape/Domains/Reservation/ReservationApiController.java +++ b/src/main/java/roomescape/Domains/Reservation/ReservationApiController.java @@ -5,7 +5,6 @@ import java.util.Objects; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; diff --git a/src/main/java/roomescape/Domains/Time/Time.java b/src/main/java/roomescape/Domains/Time/Time.java index eec8b033e..dc1d581d5 100644 --- a/src/main/java/roomescape/Domains/Time/Time.java +++ b/src/main/java/roomescape/Domains/Time/Time.java @@ -11,6 +11,10 @@ public class Time { public Time() { } + public Time(String time) { + this.time = time; + } + @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; diff --git a/src/main/java/roomescape/Domains/Time/TimeApiController.java b/src/main/java/roomescape/Domains/Time/TimeApiController.java index 91269b328..7a03597d3 100644 --- a/src/main/java/roomescape/Domains/Time/TimeApiController.java +++ b/src/main/java/roomescape/Domains/Time/TimeApiController.java @@ -2,10 +2,8 @@ import java.net.URI; import java.util.List; -import java.util.Objects; -import org.springframework.beans.factory.annotation.Autowired; +import java.util.Map; import org.springframework.http.ResponseEntity; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -13,10 +11,43 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import roomescape.Domains.Reservation.Reservation; -import roomescape.Domains.Reservation.ReservationRepository; import roomescape.exceptions.BadRequestException; @RestController public class TimeApiController { + private final TimeRepository timeRepository; + + public TimeApiController(TimeRepository timeRepository) { + this.timeRepository = timeRepository; + } + + @GetMapping("/times") + public ResponseEntity> index() { + List