11package org .prgrms .kdtspringdemo .voucher .controller ;
22
33import org .prgrms .kdtspringdemo .voucher .domain .Voucher ;
4+ import org .prgrms .kdtspringdemo .voucher .domain .VoucherTypeFunction ;
5+ import org .prgrms .kdtspringdemo .voucher .domain .dto .VoucherRequestDto ;
46import org .prgrms .kdtspringdemo .voucher .domain .dto .VoucherViewDto ;
57import org .prgrms .kdtspringdemo .voucher .service .VoucherService ;
68import org .springframework .http .HttpStatus ;
79import 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
1212import 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}
0 commit comments