Skip to content

[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

Open
wants to merge 4 commits into
base: legowww
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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,73 @@
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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
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)
public ResponseEntity<Response<String>> create(@RequestBody VoucherCreateRequest voucherCreateRequest) {
String id = voucherService.insert(voucherCreateRequest);

return ResponseEntity.created(URI.create("/api/v1/vouchers/"+id))
.body(Response.success(id));
}

@GetMapping
public ResponseEntity<Response<List<VoucherDto>>> vouchers(@RequestParam("voucherPolicy") Optional<VoucherPolicy> voucherPolicy) {
List<VoucherDto> voucherDtos = voucherService.findAllVouchersAppliedQueryString(voucherPolicy);

return ResponseEntity.ok()
.body(Response.success(voucherDtos));
}

@GetMapping("/date")
public ResponseEntity<Response<List<VoucherDto>>> betweenDatesCreatedVouchers(
@RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
) {
List<VoucherDto> voucherDtos = voucherService.findAllBetweenDates(startDate, endDate);

return ResponseEntity.ok()
.body(Response.success(voucherDtos));
}

@GetMapping("/{id}")
public ResponseEntity<Response<VoucherDto>> voucher(@PathVariable String id) {
VoucherDto voucherDtos = voucherService.findById(id);

return ResponseEntity.ok()
.body(Response.success(voucherDtos));
}

@DeleteMapping("/{id}")
public ResponseEntity<Response<String>> delete(@PathVariable String id) {
voucherService.deleteById(id);

return ResponseEntity.ok()
.body(Response.success(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
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)
public ResponseEntity<Response<String>> create(@RequestBody VoucherCreateRequest voucherCreateRequest) {
String id = voucherService.insert(voucherCreateRequest);

return ResponseEntity.created(URI.create("/api/v1/vouchers/"+id))
.body(Response.success(id));
}

@GetMapping
public ResponseEntity<Response<List<VoucherDto>>> vouchers(@RequestParam("voucherPolicy") Optional<VoucherPolicy> voucherPolicy) {
List<VoucherDto> voucherDtos = voucherService.findAllVouchersAppliedQueryString(voucherPolicy);

return ResponseEntity.ok()
.body(Response.success(voucherDtos));
}

@GetMapping("/date")
public ResponseEntity<Response<List<VoucherDto>>> betweenDatesCreatedVouchers(
@RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
) {
List<VoucherDto> voucherDtos = voucherService.findAllBetweenDates(startDate, endDate);

return ResponseEntity.ok()
.body(Response.success(voucherDtos));
}

@GetMapping("/{id}")
public ResponseEntity<Response<VoucherDto>> voucher(@PathVariable String id) {
VoucherDto voucherDtos = voucherService.findById(id);

return ResponseEntity.ok()
.body(Response.success(voucherDtos));
}

@DeleteMapping("/{id}")
public ResponseEntity<Response<String>> delete(@PathVariable String id) {
voucherService.deleteById(id);

return ResponseEntity.ok()
.body(Response.success(id));
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
Expand All @@ -24,14 +25,26 @@ public VoucherService(VoucherDao voucherDao) {
}

@Transactional
public void insert(VoucherCreateRequest voucherCreateRequest) {
public String insert(VoucherCreateRequest voucherCreateRequest) {
UUID uuid = UUID.randomUUID();

Voucher voucher = Voucher.of(
UUID.randomUUID(),
uuid,
LocalDateTime.now(),
voucherCreateRequest.voucherPolicy(),
voucherCreateRequest.discountFigure()
);
voucherDao.insert(voucher);

return uuid.toString();
}

public List<VoucherDto> findAllVouchersAppliedQueryString(Optional<VoucherPolicy> voucherPolicy) {
if (voucherPolicy.isEmpty()) {
return findAllVouchers();
}

return findAllVouchersByPolicy(voucherPolicy.get());
}

public List<VoucherDto> findAllVouchers() {
Expand Down