Skip to content

Commit e20a305

Browse files
committed
refactor : wallet controller - domain 분리
1 parent a14d001 commit e20a305

File tree

7 files changed

+51
-42
lines changed

7 files changed

+51
-42
lines changed

src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerController.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void insert() {
3434
outputConsole.getCustomerName();
3535
String name = inputConsole.getString();
3636
outputConsole.getCustomerIsBlack();
37-
Boolean isBlack = Boolean.parseBoolean(inputConsole.getString());
37+
boolean isBlack = Boolean.parseBoolean(inputConsole.getString());
3838

3939
customerService.insert(new CustomerRequestDto(name, isBlack));
4040
walletService.create(customerId); // 고객 생성 시 지갑 자동 생성
@@ -46,16 +46,13 @@ public void insert() {
4646
}
4747

4848
public void printAllCustomers() {
49-
List<Customer> customerList = customerService.findAll();
50-
customerList.stream().forEach(customer -> {
51-
CustomerViewDto customerViewDto = new CustomerViewDto(customer);
52-
outputConsole.printCustomer(customerViewDto);
53-
});
49+
customerService.getCustomerViewDtoLists()
50+
.forEach(outputConsole::printCustomer);
5451
}
5552

5653
public void printAllBlackListCustomer() throws IOException {
5754
List<Customer> customerList = customerService.getBlackListCustomers();
58-
customerList.stream().forEach(customer -> {
55+
customerList.forEach(customer -> {
5956
CustomerViewDto customerViewDto = new CustomerViewDto(customer);
6057
outputConsole.printCustomer(customerViewDto);
6158
}); }

src/main/java/org/prgrms/kdtspringdemo/customer/service/CustomerService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.prgrms.kdtspringdemo.customer.domain.Customer;
44
import org.prgrms.kdtspringdemo.dto.CustomerRequestDto;
55
import org.prgrms.kdtspringdemo.customer.repository.CustomerRepository;
6+
import org.prgrms.kdtspringdemo.dto.CustomerViewDto;
67
import org.springframework.stereotype.Service;
78

9+
import java.util.ArrayList;
810
import java.util.List;
911
import java.util.UUID;
1012

@@ -25,6 +27,13 @@ public List<Customer> findAll() {
2527
return customerRepository.findAll();
2628
}
2729

30+
public List<CustomerViewDto> getCustomerViewDtoLists() {
31+
List<Customer> customerList = this.findAll();
32+
List<CustomerViewDto> customerViewDtoList = new ArrayList<>();
33+
customerList.forEach(customer -> customerViewDtoList.add(new CustomerViewDto(customer)));
34+
return customerViewDtoList;
35+
}
36+
2837
public List<Customer> findNoneHaveWalletCustomer() {
2938
return customerRepository.findNotHaveWalletCustomers();
3039
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,13 @@ public void createVoucher() {
4141
voucherService.createVoucher(voucherType, amount);
4242
} catch (NumberFormatException e) {
4343
logger.error("올바른 숫자 형식이 아닙니다.");
44-
} catch (IOException e) {
45-
logger.error(e.getMessage());
46-
} catch (RuntimeException e) {
44+
} catch (IOException | RuntimeException e) {
4745
logger.error(e.getMessage());
4846
}
4947
}
5048

5149
public void showAllVouchers() {
52-
List<Voucher> voucherList = voucherService.findAll();
53-
voucherList.forEach(voucher -> {
54-
VoucherViewDto voucherViewDto = new VoucherViewDto(voucher);
55-
outputConsole.printVoucher(voucherViewDto);
56-
});
50+
voucherService.getVoucherViewDtoList().forEach(outputConsole::printVoucher);
5751
}
5852

5953
public void endVoucherMode() {

src/main/java/org/prgrms/kdtspringdemo/voucher/service/VoucherService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.prgrms.kdtspringdemo.voucher.service;
22

3+
import org.prgrms.kdtspringdemo.dto.VoucherViewDto;
34
import org.prgrms.kdtspringdemo.voucher.domain.Voucher;
45
import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction;
56
import org.prgrms.kdtspringdemo.dto.VoucherRequestDto;
@@ -9,6 +10,7 @@
910
import org.springframework.stereotype.Service;
1011

1112
import java.text.MessageFormat;
13+
import java.util.ArrayList;
1214
import java.util.List;
1315
import java.util.UUID;
1416

@@ -40,6 +42,13 @@ public List<Voucher> findAll() {
4042
return voucherRepository.findAll();
4143
}
4244

45+
public List<VoucherViewDto> getVoucherViewDtoList() {
46+
List<Voucher> voucherList = this.findAll();
47+
List<VoucherViewDto> voucherViewDtoList = new ArrayList<>();
48+
voucherList.forEach(voucher -> voucherViewDtoList.add(new VoucherViewDto(voucher)));
49+
return voucherViewDtoList;
50+
}
51+
4352
public List<Voucher> findByPolicy(String policy) {
4453
return voucherRepository.findByPolicy(policy);
4554
}

src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletController.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ public void printVouchersByCustomerId() {
3030
try {
3131
outputConsole.getCustomerId();
3232
UUID customerId = UUID.fromString(inputConsole.getString());
33-
List<Voucher> vouchers = walletService.findVouchersById(customerId);
34-
vouchers.stream().forEach(voucher -> {
35-
VoucherViewDto voucherViewDto = new VoucherViewDto(voucher);
36-
outputConsole.printVoucher(voucherViewDto);
37-
});
33+
List<VoucherViewDto> vouchers = walletService.findVouchersById(customerId);
34+
vouchers.forEach(outputConsole::printVoucher);
3835
} catch (IOException e) {
3936
logger.error(e.getMessage());
4037
}
@@ -67,11 +64,7 @@ public void deleteVoucherByVoucherId() {
6764
}
6865

6966
public void printAllWallet() {
70-
List<Wallet> walletList = walletService.findAll();
71-
walletList.stream().forEach(wallet -> {
72-
WalletViewDto walletViewDto = new WalletViewDto(wallet);
73-
outputConsole.printWallet(walletViewDto);
74-
});
67+
walletService.getWalletViewDtoList().forEach(outputConsole::printWallet);
7568
}
7669

7770
public void endWalletMode() {

src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletWebController.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,19 @@ public WalletWebController(WalletService walletService, VoucherService voucherSe
2828

2929
@GetMapping
3030
public String listWallets(Model model) {
31-
List<Wallet> walletList = walletService.findAll();
32-
model.addAttribute("walletList", walletList);
31+
List<WalletViewDto> walletViewDtoList = walletService.getWalletViewDtoList();
32+
model.addAttribute("walletList", walletViewDtoList);
3333
return "wallet";
3434
}
3535

3636
@GetMapping("/{walletId}")
3737
public String viewWallet(@PathVariable UUID walletId, Model model) {
38-
Wallet wallet = walletService.findById(walletId).orElse(null);
39-
List<Voucher> vouchers = walletService.findVouchersById(wallet.getCustomerId());
40-
List<VoucherViewDto> voucherViewDtos = new ArrayList<>();
41-
vouchers.stream().forEach(voucher -> voucherViewDtos.add(new VoucherViewDto(voucher)));
42-
43-
WalletViewDto walletDetailsDto = new WalletViewDto(walletId, wallet.getCustomerId(), voucherViewDtos);
44-
38+
WalletViewDto walletViewDto = walletService.findById(walletId).orElseThrow(NoSuchFieldError::new);
39+
List<VoucherViewDto> voucherViewDtoList = walletService.findVouchersById(walletViewDto.getCustomerId());
40+
walletViewDto.setVoucherList(voucherViewDtoList);
4541
List<Voucher> unallocatedVouchers = voucherService.findUnallocatedVoucher();
4642

47-
model.addAttribute("wallet", walletDetailsDto);
43+
model.addAttribute("wallet", walletViewDto);
4844
model.addAttribute("voucherList", unallocatedVouchers);
4945
model.addAttribute("addVoucherToWalletDto", new AddVoucherToWalletDto());
5046
return "wallet_details";
@@ -57,11 +53,11 @@ public String addVoucherToWallet(
5753

5854
UUID selectedVoucherId = addVoucherToWalletDto.getSelectedVoucherId();
5955
if (selectedVoucherId != null) {
60-
Wallet wallet = walletService.findById(walletId).orElse(null);
56+
WalletViewDto walletViewDto = walletService.findById(walletId).orElse(null);
6157
Voucher selectedVoucher = voucherService.findById(selectedVoucherId);
6258

63-
if (wallet != null && selectedVoucher != null) {
64-
walletService.addVoucherByCustomerId(wallet.getWalletId(), wallet.getCustomerId(), selectedVoucherId);
59+
if (walletViewDto != null && selectedVoucher != null) {
60+
walletService.addVoucherByCustomerId(walletViewDto.getWalletId(), walletViewDto.getCustomerId(), selectedVoucherId);
6561
}
6662
}
6763

src/main/java/org/prgrms/kdtspringdemo/wallet/service/WalletService.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package org.prgrms.kdtspringdemo.wallet.service;
22

3+
import org.prgrms.kdtspringdemo.dto.VoucherViewDto;
4+
import org.prgrms.kdtspringdemo.dto.WalletViewDto;
35
import org.prgrms.kdtspringdemo.voucher.domain.Voucher;
46
import org.prgrms.kdtspringdemo.wallet.domain.Wallet;
57
import org.prgrms.kdtspringdemo.wallet.repository.WalletRepository;
68
import org.springframework.stereotype.Service;
79

10+
import java.util.ArrayList;
811
import java.util.Optional;
912
import java.util.UUID;
1013
import java.util.List;
@@ -22,12 +25,14 @@ public Wallet create(UUID customerId) {
2225
return walletRepository.insert(wallet);
2326
}
2427

25-
public Optional<Wallet> findById(UUID walletId){
26-
return walletRepository.findById(walletId);
28+
public Optional<WalletViewDto> findById(UUID walletId){
29+
return Optional.of(new WalletViewDto(walletRepository.findById(walletId).get()));
2730
}
2831

29-
public List<Voucher> findVouchersById(UUID customerId) {
30-
return walletRepository.findVouchersByCustomerId(customerId);
32+
public List<VoucherViewDto> findVouchersById(UUID customerId) {
33+
List<VoucherViewDto> voucherViewDtoList = new ArrayList<>();
34+
walletRepository.findVouchersByCustomerId(customerId).forEach(voucher -> voucherViewDtoList.add(new VoucherViewDto(voucher)));
35+
return voucherViewDtoList;
3136
}
3237

3338
public void addVoucherByCustomerId(UUID walletId, UUID customerId, UUID voucherId) {
@@ -41,6 +46,12 @@ public void deleteVoucherByVoucherId(UUID customerId, UUID voucherId) {
4146
public List<Wallet> findAll() {
4247
return walletRepository.findAll();
4348
}
49+
public List<WalletViewDto> getWalletViewDtoList() {
50+
List<Wallet> walletList = this.findAll();
51+
List<WalletViewDto> walletViewDtoList = new ArrayList<>();
52+
walletList.forEach(wallet -> walletViewDtoList.add(new WalletViewDto(wallet)));
53+
return walletViewDtoList;
54+
}
4455

4556
public void deleteById(UUID walletId) {
4657
walletRepository.deleteById(walletId);

0 commit comments

Comments
 (0)