Skip to content

Commit 7dfd8c5

Browse files
committed
feat: 예약 추가/취소
- ReservationController속 변수에 값 넣어준 코드 주석처리
1 parent ad9fc14 commit 7dfd8c5

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed
Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package roomescape.controller;
22

3+
import org.springframework.http.ResponseEntity;
34
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.DeleteMapping;
46
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PostMapping;
59
import org.springframework.web.bind.annotation.RequestBody;
6-
import org.springframework.web.bind.annotation.RequestMapping;
710
import org.springframework.web.bind.annotation.ResponseBody;
811
import roomescape.dto.Reservation;
912

13+
import java.net.URI;
1014
import java.util.ArrayList;
1115
import java.util.List;
1216
import java.util.concurrent.atomic.AtomicLong;
@@ -17,20 +21,46 @@ public class ReservationController {
1721
private List<Reservation> reservations = new ArrayList<>();
1822
private AtomicLong index = new AtomicLong(1);
1923

20-
public ReservationController() {
21-
reservations.add(new Reservation(1L, "브라운", "2023-01-01", "10:00"));
22-
reservations.add(new Reservation(2L, "브라운", "2023-01-02", "11:00"));
23-
reservations.add(new Reservation(3L, "브라운", "2023-01-03", "12:00"));
24-
}
24+
// public ReservationController() {
25+
// reservations.add(new Reservation(1L, "브라운1", "2023-01-01", "10:00"));
26+
// reservations.add(new Reservation(2L, "브라운2", "2023-01-02", "11:00"));
27+
// reservations.add(new Reservation(3L, "브라운3", "2023-01-03", "12:00"));
28+
// }
2529

30+
// 홈화면
2631
@GetMapping("/reservation")
2732
public String reservationPage(){
2833
return "reservation";
2934
}
3035

36+
//예약 조회
3137
@ResponseBody
3238
@GetMapping("/reservations")
33-
public List<Reservation> reservationList(){
39+
public List<Reservation> list(){
3440
return reservations;
3541
}
42+
43+
//예약 추가
44+
@ResponseBody
45+
@PostMapping("/reservations")
46+
public ResponseEntity<Reservation> create(@RequestBody Reservation newReservation){
47+
48+
Reservation reservation = new Reservation(index.getAndIncrement(), newReservation.getName(), newReservation.getDate(), newReservation.getTime());
49+
50+
reservations.add(reservation);
51+
return ResponseEntity.created(URI.create("/reservations/" + reservation.getId()))
52+
.body(reservation);
53+
}
54+
55+
//예약 삭제
56+
@DeleteMapping("/reservations/{id}")
57+
public ResponseEntity<Void> delete(@PathVariable Long id){
58+
Reservation reservation = reservations.stream()
59+
.filter(it -> it.getId().equals(id))
60+
.findFirst()
61+
.orElseThrow(() -> new RuntimeException("Reservation not found"));
62+
63+
reservations.remove(reservation);
64+
return ResponseEntity.noContent().build();
65+
}
3666
}

src/main/java/roomescape/dto/Reservation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ public String getDate() {
2929
public String getTime() {
3030
return time;
3131
}
32+
3233
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=8080

src/test/java/roomescape/MissionStepTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package roomescape;
22

33
import io.restassured.RestAssured;
4+
import io.restassured.http.ContentType;
45
import org.junit.jupiter.api.Test;
56
import org.springframework.boot.test.context.SpringBootTest;
67
import org.springframework.test.annotation.DirtiesContext;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
712
import static org.hamcrest.Matchers.is;
813

914
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@@ -32,4 +37,39 @@ public class MissionStepTest {
3237
.body("size()", is(3)); // 아직 생성 요청이 없으니 Controller에서 임의로 넣어준 Reservation 갯수 만큼 검증하거나 0개임을 확인하세요.
3338
}
3439

40+
@Test
41+
void 삼단계() {
42+
Map<String, String> params = new HashMap<>();
43+
params.put("name", "브라운");
44+
params.put("date", "2023-08-05");
45+
params.put("time", "15:40");
46+
47+
RestAssured.given().log().all()
48+
.contentType(ContentType.JSON)
49+
.body(params)
50+
.when().post("/reservations")
51+
.then().log().all()
52+
.statusCode(201)
53+
.header("Location", "/reservations/1")
54+
.body("id", is(1));
55+
56+
RestAssured.given().log().all()
57+
.when().get("/reservations")
58+
.then().log().all()
59+
.statusCode(200)
60+
.body("size()", is(1));
61+
62+
RestAssured.given().log().all()
63+
.when().delete("/reservations/1")
64+
.then().log().all()
65+
.statusCode(204);
66+
67+
RestAssured.given().log().all()
68+
.when().get("/reservations")
69+
.then().log().all()
70+
.statusCode(200)
71+
.body("size()", is(0));
72+
}
73+
74+
3575
}

0 commit comments

Comments
 (0)