diff --git a/build.gradle b/build.gradle index 57267157c..738895123 100644 --- a/build.gradle +++ b/build.gradle @@ -13,11 +13,19 @@ repositories { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter' + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-starter-jdbc' + implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation 'jakarta.validation:jakarta.validation-api:3.0.0' + runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.rest-assured:rest-assured:5.3.1' + testImplementation "junit:junit:4.12" } test { useJUnitPlatform() + jvmArgs '-Xshare:off' } diff --git a/src/main/java/roomescape/advice/GlobalControllerAdvice.java b/src/main/java/roomescape/advice/GlobalControllerAdvice.java new file mode 100644 index 000000000..bd2d8d0f7 --- /dev/null +++ b/src/main/java/roomescape/advice/GlobalControllerAdvice.java @@ -0,0 +1,34 @@ +package roomescape.advice; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +import roomescape.exception.DatabaseOperationException; +import roomescape.exception.ReservationNotFoundException; +import roomescape.exception.TimeNotFoundException; + +@ControllerAdvice +public class GlobalControllerAdvice { + @ExceptionHandler(ReservationNotFoundException.class) + public ResponseEntity handleReservationNotFoundException(ReservationNotFoundException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage()); + } + + @ExceptionHandler(DatabaseOperationException.class) + public ResponseEntity handleDatabaseOperationException(DatabaseOperationException ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage()); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleValidationExceptions(MethodArgumentNotValidException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage()); + } + + @ExceptionHandler(TimeNotFoundException.class) + public ResponseEntity handleTimeNotFoundException(TimeNotFoundException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage()); + } +} diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java new file mode 100644 index 000000000..b0b12d315 --- /dev/null +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -0,0 +1,53 @@ +package roomescape.controller; + +import java.net.URI; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import jakarta.validation.Valid; + +import roomescape.domain.Reservation; +import roomescape.service.ReservationService; + +@Controller +public class ReservationController { + @Autowired + private ReservationService reservationService; + + @GetMapping("/reservation") + public String reservation() { + return "new-reservation"; + } + + @GetMapping("/reservations") + @ResponseBody + public ResponseEntity> getReservations(){ + List reservations = reservationService.getAllReservations(); + return ResponseEntity.ok(reservations); + } + + @GetMapping("/reservations/{id}") + @ResponseBody + public ResponseEntity getReservation(@PathVariable long id){ + Reservation reservation = reservationService.getReservationById(id); + return ResponseEntity.ok(reservation); + } + + @PostMapping("/reservations") + @ResponseBody + public ResponseEntity createReservation(@Valid @RequestBody Reservation reservation) { + Reservation newReservation = reservationService.createReservation(reservation); + String uri = "/reservations/" + newReservation.getId(); + return ResponseEntity.created(URI.create(uri)).body(newReservation); + } + + @DeleteMapping("/reservations/{id}") + public ResponseEntity deleteReservation(@PathVariable long id){ + reservationService.deleteReservation(id); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/src/main/java/roomescape/controller/TimeController.java b/src/main/java/roomescape/controller/TimeController.java new file mode 100644 index 000000000..d9965a2a8 --- /dev/null +++ b/src/main/java/roomescape/controller/TimeController.java @@ -0,0 +1,50 @@ +package roomescape.controller; + +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import roomescape.domain.Time; +import roomescape.service.TimeService; + +import java.net.URI; +import java.util.List; + +@Controller +public class TimeController { + @Autowired + private TimeService timeService; + + @GetMapping("/time") + public String time() { + return "time"; + } + + @GetMapping("/times") + public ResponseEntity> getTimes() { + List