-
Notifications
You must be signed in to change notification settings - Fork 165
[4기 - 박형진] SpringBoot Part3 Weekly Mission 두 번째 PR 제출합니다. #851
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: legowww
Are you sure you want to change the base?
Changes from 3 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,69 @@ | ||
| package com.dev.voucherproject.controller.web; | ||
|
|
||
|
|
||
| import com.dev.voucherproject.controller.web.request.VoucherCreateRequest; | ||
| import com.dev.voucherproject.controller.web.response.Response; | ||
| import com.dev.voucherproject.model.service.VoucherService; | ||
| import com.dev.voucherproject.model.voucher.VoucherDto; | ||
| import com.dev.voucherproject.model.voucher.VoucherPolicy; | ||
| import org.springframework.format.annotation.DateTimeFormat; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @RestController | ||
| @RequestMapping(value = "/api/v1/vouchers", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public class VoucherJsonApiController { | ||
| private final VoucherService voucherService; | ||
|
|
||
| public VoucherJsonApiController(VoucherService voucherService) { | ||
| this.voucherService = voucherService; | ||
| } | ||
|
|
||
| @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public Response<Void> create(@RequestBody VoucherCreateRequest voucherCreateRequest) { | ||
| voucherService.insert(voucherCreateRequest); | ||
|
|
||
| return Response.success(null); | ||
|
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. 객체를 생성했을때 200 상태와 null을 리턴하는 방법으로 선택하신 이유가 있나요?
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. @ResponseStatus(HttpStatus.CREATED) 때문에 201 상태 아닌가요 ❓ null을 리턴한 이유는 insert 메서드의 반환타입이 void 인 상태여서 마땅히 반환할 값이 없어서 null 을 사용했는데, |
||
| } | ||
|
|
||
| @GetMapping | ||
| public Response<List<VoucherDto>> vouchers(@RequestParam("policy") Optional<VoucherPolicy> policy) { | ||
| List<VoucherDto> vouchers = policy | ||
| .map(voucherService::findAllVouchersByPolicy) | ||
| .orElse(voucherService.findAllVouchers()) | ||
| .stream() | ||
| .toList(); | ||
|
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. 해당 비즈니스는 컨트롤러가 아니라 서비스에서 처리해주면 좋겠군요~! |
||
|
|
||
| return Response.success(vouchers); | ||
| } | ||
|
|
||
| @GetMapping("/date") | ||
| public Response<List<VoucherDto>> betweenDatesCreatedVouchers( | ||
| @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate, | ||
| @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate | ||
| ) { | ||
|
Comment on lines
+46
to
+51
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. 요기 API는
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. 그렇네요 |
||
| List<VoucherDto> vouchers = voucherService.findAllBetweenDates(startDate, endDate); | ||
|
|
||
| return Response.success(vouchers); | ||
| } | ||
|
Comment on lines
+46
to
+56
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. 조회를 위한 GET Method 사용 시 쿼리스트링이 아닌 메세지 바디에 데이터를 담아서 전송하기도 하나요? 검색해보니 14년 이후부터는 GET Method 에서 메세지 바디에 값을 실는 것이 문제없다고 하는데 HTTP 프로토콜과는 잘 맞지 않는 방식 같아서 사용하기 꺼려지네요 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. 네 request parameter가 크지 않다면 보통 쿼리파라미터로 받습니다.
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. 아니오 없습니다!! 단순 질문이었습니다. |
||
|
|
||
| @GetMapping("/{id}") | ||
| public Response<VoucherDto> voucher(@PathVariable String id) { | ||
| VoucherDto voucher = voucherService.findById(id); | ||
|
|
||
| return Response.success(voucher); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public Response<Void> delete(@PathVariable String id) { | ||
| voucherService.deleteById(id); | ||
|
|
||
| return Response.success(null); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.dev.voucherproject.controller.web; | ||
|
|
||
|
|
||
| import com.dev.voucherproject.controller.web.request.VoucherCreateRequest; | ||
| import com.dev.voucherproject.controller.web.response.Response; | ||
| import com.dev.voucherproject.model.service.VoucherService; | ||
| import com.dev.voucherproject.model.voucher.VoucherDto; | ||
| import com.dev.voucherproject.model.voucher.VoucherPolicy; | ||
| import org.springframework.format.annotation.DateTimeFormat; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @RestController | ||
| @RequestMapping(value = "/api/v1/vouchers", produces = MediaType.APPLICATION_ATOM_XML_VALUE) | ||
| public class VoucherXmlApiController { | ||
| private final VoucherService voucherService; | ||
|
|
||
| public VoucherXmlApiController(VoucherService voucherService) { | ||
| this.voucherService = voucherService; | ||
| } | ||
|
|
||
| @PostMapping(consumes = MediaType.APPLICATION_ATOM_XML_VALUE) | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public Response<Void> create(@RequestBody VoucherCreateRequest voucherCreateRequest) { | ||
| voucherService.insert(voucherCreateRequest); | ||
|
|
||
| return Response.success(null); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public Response<List<VoucherDto>> vouchers(@RequestParam("policy") Optional<VoucherPolicy> policy) { | ||
| List<VoucherDto> vouchers = policy | ||
| .map(voucherService::findAllVouchersByPolicy) | ||
| .orElse(voucherService.findAllVouchers()) | ||
| .stream() | ||
| .toList(); | ||
|
|
||
| return Response.success(vouchers); | ||
| } | ||
|
|
||
| @GetMapping("/date") | ||
| public Response<List<VoucherDto>> betweenDatesCreatedVouchers( | ||
| @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate, | ||
| @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate | ||
| ) { | ||
| List<VoucherDto> vouchers = voucherService.findAllBetweenDates(startDate, endDate); | ||
|
|
||
| return Response.success(vouchers); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public Response<VoucherDto> voucher(@PathVariable String id) { | ||
| VoucherDto voucher = voucherService.findById(id); | ||
|
|
||
| return Response.success(voucher); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public Response<Void> delete(@PathVariable String id) { | ||
| voucherService.deleteById(id); | ||
|
|
||
| return Response.success(null); | ||
| } | ||
| } |
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.
ResponseEntity를 사용해보면 어떨까요?
https://tecoble.techcourse.co.kr/post/2021-05-10-response-entity/