-
Notifications
You must be signed in to change notification settings - Fork 193
[그리디] 허석준 Spring MVC 3,4단계 제출합니다 #440
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
Changes from 39 commits
5277a36
3ae6863
6e05245
363baa0
0a84565
0c7afc6
2f99f81
4ceb2a8
698ae7e
e09efa9
e597d1b
d11c3a4
6f40799
16b13ed
9203e52
34444aa
a0685fd
a10ee63
f5d9211
c670ca7
958dd27
ca8c4f1
6f5afca
984c1fa
b98bf11
99190e7
6dd45ef
809e2db
a3d38dd
8e49f4e
5a9fc02
6e028d7
ef07788
967b876
ac439b1
7af7815
05e844e
fa64bd3
e5e2d78
d0f88e9
7001478
cd2ce20
660d181
59454c7
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,51 @@ | ||
| package roomescape.controller; | ||
|
|
||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import roomescape.controller.dto.ReservationRequest; | ||
| import roomescape.controller.dto.ReservationResponse; | ||
| import roomescape.mapper.ReservationMapper; | ||
| import roomescape.service.ReservationService; | ||
| import roomescape.service.dto.ReservationResult; | ||
| import roomescape.service.dto.SaveReservationCommand; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| public class ReservationApiController { | ||
|
|
||
| private final ReservationService reservationService; | ||
| private final ReservationMapper mapper; | ||
|
|
||
| public ReservationApiController(ReservationService reservationService, ReservationMapper mapper) { | ||
| this.reservationService = reservationService; | ||
| this.mapper = mapper; | ||
| } | ||
|
|
||
| @GetMapping("/reservations") | ||
| public ResponseEntity<List<ReservationResponse>> findAll() { | ||
| List<ReservationResult> all = reservationService.findAll(); | ||
| List<ReservationResponse> reservationResponses = all.stream() | ||
| .map(mapper::ResultToResponse) | ||
| .toList(); | ||
| return ResponseEntity.ok(reservationResponses); | ||
| } | ||
|
|
||
| @PostMapping("/reservations") | ||
| public ResponseEntity<ReservationResponse> save(@RequestBody ReservationRequest reservationRequest) { | ||
| SaveReservationCommand command = mapper.requestToCommand(reservationRequest); | ||
| ReservationResult result = reservationService.save(command); | ||
| ReservationResponse reservationResponse = mapper.ResultToResponse(result); | ||
| return ResponseEntity.created(URI.create("/reservations/" + result.id())) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .body(reservationResponse); | ||
| } | ||
|
|
||
| @DeleteMapping("/reservations/{id}") | ||
| public ResponseEntity<Void> delete(@PathVariable Long id) { | ||
| reservationService.delete(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package roomescape.controller.dto; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
|
|
||
| public class ReservationRequest { | ||
| private final String name; | ||
| private final LocalDate date; | ||
| private final LocalTime time; | ||
|
|
||
| public ReservationRequest(String name, LocalDate date, LocalTime time) { | ||
| if (name == null || name.isBlank()) { | ||
| throw new IllegalArgumentException("이름은 필수입니다."); | ||
| } | ||
| if (date == null) { | ||
| throw new IllegalArgumentException("날짜는 필수입니다."); | ||
| } | ||
| if (time == null) { | ||
| throw new IllegalArgumentException("시간은 필수입니다."); | ||
| } | ||
|
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. Controller로 들어오는 요청 데이터를 검증 하는 로직이 추가 되었네요. 간단한 퀴즈 하나 내보겠습니다..! {
"date": "2025-08-05",
"name": null,
"time": "15:40"
}이런 요청이 들어 왔을 때, 기본적으로 jackson 라이브러리를 통해서 json 형태의 텍스트가 저희가 정의한 객체로 만들어지게 됩니다. 퀴즈와는 조금 먼 내용이지만, 참고 하면 좋을 자료
Author
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.
IllegalArgumentException으로 처리될 줄 알았는데, 실제로는 HttpMessageNotReadableException으로 처리되더라고요. 현재는 HttpMessageNotReadableException이 발생할 경우 나중에 로그를 남길 때도 이 부분에 대해서 생각을 하며 로그를 남기면 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. 꼼꼼하게 트래킹 해보셨군요..!👍👍 이처럼 의도치 않게 작동하는 로직이 존재 할 수 있으니, 추후 테스트를 더 보강 하여 이런 문제를 조기에 발견 할 수 있음 좋겠네요ㅎㅎ |
||
|
|
||
| this.name = name; | ||
| this.date = date; | ||
| this.time = time; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public LocalDate getDate() { | ||
| return date; | ||
| } | ||
|
|
||
| public LocalTime getTime() { | ||
| return time; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package roomescape.controller.dto; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
|
|
||
| public record ReservationResponse( | ||
| Long id, | ||
| String name, | ||
| LocalDate date, | ||
| LocalTime time | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,12 +18,12 @@ public Reservation(Long id, String name, LocalDate date, LocalTime time) { | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public Reservation(String name, LocalDate date, LocalTime time) { | ||||||||||||||||||||||||||||||||||||||||||
| validateDate(date); | ||||||||||||||||||||||||||||||||||||||||||
| this.id = null; | ||||||||||||||||||||||||||||||||||||||||||
| this.name = name; | ||||||||||||||||||||||||||||||||||||||||||
| this.date = date; | ||||||||||||||||||||||||||||||||||||||||||
| this.time = time; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
20
to
25
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. 생성 단계에서 검증이 이루어지네요. 현재 Reservation에는 2개의 생성자가 있는데...
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public Reservation withId(Long id) { | ||||||||||||||||||||||||||||||||||||||||||
| return new Reservation(id, this.name, this.date, this.time); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,4 +43,10 @@ public LocalDate getDate() { | |||||||||||||||||||||||||||||||||||||||||
| public LocalTime getTime() { | ||||||||||||||||||||||||||||||||||||||||||
| return time; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private void validateDate(LocalDate date) { | ||||||||||||||||||||||||||||||||||||||||||
| if (date.isBefore(LocalDate.now())) { | ||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("예약 날짜는 오늘 이후여야 합니다."); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package roomescape.exception; | ||
|
|
||
| public record ErrorResult( | ||
| String code, | ||
| String message | ||
| ) {} |

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.
정적 페이지를 제공하는 역할에 맞는 더 적합한 네이밍이네요.
섬세하시군요!👍