11package roomescape .controller ;
22
3+ import org .springframework .http .ResponseEntity ;
34import org .springframework .stereotype .Controller ;
5+ import org .springframework .web .bind .annotation .DeleteMapping ;
46import org .springframework .web .bind .annotation .GetMapping ;
7+ import org .springframework .web .bind .annotation .PathVariable ;
8+ import org .springframework .web .bind .annotation .PostMapping ;
59import org .springframework .web .bind .annotation .RequestBody ;
6- import org .springframework .web .bind .annotation .RequestMapping ;
710import org .springframework .web .bind .annotation .ResponseBody ;
811import roomescape .dto .Reservation ;
912
13+ import java .net .URI ;
1014import java .util .ArrayList ;
1115import java .util .List ;
1216import 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}
0 commit comments