-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathVoucherController.java
More file actions
89 lines (78 loc) · 3.68 KB
/
VoucherController.java
File metadata and controls
89 lines (78 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.prgms.kdtspringweek1.controller.consoleController;
import org.prgms.kdtspringweek1.console.ConsoleOutput;
import org.prgms.kdtspringweek1.controller.consoleController.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.voucher.service.dto.CreateVoucherRequestDto;
import org.prgms.kdtspringweek1.voucher.service.dto.FindVoucherResponseDto;
import org.prgms.kdtspringweek1.voucher.service.VoucherService;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
public class VoucherController {
private final ConsoleInputConverter consoleInputConverter;
private final ConsoleOutput consoleOutput;
private final VoucherService voucherService;
public VoucherController(ConsoleInputConverter consoleInputConverter, ConsoleOutput consoleOutput, VoucherService voucherService) {
this.consoleInputConverter = consoleInputConverter;
this.consoleOutput = consoleOutput;
this.voucherService = voucherService;
}
public void selectVoucherFunction(SelectFunctionTypeDto functionTypeDto) {
switch (functionTypeDto) {
case CREATE_VOUCHER -> createVoucher();
case LIST_VOUCHERS -> getAllVouchers();
case SEARCH_VOUCHER -> getVoucherById();
case UPDATE_VOUCHER -> updateVoucherInfo();
case DELETE_ALL_VOUCHERS -> deleteAllVouchers();
case DELETE_VOUCHER -> deleteVoucher();
}
}
private void createVoucher() {
consoleOutput.printVouchersToSelect();
String voucherType = consoleInputConverter.getVoucherTypeString();
consoleOutput.printRequestMessageforDiscountValue();
long discountValue = consoleInputConverter.getDiscountValue();
voucherService.registerVoucher(new CreateVoucherRequestDto(discountValue, voucherType).toVoucher());
consoleOutput.printSuccessToCreate();
}
private void getAllVouchers() {
voucherService.searchAllVouchers()
.forEach(FindVoucherResponseDto::printVoucherInfo);
consoleOutput.printSuccessToSearch();
}
private void getVoucherById() {
consoleOutput.printRequestMessageForVoucherId();
voucherService.searchVoucherById(consoleInputConverter.getId())
.ifPresentOrElse(
findVoucherResponseDto -> {
findVoucherResponseDto.printVoucherInfo();
consoleOutput.printSuccessToSearch();
},
consoleOutput::printValueNotFound
);
}
private void updateVoucherInfo() {
consoleOutput.printRequestMessageForVoucherId();
UUID voucherId = consoleInputConverter.getId();
consoleOutput.printVouchersToSelect();
switch (consoleInputConverter.getVoucherType()) {
case FIXED_AMOUNT -> {
consoleOutput.printRequestMessageforDiscountValue();
voucherService.update(consoleInputConverter.getUpdateVoucherRequestDto(voucherId).toFixedAmountVoucher());
}
case PERCENT_DISCOUNT -> {
consoleOutput.printRequestMessageforDiscountValue();
voucherService.update(consoleInputConverter.getUpdateVoucherRequestDto(voucherId).toPercentDiscountVoucher());
}
}
consoleOutput.printSuccessToUpdate();
}
private void deleteVoucher() {
consoleOutput.printRequestMessageForVoucherId();
voucherService.deleteVoucherById(consoleInputConverter.getId());
consoleOutput.printSuccessToDelete();
}
private void deleteAllVouchers() {
voucherService.deleteAllVouchers();
consoleOutput.printSuccessToDelete();
}
}