1
1
package roomescape .controller ;
2
2
3
+ import org .springframework .http .ResponseEntity ;
3
4
import org .springframework .stereotype .Controller ;
5
+ import org .springframework .web .bind .annotation .DeleteMapping ;
4
6
import org .springframework .web .bind .annotation .GetMapping ;
7
+ import org .springframework .web .bind .annotation .PathVariable ;
8
+ import org .springframework .web .bind .annotation .PostMapping ;
5
9
import org .springframework .web .bind .annotation .RequestBody ;
6
- import org .springframework .web .bind .annotation .RequestMapping ;
7
10
import org .springframework .web .bind .annotation .ResponseBody ;
8
11
import roomescape .dto .Reservation ;
9
12
13
+ import java .net .URI ;
10
14
import java .util .ArrayList ;
11
15
import java .util .List ;
12
16
import java .util .concurrent .atomic .AtomicLong ;
@@ -17,20 +21,46 @@ public class ReservationController {
17
21
private List <Reservation > reservations = new ArrayList <>();
18
22
private AtomicLong index = new AtomicLong (1 );
19
23
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
+ // }
25
29
30
+ // 홈화면
26
31
@ GetMapping ("/reservation" )
27
32
public String reservationPage (){
28
33
return "reservation" ;
29
34
}
30
35
36
+ //예약 조회
31
37
@ ResponseBody
32
38
@ GetMapping ("/reservations" )
33
- public List <Reservation > reservationList (){
39
+ public List <Reservation > list (){
34
40
return reservations ;
35
41
}
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
+ }
36
66
}
0 commit comments