-
Notifications
You must be signed in to change notification settings - Fork 169
[Spring MVC] 진웨이얀 3,4단게 미션제출합니다 #517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: zinyan
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package roomescape; | ||
|
|
||
|
|
||
| public class Reservation{ | ||
| private Long id; | ||
| private String name; | ||
| private String date; | ||
| private String time; | ||
| public Reservation(){ | ||
|
|
||
| } | ||
| public Reservation(Long id,String name,String date,String time){ | ||
| this.id = id; | ||
| this.name = name; | ||
| this.date = date; | ||
| this.time = time; | ||
| } | ||
|
|
||
| public Reservation(String name,String date,String time){ | ||
| this.name = name; | ||
| this.date = date; | ||
| this.time = time; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getDate() { | ||
| return date; | ||
| } | ||
|
|
||
| public String getTime() { | ||
| return time; | ||
| } | ||
|
|
||
| public static Reservation toEntity(Reservation reservation,Long id){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정적 팩토리 메서드는 엔티티와 DTO 가 분리되었을 때 객체 생성을 위해서 사용하는 메서드입니다. 현재는 DTO 만 존재하니 필요하지 않을 것 같습니다~ |
||
| return new Reservation(id,reservation.name,reservation.date,reservation.time); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package roomescape; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| import static org.apache.logging.log4j.util.Strings.isBlank; | ||
|
|
||
| @Controller | ||
| public class ReservationController { | ||
|
|
||
| private final List<Reservation> reservations = new ArrayList<>(); | ||
| private final AtomicLong index =new AtomicLong(1); | ||
|
|
||
| // render | ||
| @GetMapping("/reservation") | ||
| public String reservationPage() { | ||
| return "reservation"; | ||
| } | ||
|
|
||
| // Read | ||
| @GetMapping("/reservations") | ||
| @ResponseBody | ||
| public ResponseEntity<List<Reservation>> list() { | ||
| return ResponseEntity.ok().body(reservations); | ||
| } | ||
|
|
||
| // Create | ||
| @PostMapping("/reservations") | ||
| public ResponseEntity<Reservation> add_reservation(@RequestBody Reservation reservation){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드명이 snake case 로 되어있네요.Java 컨벤션에서 메서드명은 camelCase 을 주로 사용합니다 :) |
||
| // handle exception -> any required field empty | ||
| if(isBlank(reservation.getName()) || isBlank(reservation.getDate()) || isBlank(reservation.getTime())){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 파라미터 입력을 수동으로 검증을 하고 있네요. Valid 어노테이션을 사용해볼까요? 이 글 을 참고하면, DTO 의 필드 입력을 컨트롤러에서 검증하는 Valid 어노테이션 동작을 이해하는 데 도움될 것 같습니다! |
||
| throw new BadRequestReservationException(); | ||
| } | ||
|
|
||
|
|
||
| Reservation newReservation = Reservation.toEntity(reservation,index.getAndIncrement()); | ||
| reservations.add(newReservation); | ||
| return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())).body(newReservation); | ||
| } | ||
|
|
||
| // Delete | ||
| @DeleteMapping("/reservations/{id}") | ||
| public ResponseEntity<Void> cancel_reservation(@PathVariable Long id){ | ||
|
|
||
|
|
||
| Reservation newReservation = reservations.stream() | ||
| .filter(it-> Objects.equals(it.getId(),id)) | ||
| .findFirst() | ||
| //handle exception -> reservation is not found | ||
| .orElseThrow(NotFoundReservationException::new); | ||
| reservations.remove(newReservation); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
|
|
||
| // Exception Handler | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외 클래스는 별도의 별도 패키지로 분리해주세요~! |
||
| public class NotFoundReservationException extends RuntimeException {} | ||
| public class BadRequestReservationException extends RuntimeException {} | ||
| @ExceptionHandler({BadRequestReservationException.class, NotFoundReservationException.class}) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NotFound 에러는 404 코드를 던지는 게 좋을 것 같아요! 현재는 Bad request 로 처리하고 있네요. 별도 메서드로 분리해볼까요? |
||
| public ResponseEntity<Void> handleBadRequest(RuntimeException e){ | ||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
파일 경로를 변경해볼까요? /controller 와 /dto 를 분리하면 좋을 것 같습니다