Skip to content

Commit 4978d05

Browse files
committed
feat : rest API CRUD 기능구현
1 parent 63c7378 commit 4978d05

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherRestController.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package org.prgrms.kdtspringdemo.voucher.controller;
22

33
import org.prgrms.kdtspringdemo.voucher.domain.Voucher;
4+
import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction;
5+
import org.prgrms.kdtspringdemo.voucher.domain.dto.VoucherRequestDto;
46
import org.prgrms.kdtspringdemo.voucher.domain.dto.VoucherViewDto;
57
import org.prgrms.kdtspringdemo.voucher.service.VoucherService;
68
import org.springframework.http.HttpStatus;
79
import org.springframework.http.ResponseEntity;
8-
import org.springframework.web.bind.annotation.GetMapping;
9-
import org.springframework.web.bind.annotation.RequestMapping;
10-
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.web.bind.annotation.*;
1111

1212
import java.util.List;
13+
import java.util.UUID;
1314

1415
@RestController
1516
@RequestMapping("/api/v1/voucher")
@@ -22,10 +23,34 @@ public VoucherRestController(VoucherService voucherService) {
2223

2324
@GetMapping("/vouchers")
2425
ResponseEntity<List<VoucherViewDto>> showAllVouchers() {
25-
final List<VoucherViewDto> vouchers = voucherService.findAll().stream()
26+
List<VoucherViewDto> vouchers = voucherService.findAll().stream()
2627
.map(VoucherViewDto::new).toList();
2728
return new ResponseEntity<>(vouchers, HttpStatus.OK);
29+
}
30+
31+
@PostMapping("/create")
32+
ResponseEntity<Voucher> create(@ModelAttribute VoucherRequestDto voucherRequestDto) {
33+
VoucherTypeFunction voucherTypeFunction = VoucherTypeFunction.findByCode(voucherRequestDto.getVoucherPolicy());
34+
long amount = voucherRequestDto.getAmount();
35+
Voucher voucher = voucherService.createVoucher(voucherTypeFunction, amount);
36+
return new ResponseEntity<>(voucher, HttpStatus.CREATED);
37+
}
38+
39+
@GetMapping("/{voucherId}")
40+
ResponseEntity<Voucher> findById(@PathVariable UUID voucherId) {
41+
Voucher voucher = voucherService.findById(voucherId);
42+
return new ResponseEntity<>(voucher, HttpStatus.OK);
43+
}
44+
45+
@GetMapping("/vouchers/policy")
46+
ResponseEntity<List<Voucher>> findByPolicy(@RequestParam String policy) {
47+
List<Voucher> vouchers = voucherService.findByPolicy(policy);
48+
return new ResponseEntity<>(vouchers, HttpStatus.OK);
49+
}
2850

51+
@DeleteMapping("/{voucherId}")
52+
void deleteById(@PathVariable UUID voucherId) {
53+
voucherService.deleteById(voucherId);
2954
}
3055

3156
}

src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
voucher_file: src/main/resources/csvFiles/voucherList.csv
22
blackList_file: src/main/resources/csvFiles/customer_blacklist.csv
33

4+
server:
5+
port: 8090
6+
47
spring:
58
profiles:
69
active: DB

0 commit comments

Comments
 (0)