Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

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/

voucherService.insert(voucherCreateRequest);

return Response.success(null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

객체를 생성했을때 200 상태와 null을 리턴하는 방법으로 선택하신 이유가 있나요?
다른 선택지도 있을까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ResponseStatus(HttpStatus.CREATED) 때문에 201 상태 아닌가요 ❓

null을 리턴한 이유는 insert 메서드의 반환타입이 void 인 상태여서 마땅히 반환할 값이 없어서 null 을 사용했는데,
ID 값을 반환하도록 변경해보겠습니다.

}

@GetMapping
public Response<List<VoucherDto>> vouchers(@RequestParam("policy") Optional<VoucherPolicy> policy) {
List<VoucherDto> vouchers = policy
.map(voucherService::findAllVouchersByPolicy)
.orElse(voucherService.findAllVouchers())
.stream()
.toList();

Choose a reason for hiding this comment

The 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
) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요기 API는 /api/v1/vouchers와 동일하게 사용할 수 있을 거 같은데요.
나중에 동적쿼리를 적용해봐도 좋을 거 같아요~!

Copy link
Author

@legowww legowww Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇네요
우선 개인과제 마무리해보고 해보겠습니다 감사합니다~

List<VoucherDto> vouchers = voucherService.findAllBetweenDates(startDate, endDate);

return Response.success(vouchers);
}
Copy link
Author

@legowww legowww Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조회를 위한 GET Method 사용 시 쿼리스트링이 아닌 메세지 바디에 데이터를 담아서 전송하기도 하나요?
(이 메서드의 경우 메세지 바디에 json 타입의 startDate, endDate 데이터 추가)

검색해보니 14년 이후부터는 GET Method 에서 메세지 바디에 값을 실는 것이 문제없다고 하는데 HTTP 프로토콜과는 잘 맞지 않는 방식 같아서 사용하기 꺼려지네요

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 request parameter가 크지 않다면 보통 쿼리파라미터로 받습니다.
바디로 보내실려는 이유가 있을까요?

Copy link
Author

@legowww legowww Jul 20, 2023

Choose a reason for hiding this comment

The 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
import org.springframework.context.annotation.Configuration;

import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOrigins("http://localhost:3000");
}
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
return new HiddenHttpMethodFilter();
Expand Down