- 
                Notifications
    You must be signed in to change notification settings 
- Fork 170
[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); | ||
|          | ||
| } | ||
|  | ||
| @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 | ||
| ) { | ||
| 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는  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); | ||
| } | ||
| 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가 크지 않다면 보통 쿼리파라미터로 받습니다. 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/