diff --git a/pom.xml b/pom.xml index 45ccfbc37f..89c159e605 100644 --- a/pom.xml +++ b/pom.xml @@ -22,11 +22,22 @@ spring-boot-starter + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot spring-boot-starter-jdbc + + org.springframework.boot + spring-boot-starter-thymeleaf + + org.springframework.boot spring-boot-starter-test @@ -68,3 +79,4 @@ + diff --git a/src/main/java/org/prgrms/kdtspringdemo/AppController.java b/src/main/java/org/prgrms/kdtspringdemo/AppController.java index 19c33163e5..824c815d7b 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/AppController.java +++ b/src/main/java/org/prgrms/kdtspringdemo/AppController.java @@ -1,42 +1,69 @@ package org.prgrms.kdtspringdemo; +import org.prgrms.kdtspringdemo.customer.CustomerFunction; import org.prgrms.kdtspringdemo.customer.controller.CustomerController; import org.prgrms.kdtspringdemo.view.InputConsole; import org.prgrms.kdtspringdemo.view.OutputConsole; import org.prgrms.kdtspringdemo.voucher.VoucherFunction; import org.prgrms.kdtspringdemo.voucher.controller.VoucherController; +import org.prgrms.kdtspringdemo.wallet.WalletFunction; +import org.prgrms.kdtspringdemo.wallet.controller.WalletController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Controller; import java.io.IOException; @Controller +@Profile("dev") public class AppController implements CommandLineRunner { private final InputConsole inputConsole; private final OutputConsole outputConsole; private final VoucherController voucherController; private final CustomerController customerController; + private final WalletController walletController; private final Logger logger = LoggerFactory.getLogger(KdtSpringDemoApplication.class); - public AppController(InputConsole inputConsole, OutputConsole outputConsole, VoucherController voucherController, CustomerController customerController) { + public AppController(InputConsole inputConsole, OutputConsole outputConsole, VoucherController voucherController, CustomerController customerController, WalletController walletController) { this.inputConsole = inputConsole; this.outputConsole = outputConsole; this.voucherController = voucherController; this.customerController = customerController; + this.walletController = walletController; } @Override public void run(String... args) throws IOException { try { while (true) { - outputConsole.start(); + outputConsole.startProgram(); String fun = inputConsole.getString(); try { - VoucherFunction.findByCode(fun).execute(voucherController); + // Mode 선택 + ProgramFunction mode = ProgramFunction.findByCode(fun); + mode.execute(outputConsole); + switch (mode) { + case VOUCHER: + fun = inputConsole.getString(); //voucher 실행 + VoucherFunction.findByCode(fun).execute(voucherController); + break; + case CUSTOMER: + fun = inputConsole.getString(); + CustomerFunction.findByCode(fun).execute(customerController); + break; + case WALLET: + fun = inputConsole.getString(); + WalletFunction.findByCode(fun).execute(walletController); + break; + case EXIT: + ProgramFunction.findByCode(fun).execute(outputConsole); + System.exit(0); + break; + } } catch (Exception e) { - System.out.println(e.getMessage()); + logger.error(e.getMessage()); } } } catch (Exception e) { diff --git a/src/main/java/org/prgrms/kdtspringdemo/ProgramFunction.java b/src/main/java/org/prgrms/kdtspringdemo/ProgramFunction.java new file mode 100644 index 0000000000..aba5fa63c6 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/ProgramFunction.java @@ -0,0 +1,41 @@ +package org.prgrms.kdtspringdemo; + +import org.prgrms.kdtspringdemo.view.OutputConsole; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.NoSuchElementException; +import java.util.function.Consumer; + +public enum ProgramFunction { + VOUCHER("voucher", "바우처 모드", OutputConsole::startVoucherMode), + CUSTOMER("customer", "고객 모드", OutputConsole::startCustomerMode), + WALLET("wallet", "지갑 모드", OutputConsole::startWalletMode), + EXIT("exit", "프로그램을 종료합니다.", OutputConsole::printProgramEnd); + private final String fun; + private final String description; + private final Consumer outputConsoleConsumer; + private final static Logger logger = LoggerFactory.getLogger(ProgramFunction.class); + + ProgramFunction(String fun, String description, Consumer outputConsoleConsumer) { + this.fun = fun; + this.description = description; + this.outputConsoleConsumer = outputConsoleConsumer; + } + + public static ProgramFunction findByCode(String fun) { + String lowerFun = fun.toLowerCase(); + return Arrays.stream(values()) + .filter(option -> option.fun.equals(lowerFun)) + .findFirst() + .orElseThrow(() -> { + logger.error("해당 명령어가 존재하지 않습니다."); + return new NoSuchElementException("해당 명령어가 존재하지 않습니다."); + }); + } + + public void execute(OutputConsole outputConsole) { + this.outputConsoleConsumer.accept(outputConsole); + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/CustomerFunction.java b/src/main/java/org/prgrms/kdtspringdemo/customer/CustomerFunction.java new file mode 100644 index 0000000000..903693f093 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/CustomerFunction.java @@ -0,0 +1,41 @@ +package org.prgrms.kdtspringdemo.customer; + +import org.prgrms.kdtspringdemo.customer.controller.CustomerController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.NoSuchElementException; +import java.util.function.Consumer; + +public enum CustomerFunction { + CREATE("create", "고객 등록", CustomerController::insert), + LIST_ALL_CUSTOMERS("list", "고객 목록", CustomerController::printAllCustomers), + EXIT("exit", "voucher mode 종료", CustomerController::endCustomerMode); + + private final String fun; + private final String description; + private final Consumer customerControllerConsumer; + private final static Logger logger = LoggerFactory.getLogger(CustomerFunction.class); + + CustomerFunction(String fun, String description, Consumer customerControllerConsumer) { + this.fun = fun; + this.description = description; + this.customerControllerConsumer = customerControllerConsumer; + } + + public static CustomerFunction findByCode(String fun) { + String lowerFun = fun.toLowerCase(); + return Arrays.stream(values()) + .filter(option -> option.fun.equals(lowerFun)) + .findFirst() + .orElseThrow(() -> { + logger.error("해당 명령어가 존재하지 않습니다."); + return new NoSuchElementException("해당 명령어가 존재하지 않습니다."); + }); + } + + public void execute(CustomerController customerController) { + this.customerControllerConsumer.accept(customerController); + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerController.java b/src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerController.java index c257465bc8..c470b3a1b4 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerController.java +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerController.java @@ -1,8 +1,13 @@ package org.prgrms.kdtspringdemo.customer.controller; -import org.prgrms.kdtspringdemo.customer.domain.Customer; import org.prgrms.kdtspringdemo.customer.service.CustomerService; +import org.prgrms.kdtspringdemo.dto.CustomerRequestDto; +import org.prgrms.kdtspringdemo.dto.CustomerViewDto; +import org.prgrms.kdtspringdemo.view.InputConsole; import org.prgrms.kdtspringdemo.view.OutputConsole; +import org.prgrms.kdtspringdemo.wallet.service.WalletService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import java.io.IOException; @@ -12,14 +17,43 @@ @Controller public class CustomerController { private final CustomerService customerService; + private final WalletService walletService; + private final InputConsole inputConsole = new InputConsole(); private final OutputConsole outputConsole = new OutputConsole(); + private final Logger logger = LoggerFactory.getLogger(CustomerController.class); - public CustomerController(CustomerService customerService) { + public CustomerController(CustomerService customerService, WalletService walletService) { this.customerService = customerService; + this.walletService = walletService; } - public void printAllBlackListCustomer() throws IOException { - List customerList = customerService.getBlackListCustomers(); - customerList.stream().forEach(customer -> outputConsole.printCustomer(customer)); + public void insert() { + try { + UUID customerId = UUID.randomUUID(); + outputConsole.getCustomerName(); + String name = inputConsole.getString(); + outputConsole.getCustomerIsBlack(); + boolean isBlack = Boolean.parseBoolean(inputConsole.getString()); + + customerService.insert(new CustomerRequestDto(name, isBlack)); + walletService.create(customerId); // 고객 생성 시 지갑 자동 생성 + } catch (IOException e) { + logger.error(e.getMessage()); + } catch (IllegalArgumentException e) { + logger.error("유효한 UUID 값이 아닙니다."); + } + } + + public void printAllCustomers() { + customerService.getCustomerViewDtoLists().forEach(outputConsole::printCustomer); + } + + public void printAllBlackListCustomer() { + List customerList = customerService.getBlackListCustomers(); + customerList.forEach(outputConsole::printCustomer); + } + + public void endCustomerMode() { + outputConsole.printCustomerModeEnd(); } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerWebController.java b/src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerWebController.java new file mode 100644 index 0000000000..faee73b80a --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/controller/CustomerWebController.java @@ -0,0 +1,58 @@ +package org.prgrms.kdtspringdemo.customer.controller; + +import org.prgrms.kdtspringdemo.customer.domain.Customer; +import org.prgrms.kdtspringdemo.customer.service.CustomerService; +import org.prgrms.kdtspringdemo.dto.CustomerRequestDto; +import org.prgrms.kdtspringdemo.dto.CustomerViewDto; +import org.prgrms.kdtspringdemo.wallet.service.WalletService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Controller +@RequestMapping("/customers") +public class CustomerWebController { + private final CustomerService customerService; + private final WalletService walletService; + + public CustomerWebController(CustomerService customerService, WalletService walletService) { + this.customerService = customerService; + this.walletService = walletService; + } + + @GetMapping + public String getAllCustomers(Model model) { + List customerList = customerService.findAll(); + List customerViewDtos = new ArrayList<>(); + customerList.forEach(customer -> customerViewDtos.add(new CustomerViewDto(customer))); + List noneHaveWalletCustomers = customerService.findNoneHaveWalletCustomer(); + + model.addAttribute("customerList", customerViewDtos); + model.addAttribute("customers", noneHaveWalletCustomers); + + return "customer"; + } + + @PostMapping("/create") + public String createCustomer(@ModelAttribute CustomerRequestDto customerRequestDto) { + CustomerViewDto customer = customerService.insert(customerRequestDto); + if (customer != null) walletService.create(customer.getCustomerId()); + return "redirect:/customers"; + } + + @GetMapping("/{customerId}/createWallet") + public String createWalletForCustomer(@PathVariable UUID customerId) { + walletService.create(customerId); + return "redirect:/customers"; + } + + @GetMapping("/{customerId}/delete") + public String deleteVoucher(@PathVariable UUID customerId) { + customerService.deleteById(customerId); + return "redirect:/customers"; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/domain/Customer.java b/src/main/java/org/prgrms/kdtspringdemo/customer/domain/Customer.java index 3f8480a9d2..62a874bfed 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/customer/domain/Customer.java +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/domain/Customer.java @@ -24,4 +24,12 @@ public String getName() { public boolean isBlack() { return isBlack; } + + @Override + public String toString() { + return "=======================\n"+ + "customerId : " + customerId + "\n" + + "customer name : " + name + "\n" + + "isBlack : " + isBlack + "\n"; + } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/repository/CustomerRepository.java b/src/main/java/org/prgrms/kdtspringdemo/customer/repository/CustomerRepository.java index 61ba3be405..88feba4e4a 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/customer/repository/CustomerRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/repository/CustomerRepository.java @@ -5,7 +5,13 @@ import java.io.IOException; import java.util.List; import java.util.Optional; +import java.util.UUID; public interface CustomerRepository { - Optional> getAllBlackList() throws IOException; + Customer insert(Customer customer); + void deleteAll(); + void deleteById(UUID customerId); + List findAll(); + List findNotHaveWalletCustomers(); + List getAllBlackList(); } diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/repository/FileCustomerRepository.java b/src/main/java/org/prgrms/kdtspringdemo/customer/repository/FileCustomerRepository.java index 8595e1d6b0..4efa0c6702 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/customer/repository/FileCustomerRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/repository/FileCustomerRepository.java @@ -3,6 +3,8 @@ import org.apache.commons.csv.CSVRecord; import org.prgrms.kdtspringdemo.customer.domain.Customer; import org.prgrms.kdtspringdemo.file.CsvFileHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; @@ -19,26 +21,53 @@ public class FileCustomerRepository implements CustomerRepository{ private CsvFileHandler csvFileHandler; @Value("${blackList_file}") private String blackListFilePath; + private final Logger logger = LoggerFactory.getLogger(FileCustomerRepository.class); public FileCustomerRepository() { this.csvFileHandler = new CsvFileHandler(); } @Override - public Optional> getAllBlackList() throws IOException { + public Customer insert(Customer customer) { + return null; + } + + @Override + public void deleteAll() { + } + + @Override + public void deleteById(UUID customerId) { + } + + @Override + public List findAll() { + return null; + } + + @Override + public List findNotHaveWalletCustomers() { + return null; + } + + @Override + public List getAllBlackList() { List customerList = new ArrayList<>(); + try { + List data = csvFileHandler.readCSV(blackListFilePath); + data.stream() + .filter(line -> line.get("isBlack").equals("true")) + .forEach(line -> { + UUID customerId = UUID.fromString(line.get("customerId")); + String name = line.get("name"); + boolean isBlack = true; - List data = csvFileHandler.readCSV(blackListFilePath); - data.stream() - .filter(line -> line.get("isBlack").equals("true")) - .forEach(line -> { - UUID customerId = UUID.fromString(line.get("customerId")); - String name = line.get("name"); - boolean isBlack = true; - - Customer customer = new Customer(customerId, name, isBlack); - customerList.add(customer); - }); - return Optional.of(customerList); + Customer customer = new Customer(customerId, name, isBlack); + customerList.add(customer); + }); + } catch (IOException e) { + logger.error(e.getMessage()); + } + return customerList; } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepository.java b/src/main/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepository.java index 891d231242..fe4c530e97 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepository.java @@ -10,10 +10,8 @@ import org.springframework.stereotype.Repository; import javax.sql.DataSource; -import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; -import java.util.Optional; import java.util.UUID; @Repository @@ -39,23 +37,45 @@ public JdbcCustomerRepository(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } + @Override public Customer insert(Customer customer) { var update = jdbcTemplate.update("INSERT INTO customers(customer_id, name, is_black) VALUES (UUID_TO_BIN(?), ?, ?)", customer.getCustomerId().toString().getBytes(), customer.getName(), customer.isBlack()); if(update != 1) { + logger.info("Nothing was inserted"); throw new RuntimeException("Nothing was inserted"); } return customer; } + @Override + public List findAll() { + return jdbcTemplate.query("select * from customers", customerRowMapper); + } + + @Override + public List findNotHaveWalletCustomers() { + return jdbcTemplate.query("SELECT c.customer_id, c.name, c.is_black FROM customers c LEFT JOIN wallet w ON c.customer_id = w.customer_id WHERE w.customer_id IS NULL", + customerRowMapper); + } + + @Override public void deleteAll() { jdbcTemplate.update("DELETE FROM customers"); } @Override - public Optional> getAllBlackList() throws IOException { - return Optional.of(jdbcTemplate.query("select * from customers where is_black = ?", customerRowMapper,true)); + public void deleteById(UUID customerId) { + jdbcTemplate.update("DELETE FROM customers where customer_id = UUID_TO_BIN(?)", + customerId.toString()); + jdbcTemplate.update("DELETE FROM wallet where customer_id = UUID_TO_BIN(?)", + customerId.toString()); + } + + @Override + public List getAllBlackList() { + return jdbcTemplate.query("select * from customers where is_black = ?", customerRowMapper,true); } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/customer/service/CustomerService.java b/src/main/java/org/prgrms/kdtspringdemo/customer/service/CustomerService.java index 741cf5d2ca..583c64701a 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/customer/service/CustomerService.java +++ b/src/main/java/org/prgrms/kdtspringdemo/customer/service/CustomerService.java @@ -2,21 +2,44 @@ import org.prgrms.kdtspringdemo.customer.domain.Customer; import org.prgrms.kdtspringdemo.customer.repository.CustomerRepository; -import org.prgrms.kdtspringdemo.customer.repository.FileCustomerRepository; +import org.prgrms.kdtspringdemo.dto.CustomerRequestDto; +import org.prgrms.kdtspringdemo.dto.CustomerViewDto; import org.springframework.stereotype.Service; -import java.io.IOException; import java.util.List; +import java.util.UUID; @Service public class CustomerService { private final CustomerRepository customerRepository; - public CustomerService() { - this.customerRepository = new FileCustomerRepository(); + public CustomerService(CustomerRepository customerRepository) { + this.customerRepository = customerRepository; } - public List getBlackListCustomers() throws IOException { - return customerRepository.getAllBlackList().get(); + public CustomerViewDto insert(CustomerRequestDto customerRequestDto) { + Customer customer = customerRepository.insert(new Customer(UUID.randomUUID(), customerRequestDto.getName(), customerRequestDto.isBlack())); + return new CustomerViewDto(customer); + } + + public List findAll() { + return customerRepository.findAll(); + } + + public List getCustomerViewDtoLists() { + List customerList = this.findAll(); + return customerList.stream().map(CustomerViewDto::new).toList(); + } + + public List findNoneHaveWalletCustomer() { + return customerRepository.findNotHaveWalletCustomers().stream().map(CustomerViewDto::new).toList(); + } + + public List getBlackListCustomers() { + return customerRepository.getAllBlackList().stream().map(CustomerViewDto::new).toList(); + } + + public void deleteById(UUID customerId) { + customerRepository.deleteById(customerId); } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/dto/AddVoucherToWalletDto.java b/src/main/java/org/prgrms/kdtspringdemo/dto/AddVoucherToWalletDto.java new file mode 100644 index 0000000000..6134b625ac --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/dto/AddVoucherToWalletDto.java @@ -0,0 +1,33 @@ +package org.prgrms.kdtspringdemo.dto; + +import java.util.UUID; + +public class AddVoucherToWalletDto { + + private UUID walletId; + private UUID selectedVoucherId; + + public AddVoucherToWalletDto() { + } + + public AddVoucherToWalletDto(UUID walletId, UUID selectedVoucherId) { + this.walletId = walletId; + this.selectedVoucherId = selectedVoucherId; + } + + public UUID getWalletId() { + return walletId; + } + + public UUID getSelectedVoucherId() { + return selectedVoucherId; + } + + public void setWalletId(UUID walletId) { + this.walletId = walletId; + } + + public void setSelectedVoucherId(UUID selectedVoucherId) { + this.selectedVoucherId = selectedVoucherId; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/dto/CustomerRequestDto.java b/src/main/java/org/prgrms/kdtspringdemo/dto/CustomerRequestDto.java new file mode 100644 index 0000000000..efc656a615 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/dto/CustomerRequestDto.java @@ -0,0 +1,24 @@ +package org.prgrms.kdtspringdemo.dto; + +public class CustomerRequestDto { + private String name; + private boolean isBlack; + + public CustomerRequestDto(String name, boolean isBlack) { + this.name = name; + this.isBlack = isBlack; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public boolean isBlack() { + return isBlack; + } + public void setBlack(boolean black) { + isBlack = black; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/dto/CustomerViewDto.java b/src/main/java/org/prgrms/kdtspringdemo/dto/CustomerViewDto.java new file mode 100644 index 0000000000..44ce6eb3ba --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/dto/CustomerViewDto.java @@ -0,0 +1,49 @@ +package org.prgrms.kdtspringdemo.dto; + +import org.prgrms.kdtspringdemo.customer.domain.Customer; + +import java.util.UUID; + +public class CustomerViewDto { + private UUID customerId; + private String name; + private boolean isBlack; + + public CustomerViewDto(UUID customerId, String name, boolean isBlack) { + this.customerId = customerId; + this.name = name; + this.isBlack = isBlack; + } + + public CustomerViewDto(Customer customer) { + this.customerId = customer.getCustomerId(); + this.name = customer.getName(); + this.isBlack = customer.isBlack(); + } + + @Override + public String toString() { + return "customerId =" + customerId + + " / name ='" + name + '\'' + + " / isBlack =" + isBlack; + } + + public UUID getCustomerId() { + return customerId; + } + public void setCustomerId(UUID customerId) { + this.customerId = customerId; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public boolean isBlack() { + return isBlack; + } + public void setBlack(boolean black) { + isBlack = black; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/dto/VoucherRequestDto.java b/src/main/java/org/prgrms/kdtspringdemo/dto/VoucherRequestDto.java new file mode 100644 index 0000000000..4d4b2d58b1 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/dto/VoucherRequestDto.java @@ -0,0 +1,27 @@ +package org.prgrms.kdtspringdemo.dto; + +public class VoucherRequestDto { + private String voucherPolicy; + private long amount; + + public VoucherRequestDto(String voucherPolicy, long amount) { + this.voucherPolicy = voucherPolicy; + this.amount = amount; + } + + public String getVoucherPolicy() { + return voucherPolicy; + } + + public void setVoucherPolicy(String voucherPolicy) { + this.voucherPolicy = voucherPolicy; + } + + public Long getAmount() { + return amount; + } + + public void setAmount(Long amount) { + this.amount = amount; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/dto/VoucherViewDto.java b/src/main/java/org/prgrms/kdtspringdemo/dto/VoucherViewDto.java new file mode 100644 index 0000000000..3ba7e7bcd4 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/dto/VoucherViewDto.java @@ -0,0 +1,54 @@ +package org.prgrms.kdtspringdemo.dto; + +import org.prgrms.kdtspringdemo.voucher.domain.Voucher; + +import java.util.UUID; + +public class VoucherViewDto { + private UUID voucherId; + private String voucherPolicy; + private Long amount; + + public VoucherViewDto(UUID voucherId, String voucherPolicy, Long amount) { + this.voucherId = voucherId; + this.voucherPolicy = voucherPolicy; + this.amount = amount; + } + + public VoucherViewDto(Voucher voucher) { + this.voucherId = voucher.getVoucherId(); + this.voucherPolicy = voucher.getVoucherPolicy().getVoucherType(); + this.amount = voucher.getVoucherPolicy().getAmount(); + } + + @Override + public String toString() { + return "voucherId =" + voucherId + + " / voucherPolicy ='" + voucherPolicy + '\'' + + " / amount =" + amount; + } + + public UUID getVoucherId() { + return voucherId; + } + + public void setVoucherId(UUID voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherPolicy() { + return voucherPolicy; + } + + public void setVoucherPolicy(String voucherPolicy) { + this.voucherPolicy = voucherPolicy; + } + + public Long getAmount() { + return amount; + } + + public void setAmount(Long amount) { + this.amount = amount; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/dto/WalletViewDto.java b/src/main/java/org/prgrms/kdtspringdemo/dto/WalletViewDto.java new file mode 100644 index 0000000000..fd3eedcde6 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/dto/WalletViewDto.java @@ -0,0 +1,48 @@ +package org.prgrms.kdtspringdemo.dto; + +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; +import org.prgrms.kdtspringdemo.wallet.domain.Wallet; + +import java.util.List; +import java.util.UUID; + +public class WalletViewDto { + private UUID walletId; + private UUID customerId; + private List voucherList; + + public WalletViewDto(UUID walletId, UUID customerId, List voucherList) { + this.walletId = walletId; + this.customerId = customerId; + this.voucherList = voucherList; + } + + public WalletViewDto(Wallet wallet) { + this.walletId = wallet.getWalletId(); + this.customerId = wallet.getCustomerId(); + } + + public UUID getWalletId() { + return walletId; + } + + public void setWalletId(UUID walletId) { + this.walletId = walletId; + } + + public UUID getCustomerId() { + return customerId; + } + + public void setCustomerId(UUID customerId) { + this.customerId = customerId; + } + + public List getVoucherList() { + return voucherList; + } + + public void setVoucherList(List voucherList) { + this.voucherList = voucherList; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/view/OutputConsole.java b/src/main/java/org/prgrms/kdtspringdemo/view/OutputConsole.java index 22c9191930..d8e8e382a2 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/view/OutputConsole.java +++ b/src/main/java/org/prgrms/kdtspringdemo/view/OutputConsole.java @@ -1,37 +1,63 @@ package org.prgrms.kdtspringdemo.view; -import org.prgrms.kdtspringdemo.customer.domain.Customer; -import org.prgrms.kdtspringdemo.voucher.domain.Voucher; -import org.prgrms.kdtspringdemo.voucher.domain.VoucherPolicy; +import org.prgrms.kdtspringdemo.dto.CustomerViewDto; +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; +import org.prgrms.kdtspringdemo.dto.WalletViewDto; import org.springframework.stereotype.Component; @Component public class OutputConsole { private final String start_string = "> "; - public void start() { + public void startProgram() { System.out.println(); System.out.println("=== Voucher Program ==="); System.out.println("Type exit to exit the program."); - System.out.println("Type create to create the program."); - System.out.println("Type list to list all vouchers."); + System.out.println("Type voucher to start voucher mode."); + System.out.println("Type customer to start customer mode."); + System.out.println("Type wallet to start wallet mode."); + System.out.print(start_string); + } + + public void startCustomerMode() { + System.out.println(); + System.out.println("=== Customer Mode ==="); + System.out.println("To return to mode selection, please enter exit."); + System.out.println("Type create to create the customer."); + System.out.println("Type list to list all customers."); System.out.print(start_string); } - public void printVoucher(Voucher voucher) { - System.out.println("====================================="); - System.out.println("voucherId : " + voucher.getVoucherId()); - System.out.println("voucherAmount : " + voucher.getVoucherPolicy().getAmount()); - System.out.println("voucherType : " + voucher.getVoucherPolicy().getVoucherType()); + public void startWalletMode() { System.out.println(); + System.out.println("=== Wallet Mode ==="); + System.out.println("To return to mode selection, please enter exit."); + System.out.println("Type findByCustomerId : findVouchers by customerId"); + System.out.println("Type deleteVoucher : deleteVoucher by customerId & voucherId"); + System.out.println("Type insertVoucher : insertVoucher by customerId & voucherId"); + System.out.println("Type list : Wallet All List"); + System.out.print(start_string); } - public void printCustomer(Customer customer) { - System.out.println("====================================="); - System.out.println("customerId : " + customer.getCustomerId()); - System.out.println("name : " + customer.getName()); - System.out.println("isBlack : " + customer.isBlack()); + public void startVoucherMode() { System.out.println(); + System.out.println("=== Voucher Mode ==="); + System.out.println("To return to mode selection, please enter exit."); + System.out.println("Type create to create the voucher."); + System.out.println("Type list to list all vouchers."); + System.out.print(start_string); + } + + public void printVoucher(VoucherViewDto voucherViewDto) { + System.out.println(voucherViewDto.toString()); + } + + public void printCustomer(CustomerViewDto customerViewDto) { + System.out.println(customerViewDto.toString()); + } + + public void printWallet(WalletViewDto walletViewDto) { + System.out.println(walletViewDto.toString()); } public void getVoucherType() { @@ -40,12 +66,54 @@ public void getVoucherType() { System.out.print(start_string); } + public void getCustomerName() { + System.out.println("=============================================="); + System.out.println("Enter Customer Name"); + System.out.print(start_string); + } + + public void getCustomerId() { + System.out.println("=============================================="); + System.out.println("Enter Customer Id"); + System.out.print(start_string); + } + + public void getVoucherId() { + System.out.println("=============================================="); + System.out.println("Enter Voucher Id"); + System.out.print(start_string); + } + + public void getWalletId() { + System.out.println("=============================================="); + System.out.println("Enter Wallet Id"); + System.out.print(start_string); + } + + public void getCustomerIsBlack() { + System.out.println("=============================================="); + System.out.println("Enter Customer isBlack? (true / false)"); + System.out.print(start_string); + } + public void getVoucherAmount() { System.out.println("Enter Voucher Amount or Percent"); System.out.print(start_string); } - public void printNumberFormatException() { - System.out.println("올바른 숫자 형식이 아닙니다."); + public void printProgramEnd(){ + System.out.println("<<>>"); + } + + public void printVoucherModeEnd() { + System.out.println("<< End Voucher Mode >>"); + } + + public void printCustomerModeEnd() { + System.out.println("<< End Customer Mode >>"); + } + + public void printWalletModeEnd() { + System.out.println("<< End Wallet Mode >>"); } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/VoucherFunction.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/VoucherFunction.java index c83cddbcf3..0597265046 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/VoucherFunction.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/VoucherFunction.java @@ -1,12 +1,9 @@ package org.prgrms.kdtspringdemo.voucher; import org.prgrms.kdtspringdemo.voucher.controller.VoucherController; -import org.prgrms.kdtspringdemo.voucher.service.VoucherService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; -import java.io.IOException; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.function.Consumer; @@ -14,7 +11,7 @@ public enum VoucherFunction { CREATE("create", "바우처 등록", VoucherController::createVoucher), LIST_ALL_VOUCHERS("list", "바우처 목록", VoucherController::showAllVouchers), - EXIT("exit", "프로그램을 종료합니다.", VoucherController::endProgram); + EXIT("exit", "voucher mode 종료", VoucherController::endVoucherMode); private final String fun; private final String description; private final Consumer voucherControllerConsumer; @@ -27,8 +24,9 @@ public enum VoucherFunction { } public static VoucherFunction findByCode(String fun) { + String lowerFun = fun.toLowerCase(); return Arrays.stream(values()) - .filter(option -> option.fun.equals(fun)) + .filter(option -> option.fun.equals(lowerFun)) .findFirst() .orElseThrow(() -> { logger.error("해당 명령어가 존재하지 않습니다."); diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherController.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherController.java index 443eedcc66..af319a20c6 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherController.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherController.java @@ -2,8 +2,6 @@ import org.prgrms.kdtspringdemo.view.InputConsole; import org.prgrms.kdtspringdemo.view.OutputConsole; -import org.prgrms.kdtspringdemo.voucher.domain.Voucher; -import org.prgrms.kdtspringdemo.voucher.domain.VoucherPolicy; import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; import org.prgrms.kdtspringdemo.voucher.service.VoucherService; import org.slf4j.Logger; @@ -11,8 +9,6 @@ import org.springframework.stereotype.Controller; import java.io.IOException; -import java.util.List; -import java.util.UUID; @Controller public class VoucherController { @@ -36,28 +32,22 @@ private VoucherTypeFunction findVoucherType() throws IOException { public void createVoucher() { try { VoucherTypeFunction voucherType = findVoucherType(); - UUID voucherId = UUID.randomUUID(); outputConsole.getVoucherAmount(); long amount = Long.parseLong(inputConsole.getString()); - - voucherService.createVoucher(voucherType, voucherId, amount); + voucherService.createVoucher(voucherType, amount); } catch (NumberFormatException e) { logger.error("올바른 숫자 형식이 아닙니다."); - } catch (IOException e) { - logger.error(e.getMessage()); - } catch (RuntimeException e) { + } catch (IOException | RuntimeException e) { logger.error(e.getMessage()); } } public void showAllVouchers() { - List voucherList = voucherService.findAll().get(); - voucherList.forEach(voucher -> outputConsole.printVoucher(voucher)); + voucherService.getVoucherViewDtoList().forEach(outputConsole::printVoucher); } - public void endProgram() { - voucherService.endVoucherService(); + public void endVoucherMode() { + outputConsole.printVoucherModeEnd(); } - } diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherRestController.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherRestController.java new file mode 100644 index 0000000000..246e64316e --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherRestController.java @@ -0,0 +1,57 @@ +package org.prgrms.kdtspringdemo.voucher.controller; + +import org.prgrms.kdtspringdemo.dto.VoucherRequestDto; +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; +import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; +import org.prgrms.kdtspringdemo.voucher.service.VoucherService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@RestController +@RequestMapping("/api/v1/vouchers") +public class VoucherRestController { + private final VoucherService voucherService; + + public VoucherRestController(VoucherService voucherService) { + this.voucherService = voucherService; + } + + @GetMapping + public ResponseEntity> showAllVouchers(@RequestParam(name = "id", required = false) Optional voucherId, @RequestParam(name = "policy", required = false) Optional policy) { + + if (voucherId.isPresent()) { + VoucherViewDto voucher = voucherService.findById(voucherId.get()); + if (voucher != null) { + return new ResponseEntity<>(Collections.singletonList(voucher), HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } else if (policy.isPresent()) { + List vouchers = voucherService.findByPolicy(policy.get()); + return new ResponseEntity<>(vouchers, HttpStatus.OK); + } else { + List vouchers = voucherService.getVoucherViewDtoList(); + return new ResponseEntity<>(vouchers, HttpStatus.OK); + } + } + + @PostMapping + ResponseEntity create(@ModelAttribute VoucherRequestDto voucherRequestDto) { + VoucherTypeFunction voucherTypeFunction = VoucherTypeFunction.findByCode(voucherRequestDto.getVoucherPolicy()); + long amount = voucherRequestDto.getAmount(); + VoucherViewDto voucher = voucherService.createVoucher(voucherTypeFunction, amount); + return new ResponseEntity<>(voucher, HttpStatus.CREATED); + } + + @DeleteMapping("/{voucherId}") + void deleteById(@PathVariable UUID voucherId) { + voucherService.deleteById(voucherId); + } + +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherWebController.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherWebController.java new file mode 100644 index 0000000000..1f210c22f5 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/controller/VoucherWebController.java @@ -0,0 +1,70 @@ +package org.prgrms.kdtspringdemo.voucher.controller; + +import org.prgrms.kdtspringdemo.dto.VoucherRequestDto; +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; +import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; +import org.prgrms.kdtspringdemo.voucher.service.VoucherService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@Controller +@RequestMapping("/vouchers") +public class VoucherWebController { + private final VoucherService voucherService; + + public VoucherWebController(VoucherService voucherService) { + this.voucherService = voucherService; + } + + @GetMapping + public String getAllVouchers(@RequestParam(name = "policy", required = false) String policy, Model model) { + List vouchers; + if (policy != null && !policy.isEmpty()) { + vouchers = voucherService.findByPolicy(policy); + } else { + vouchers = voucherService.getVoucherViewDtoList(); + } + + model.addAttribute("vouchers", vouchers); + return "voucher"; + } + + @GetMapping("/{voucherId}") + public String viewVoucher(@PathVariable UUID voucherId, Model model) { + VoucherViewDto voucherViewDto = voucherService.findById(voucherId); + model.addAttribute("voucher", voucherViewDto); + return "voucher_details"; + } + + @PostMapping("/create") + public String createVoucher(@ModelAttribute VoucherRequestDto voucherRequestDto) { + VoucherTypeFunction voucherTypeFunction = VoucherTypeFunction.findByCode(voucherRequestDto.getVoucherPolicy()); + voucherService.createVoucher(voucherTypeFunction, voucherRequestDto.getAmount()); + return "redirect:/vouchers"; + } + + @GetMapping("/{voucherId}/delete") + public String deleteVoucher(@PathVariable UUID voucherId) { + voucherService.deleteById(voucherId); + return "redirect:/vouchers"; + } + + @GetMapping("/{voucherId}/edit") + public String editVoucher(@PathVariable UUID voucherId, Model model) { + VoucherViewDto voucher = voucherService.findById(voucherId); + VoucherRequestDto voucherRequestDto = new VoucherRequestDto(voucher.getVoucherPolicy(), voucher.getAmount()); + + model.addAttribute("voucher", voucherRequestDto); + return "voucher_edit"; // 바우처 정보 수정 페이지로 이동 + } + + @PostMapping("/{voucherId}/edit") + public String editVoucher(@PathVariable UUID voucherId, @ModelAttribute VoucherRequestDto voucherRequestDto) { + voucherService.updateVoucher(voucherId, voucherRequestDto); + return "redirect:/vouchers/" + voucherId; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/domain/Voucher.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/domain/Voucher.java index 5ba87988b2..33f8ad06c3 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/domain/Voucher.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/domain/Voucher.java @@ -18,4 +18,10 @@ public UUID getVoucherId() { public VoucherPolicy getVoucherPolicy() { return voucherPolicy; } + + public String toString() { + return "=======================\n"+ + "voucherId : "+voucherId+"\n"+ + "voucherPolicy : "+voucherPolicy.getVoucherType()+"\n"; + } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepository.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepository.java index e59f395106..4eb2ef79e7 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepository.java @@ -6,10 +6,10 @@ import org.prgrms.kdtspringdemo.voucher.domain.VoucherPolicy; import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; +import org.prgrms.kdtspringdemo.dto.VoucherRequestDto; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Repository; @@ -19,7 +19,7 @@ @Repository @Profile("dev") -@PropertySource(value = "classpath:application_prod.yml") +@PropertySource("classpath=application.yml") public class FileVoucherRepository implements VoucherRepository{ private CsvFileHandler csvFileHandler; @Value("${voucher_file}") @@ -47,6 +47,11 @@ public Voucher insert(Voucher voucher) { return voucher; } + @Override + public void update(UUID voucherId, VoucherRequestDto voucherRequestDto) { + + } + @Override public Optional findById(UUID voucherId) { try{ @@ -69,7 +74,7 @@ public Optional findById(UUID voucherId) { } @Override - public Optional> findAll() { + public List findAll() { List voucherList = new ArrayList<>(); try { List data = csvFileHandler.readCSV(filePath); @@ -82,10 +87,30 @@ public Optional> findAll() { Voucher voucher = voucherTypeFunction.create(voucherId, amount); voucherList.add(voucher); }); - return Optional.of(voucherList); + return voucherList; } catch (IOException e) { logger.error(e.getMessage()); } - return Optional.of(voucherList); + return voucherList; + } + + @Override + public List findByPolicy(String policy) { + return null; + } + + @Override + public List findUnallocatedVoucher() { + return null; + } + + @Override + public void deleteById(UUID voucherId) { + + } + + @Override + public void deleteAll() { + } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/JdbcVoucherRepository.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/JdbcVoucherRepository.java index 6438e123c2..ffed2e5649 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/JdbcVoucherRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/JdbcVoucherRepository.java @@ -1,9 +1,8 @@ package org.prgrms.kdtspringdemo.voucher.repository; -import org.prgrms.kdtspringdemo.customer.domain.Customer; -import org.prgrms.kdtspringdemo.customer.repository.CustomerRepository; import org.prgrms.kdtspringdemo.voucher.domain.Voucher; import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; +import org.prgrms.kdtspringdemo.dto.VoucherRequestDto; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -14,7 +13,6 @@ import org.springframework.stereotype.Repository; import javax.sql.DataSource; -import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; import java.util.Optional; @@ -45,6 +43,7 @@ public JdbcVoucherRepository(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } + @Override public Voucher insert(Voucher voucher) { var update = jdbcTemplate.update("INSERT INTO voucher(voucher_id, voucher_type, amount) VALUES (UUID_TO_BIN(?), ?, ?)", voucher.getVoucherId().toString().getBytes(), @@ -56,6 +55,17 @@ public Voucher insert(Voucher voucher) { return voucher; } + @Override + public void update(UUID voucherId, VoucherRequestDto voucherRequestDto) { + var update = jdbcTemplate.update("UPDATE voucher SET voucher_type = ?, amount = ? WHERE voucher_id = UUID_TO_BIN(?)", + voucherRequestDto.getVoucherPolicy(), + voucherRequestDto.getAmount(), + voucherId.toString()); + if(update != 1) { + throw new RuntimeException("Nothing was inserted"); + } + } + @Override public Optional findById(UUID voucherId) { try { @@ -68,12 +78,34 @@ public Optional findById(UUID voucherId) { } } + @Override public void deleteAll() { jdbcTemplate.update("DELETE FROM voucher"); } @Override - public Optional> findAll() { - return Optional.of(jdbcTemplate.query("select * from voucher", voucherRowMapper)); + public List findAll() { + return jdbcTemplate.query("select * from voucher", voucherRowMapper); + } + + @Override + public List findByPolicy(String policy) { + return jdbcTemplate.query("select * from voucher where voucher_type = ?", + voucherRowMapper, + policy); + } + + @Override + public List findUnallocatedVoucher() { + return jdbcTemplate.query("SELECT voucher.voucher_id, voucher.voucher_type, voucher.amount FROM voucher LEFT JOIN wallet_customer_voucher ON voucher.voucher_id = wallet_customer_voucher.voucher_id WHERE wallet_customer_voucher.voucher_id IS NULL", + voucherRowMapper); + } + + @Override + public void deleteById(UUID voucherId) { + jdbcTemplate.update("delete from wallet_customer_voucher where voucher_id = UUID_TO_BIN(?)", + voucherId.toString()); + jdbcTemplate.update("delete from voucher where voucher_id = UUID_TO_BIN(?)", + voucherId.toString()); } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/MemoryVoucherRepository.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/MemoryVoucherRepository.java index 2ba5720cad..17ad5be754 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/MemoryVoucherRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/MemoryVoucherRepository.java @@ -1,7 +1,7 @@ package org.prgrms.kdtspringdemo.voucher.repository; import org.prgrms.kdtspringdemo.voucher.domain.Voucher; -import org.prgrms.kdtspringdemo.voucher.domain.VoucherPolicy; +import org.prgrms.kdtspringdemo.dto.VoucherRequestDto; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository; @@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap; @Repository -@Profile({"local", "test"}) +@Profile({"local"}) public class MemoryVoucherRepository implements VoucherRepository{ private final Map storage; @@ -23,14 +23,36 @@ public Voucher insert(Voucher voucher) { return voucher; } + @Override + public void update(UUID voucherId, VoucherRequestDto voucherRequestDto) { + } + @Override public Optional findById(UUID voucherId) { return Optional.ofNullable(storage.get(voucherId)); } @Override - public Optional> findAll() { + public List findAll() { List voucherList = storage.values().stream().toList(); - return Optional.ofNullable(voucherList); + return voucherList; + } + + @Override + public List findByPolicy(String policy) { + return null; + } + + @Override + public List findUnallocatedVoucher() { + return null; + } + + @Override + public void deleteById(UUID voucherId) { + } + + @Override + public void deleteAll() { } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/VoucherRepository.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/VoucherRepository.java index da9332c5ee..ae4b971ba2 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/VoucherRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/repository/VoucherRepository.java @@ -1,7 +1,7 @@ package org.prgrms.kdtspringdemo.voucher.repository; import org.prgrms.kdtspringdemo.voucher.domain.Voucher; -import org.prgrms.kdtspringdemo.voucher.domain.VoucherPolicy; +import org.prgrms.kdtspringdemo.dto.VoucherRequestDto; import java.util.List; import java.util.Optional; @@ -9,6 +9,11 @@ public interface VoucherRepository { Voucher insert(Voucher voucher); + void update(UUID voucherId, VoucherRequestDto voucherRequestDto); Optional findById(UUID voucherId); - Optional> findAll(); + List findAll(); + List findByPolicy(String policy); + List findUnallocatedVoucher(); + void deleteById(UUID voucherId); + void deleteAll(); } diff --git a/src/main/java/org/prgrms/kdtspringdemo/voucher/service/VoucherService.java b/src/main/java/org/prgrms/kdtspringdemo/voucher/service/VoucherService.java index 8dd443789d..9d9e4cac81 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/voucher/service/VoucherService.java +++ b/src/main/java/org/prgrms/kdtspringdemo/voucher/service/VoucherService.java @@ -1,7 +1,8 @@ package org.prgrms.kdtspringdemo.voucher.service; +import org.prgrms.kdtspringdemo.dto.VoucherRequestDto; +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; import org.prgrms.kdtspringdemo.voucher.domain.Voucher; -import org.prgrms.kdtspringdemo.voucher.domain.VoucherPolicy; import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; import org.prgrms.kdtspringdemo.voucher.repository.VoucherRepository; import org.slf4j.Logger; @@ -10,7 +11,6 @@ import java.text.MessageFormat; import java.util.List; -import java.util.Optional; import java.util.UUID; @Service @@ -26,26 +26,41 @@ public VoucherTypeFunction getVoucherTypeFunction(String type) { return VoucherTypeFunction.findByCode(type); } - public Voucher createVoucher(VoucherTypeFunction voucherType, UUID voucherId, long amount) { - Voucher voucher = voucherType.create(voucherId, amount); - voucherRepository.insert(voucher); - return voucher; + public VoucherViewDto createVoucher(VoucherTypeFunction voucherType, long amount) { + UUID voucherId = UUID.randomUUID(); + Voucher voucher = voucherRepository.insert(voucherType.create(voucherId, amount)); + return new VoucherViewDto(voucher); } - public Optional> findAll() { - return Optional.of(voucherRepository.findAll().get()); + public void updateVoucher(UUID voucherId, VoucherRequestDto voucherRequestDto) { + voucherRepository.update(voucherId, voucherRequestDto); } - public Voucher findById(UUID voucherId) { - return voucherRepository - .findById(voucherId) - .orElseThrow(() -> { - logger.error(MessageFormat.format("Can not find a voucher for {0}", voucherId)); - return new RuntimeException(MessageFormat.format("Can not find a voucher for {0}", voucherId)); - }); + public List getVoucherViewDtoList() { + return voucherRepository.findAll().stream().map(VoucherViewDto::new).toList(); } - public void endVoucherService() { - System.exit(0); + public List findByPolicy(String policy) { + return voucherRepository.findByPolicy(policy).stream().map(VoucherViewDto::new).toList(); + } + + public List findUnallocatedVoucher() { + return voucherRepository.findUnallocatedVoucher().stream().map(VoucherViewDto::new).toList(); + } + + public VoucherViewDto findById(UUID voucherId) { + Voucher voucher = voucherRepository.findById(voucherId).orElseThrow(() -> { + logger.error(MessageFormat.format("Can not find a voucher for {0}", voucherId)); + return new RuntimeException(MessageFormat.format("Can not find a voucher for {0}", voucherId)); + }); + return new VoucherViewDto(voucher); + } + + public void deleteById(UUID voucherId) { + voucherRepository.deleteById(voucherId); + } + + public void deleteAll() { + voucherRepository.deleteAll(); } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/WalletFunction.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/WalletFunction.java new file mode 100644 index 0000000000..a94e833c2c --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/wallet/WalletFunction.java @@ -0,0 +1,42 @@ +package org.prgrms.kdtspringdemo.wallet; + +import org.prgrms.kdtspringdemo.wallet.controller.WalletController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.NoSuchElementException; +import java.util.function.Consumer; + +public enum WalletFunction { + INSERT_VOUCHER_IN_WALLET("insertvoucher", "customerId의 지갑에 voucherId 추가", WalletController::insertVoucherByCustomerId), + FIND_BY_CUSTOMER("findbycustomerid", "고객이 가진 지갑 속 바우처 조회", WalletController::printVouchersByCustomerId), + DELETE_VOUCHER("deletevoucher", "customerId, voucherId로 바우처 삭제", WalletController::deleteVoucherByVoucherId), + ALL_WALLET_LIST("list","wallet list 출력", WalletController::printAllWallet), + EXIT("exit", "wallet mode 종료", WalletController::endWalletMode); + private final String fun; + private final String description; + private final Consumer walletControllerConsumer; + private final static Logger logger = LoggerFactory.getLogger(WalletFunction.class); + + WalletFunction(String fun, String description, Consumer walletControllerConsumer) { + this.fun = fun; + this.description = description; + this.walletControllerConsumer = walletControllerConsumer; + } + + public static WalletFunction findByCode(String fun) { + String lowerFun = fun.toLowerCase(); + return Arrays.stream(values()) + .filter(option -> option.fun.equals(lowerFun)) + .findFirst() + .orElseThrow(() -> { + logger.error("해당 명령어가 존재하지 않습니다."); + return new NoSuchElementException("해당 명령어가 존재하지 않습니다."); + }); + } + + public void execute(WalletController walletController) { + this.walletControllerConsumer.accept(walletController); + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletController.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletController.java new file mode 100644 index 0000000000..91f5c20fd2 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletController.java @@ -0,0 +1,73 @@ +package org.prgrms.kdtspringdemo.wallet.controller; + +import org.prgrms.kdtspringdemo.view.InputConsole; +import org.prgrms.kdtspringdemo.view.OutputConsole; +import org.prgrms.kdtspringdemo.voucher.domain.Voucher; +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; +import org.prgrms.kdtspringdemo.wallet.domain.Wallet; +import org.prgrms.kdtspringdemo.dto.WalletViewDto; +import org.prgrms.kdtspringdemo.wallet.service.WalletService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; + +import java.io.IOException; +import java.util.UUID; +import java.util.List; + +@Controller +public class WalletController { + private final WalletService walletService; + private final OutputConsole outputConsole = new OutputConsole(); + private final InputConsole inputConsole = new InputConsole(); + private final Logger logger = LoggerFactory.getLogger(WalletController.class); + + public WalletController(WalletService walletService) { + this.walletService = walletService; + } + + public void printVouchersByCustomerId() { + try { + outputConsole.getCustomerId(); + UUID customerId = UUID.fromString(inputConsole.getString()); + List vouchers = walletService.findVouchersById(customerId); + vouchers.forEach(outputConsole::printVoucher); + } catch (IOException e) { + logger.error(e.getMessage()); + } + } + + public void insertVoucherByCustomerId() { + try { + outputConsole.getWalletId(); + UUID walletId = UUID.fromString(inputConsole.getString()); + outputConsole.getCustomerId(); + UUID customerId = UUID.fromString(inputConsole.getString()); + outputConsole.getVoucherId(); + UUID voucherId = UUID.fromString(inputConsole.getString()); + walletService.addVoucherByCustomerId(walletId, customerId, voucherId); + } catch (IOException e) { + logger.error(e.getMessage()); + } + } + + public void deleteVoucherByVoucherId() { + try { + outputConsole.getCustomerId(); + UUID customerId = UUID.fromString(inputConsole.getString()); + outputConsole.getVoucherId(); + UUID voucherId = UUID.fromString(inputConsole.getString()); + walletService.deleteVoucherByVoucherId(customerId, voucherId); + } catch (IOException e) { + logger.error(e.getMessage()); + } + } + + public void printAllWallet() { + walletService.getWalletViewDtoList().forEach(outputConsole::printWallet); + } + + public void endWalletMode() { + outputConsole.printWalletModeEnd(); + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletWebController.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletWebController.java new file mode 100644 index 0000000000..41b2d96833 --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/wallet/controller/WalletWebController.java @@ -0,0 +1,73 @@ +package org.prgrms.kdtspringdemo.wallet.controller; + +import org.prgrms.kdtspringdemo.dto.AddVoucherToWalletDto; +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; +import org.prgrms.kdtspringdemo.dto.WalletViewDto; +import org.prgrms.kdtspringdemo.voucher.service.VoucherService; +import org.prgrms.kdtspringdemo.wallet.service.WalletService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@Controller +@RequestMapping("/wallets") +public class WalletWebController { + private final WalletService walletService; + private final VoucherService voucherService; + + public WalletWebController(WalletService walletService, VoucherService voucherService) { + this.walletService = walletService; + this.voucherService = voucherService; + } + + @GetMapping + public String listWallets(Model model) { + List walletViewDtoList = walletService.getWalletViewDtoList(); + model.addAttribute("walletList", walletViewDtoList); + return "wallet"; + } + + @GetMapping("/{walletId}") + public String viewWallet(@PathVariable UUID walletId, Model model) { + WalletViewDto walletViewDto = walletService.findById(walletId).orElseThrow(NoSuchFieldError::new); + List voucherViewDtoList = walletService.findVouchersById(walletViewDto.getCustomerId()); + walletViewDto.setVoucherList(voucherViewDtoList); + List unallocatedVouchers = voucherService.findUnallocatedVoucher(); + + model.addAttribute("wallet", walletViewDto); + model.addAttribute("voucherList", unallocatedVouchers); + model.addAttribute("addVoucherToWalletDto", new AddVoucherToWalletDto()); + return "wallet_details"; + } + + @PostMapping("/{walletId}/add-voucher") + public String addVoucherToWallet(@PathVariable UUID walletId, @ModelAttribute AddVoucherToWalletDto addVoucherToWalletDto) { + + UUID selectedVoucherId = addVoucherToWalletDto.getSelectedVoucherId(); + if (selectedVoucherId != null) { + WalletViewDto walletViewDto = walletService.findById(walletId).orElse(null); + VoucherViewDto selectedVoucher = voucherService.findById(selectedVoucherId); + + if (walletViewDto != null && selectedVoucher != null) { + walletService.addVoucherByCustomerId(walletViewDto.getWalletId(), walletViewDto.getCustomerId(), selectedVoucherId); + } + } + + return "redirect:/wallets/{walletId}"; + } + + @GetMapping("/{walletId}/delete") + public String deleteById(@PathVariable UUID walletId) { + walletService.deleteById(walletId); + return "redirect:/wallets"; + } + + @GetMapping("/{walletId}/delete/{voucherId}") + public String deleteVoucher(@PathVariable UUID walletId, @PathVariable UUID voucherId) { + walletService.deleteVoucherByVoucherId(walletId, voucherId); + return "redirect:/wallets"; + } +} diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/domain/Wallet.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/domain/Wallet.java index 3625340f97..2b70209799 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/wallet/domain/Wallet.java +++ b/src/main/java/org/prgrms/kdtspringdemo/wallet/domain/Wallet.java @@ -6,23 +6,22 @@ public class Wallet { private final UUID walletId; private final UUID customerId; - private final List vouchers; - public Wallet(UUID walletId, UUID customerId, List vouchers) { + public Wallet(UUID walletId, UUID customerId) { this.walletId = walletId; this.customerId = customerId; - this.vouchers = vouchers; } public UUID getWalletId() { return walletId; } - public UUID getCustomerId() { return customerId; } - public List getVouchers() { - return vouchers; + public String toString() { + return "=======================\n" + + "[walletId] : " + walletId + "\n" + + "[customerId] : " + customerId + "\n"; } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/domain/dto/WalletDBDto.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/domain/dto/WalletDBDto.java deleted file mode 100644 index a0d0c472c6..0000000000 --- a/src/main/java/org/prgrms/kdtspringdemo/wallet/domain/dto/WalletDBDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.prgrms.kdtspringdemo.wallet.domain.dto; - -import java.util.UUID; - -public class WalletDBDto { - private final UUID walletId; - private final UUID customerId; - - public WalletDBDto(UUID walletId, UUID customerId) { - this.walletId = walletId; - this.customerId = customerId; - } - - public UUID getWalletId() { - return walletId; - } - - public UUID getCustomerId() { - return customerId; - } -} diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepository.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepository.java index 854c1a3cf1..b3801700c3 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepository.java @@ -1,34 +1,54 @@ package org.prgrms.kdtspringdemo.wallet.repository; import com.google.gson.Gson; -import org.prgrms.kdtspringdemo.customer.repository.JdbcCustomerRepository; +import com.google.gson.JsonArray; + +import org.prgrms.kdtspringdemo.customer.domain.Customer; import org.prgrms.kdtspringdemo.voucher.domain.Voucher; +import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; import org.prgrms.kdtspringdemo.wallet.domain.Wallet; -import org.prgrms.kdtspringdemo.wallet.domain.dto.WalletDBDto; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.json.GsonJsonParser; import org.springframework.context.annotation.Profile; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import javax.sql.DataSource; import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.UUID; @Repository @Profile("DB") public class JdbcWalletRepository implements WalletRepository{ - private static final Logger logger = LoggerFactory.getLogger(JdbcWalletRepository.class); + private final Logger logger = LoggerFactory.getLogger(JdbcWalletRepository.class); private final JdbcTemplate jdbcTemplate; - private final Gson gson = new Gson(); - private static final RowMapper customerRowMapper = (resultSet, i) -> { + private static final RowMapper walletRowMapper = (resultSet, i) -> { var walletId = toUUID(resultSet.getBytes("wallet_id")); var customerId = toUUID(resultSet.getBytes("customer_id")); - return new WalletDBDto(walletId, customerId); + + return new Wallet(walletId, customerId); + }; + + private static final RowMapper voucherRowMapper = (resultSet, i) -> { + var amount = Long.parseLong(resultSet.getString("amount")); + var voucherType = resultSet.getString("voucher_type"); + var voucherId = toUUID(resultSet.getBytes("voucher_id")); + + var voucherTypeEnum = VoucherTypeFunction.findByCode(voucherType); + return voucherTypeEnum.create(voucherId, amount); + }; + + private static final RowMapper customerRowMapper = (resultSet, i) -> { + var customerName = resultSet.getString("name"); + var isBlack = Boolean.parseBoolean(resultSet.getString("is_black")); + var customerId = toUUID(resultSet.getBytes("customer_id")); + return new Customer(customerId, customerName, isBlack); }; static UUID toUUID(byte[] bytes) { @@ -42,10 +62,9 @@ public JdbcWalletRepository(DataSource dataSource) { } @Override public Wallet insert(Wallet wallet) { - var update = jdbcTemplate.update("INSERT INTO wallet(wallet_id, customer_id, vouchers) VALUES (UUID_TO_BIN(?), UUID_TO_BIN(?), ?)", - wallet.getWalletId().toString().getBytes(), - wallet.getCustomerId().toString().getBytes(), - gson.toJson(wallet.getVouchers())); + var update = jdbcTemplate.update("INSERT INTO wallet(wallet_id, customer_id) VALUES (UUID_TO_BIN(?), UUID_TO_BIN(?))", + wallet.getWalletId().toString(), + wallet.getCustomerId().toString()); if(update != 1) { throw new RuntimeException("Nothing was inserted"); } @@ -53,17 +72,67 @@ public Wallet insert(Wallet wallet) { } @Override - public List findAllVoucherByCustomerId(UUID customerId) { - return null; + public Optional findById(UUID walletId) { + try { + return Optional.ofNullable(jdbcTemplate.queryForObject("select * from wallet WHERE wallet_id = UUID_TO_BIN(?)", + walletRowMapper, + walletId.toString())); + } catch (EmptyResultDataAccessException e) { + logger.error("Got empty result", e); + return Optional.empty(); + } } @Override - public void deleteVoucherByVoucherId(UUID voucherId) { + public List findAll() { + return jdbcTemplate.query("select * from wallet", walletRowMapper); + } + @Override + public List findVouchersByCustomerId(UUID customerId) { + return jdbcTemplate.query("select voucher.voucher_id, voucher.voucher_type, voucher.amount from voucher inner join wallet_customer_voucher on voucher.voucher_id = wallet_customer_voucher.voucher_id WHERE wallet_customer_voucher.customer_id = UUID_TO_BIN(?)", + voucherRowMapper, + customerId.toString() + ); } @Override - public void findCustomerByVoucherId(UUID voucherId) { + public void deleteVoucherByVoucherId(UUID customerId, UUID voucherId) { + var update = jdbcTemplate.update("DELETE from wallet_customer_voucher WHERE customer_id = UUID_TO_BIN(?) and voucher_id = UUID_TO_BIN(?)", + customerId.toString(), + voucherId.toString()); + if(update != 1) { + throw new RuntimeException("Nothing was deleted"); + } + } + @Override + public Optional findCustomerByVoucherId(UUID voucherId) { + return Optional.ofNullable(jdbcTemplate.queryForObject("select c.customer_id, c.name, c.is_black from customers c inner join wallet_customer_voucher inter" + + "on c.customer_id = inter.customer_id where inter.voucher_id = UUID_TO_BIN(?)", + customerRowMapper, + voucherId.toString())); + } + + @Override + public void deleteById(UUID walletId) { + jdbcTemplate.update("DELETE FROM wallet where wallet_id = UUID_TO_BIN(?)", + walletId.toString()); + } + + @Override + public void addVoucherByCustomerId(UUID walletId, UUID customerId, UUID voucherId) { + var update = jdbcTemplate.update("INSERT INTO wallet_customer_voucher(wallet_id, customer_id, voucher_id) VALUES (UUID_TO_BIN(?), UUID_TO_BIN(?), UUID_TO_BIN(?))", + walletId.toString(), + customerId.toString(), + voucherId.toString()); + if(update != 1) { + throw new RuntimeException("Nothing was inserted"); + } + } + + @Override + public void deleteAll() { + jdbcTemplate.update("DELETE FROM wallet"); } } diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/WalletRepository.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/WalletRepository.java index bee328cff3..58d5604373 100644 --- a/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/WalletRepository.java +++ b/src/main/java/org/prgrms/kdtspringdemo/wallet/repository/WalletRepository.java @@ -1,15 +1,22 @@ package org.prgrms.kdtspringdemo.wallet.repository; +import org.prgrms.kdtspringdemo.customer.domain.Customer; import org.prgrms.kdtspringdemo.voucher.domain.Voucher; import org.prgrms.kdtspringdemo.wallet.domain.Wallet; +import java.util.Optional; import java.util.UUID; import java.util.List; public interface WalletRepository { Wallet insert(Wallet wallet); - List findAllVoucherByCustomerId(UUID customerId); - void deleteVoucherByVoucherId(UUID voucherId); - void findCustomerByVoucherId(UUID voucherId); + void addVoucherByCustomerId(UUID walletId, UUID customerId, UUID voucherId); + Optional findById(UUID voucherId); + List findAll(); + List findVouchersByCustomerId(UUID customerId); + void deleteVoucherByVoucherId(UUID customerId, UUID voucherId); + Optional findCustomerByVoucherId(UUID voucherId); + void deleteById(UUID walletId); + void deleteAll(); } diff --git a/src/main/java/org/prgrms/kdtspringdemo/wallet/service/WalletService.java b/src/main/java/org/prgrms/kdtspringdemo/wallet/service/WalletService.java new file mode 100644 index 0000000000..fe00c3ba4a --- /dev/null +++ b/src/main/java/org/prgrms/kdtspringdemo/wallet/service/WalletService.java @@ -0,0 +1,63 @@ +package org.prgrms.kdtspringdemo.wallet.service; + +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; +import org.prgrms.kdtspringdemo.dto.WalletViewDto; +import org.prgrms.kdtspringdemo.wallet.domain.Wallet; +import org.prgrms.kdtspringdemo.wallet.repository.WalletRepository; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Service +public class WalletService { + private final WalletRepository walletRepository; + + public WalletService(WalletRepository walletRepository) { + this.walletRepository = walletRepository; + } + + public Wallet create(UUID customerId) { + Wallet wallet = new Wallet(UUID.randomUUID(), customerId); + return walletRepository.insert(wallet); + } + + public Optional findById(UUID walletId) { + return Optional.of(new WalletViewDto(walletRepository.findById(walletId).get())); + } + + public List findVouchersById(UUID customerId) { + List voucherViewDtoList = new ArrayList<>(); + walletRepository.findVouchersByCustomerId(customerId).forEach(voucher -> voucherViewDtoList.add(new VoucherViewDto(voucher))); + return voucherViewDtoList; + } + + public void addVoucherByCustomerId(UUID walletId, UUID customerId, UUID voucherId) { + walletRepository.addVoucherByCustomerId(walletId, customerId, voucherId); + } + + public void deleteVoucherByVoucherId(UUID customerId, UUID voucherId) { + walletRepository.deleteVoucherByVoucherId(customerId, voucherId); + } + + public List findAll() { + return walletRepository.findAll(); + } + + public List getWalletViewDtoList() { + List walletList = this.findAll(); + List walletViewDtoList = new ArrayList<>(); + walletList.forEach(wallet -> walletViewDtoList.add(new WalletViewDto(wallet))); + return walletViewDtoList; + } + + public void deleteById(UUID walletId) { + walletRepository.deleteById(walletId); + } + + public void deleteAll() { + walletRepository.deleteAll(); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6dec78b2f6..1bc65f0734 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,9 +1,21 @@ +voucher_file: src/main/resources/csvFiles/voucherList.csv +blackList_file: src/main/resources/csvFiles/customer_blacklist.csv + +server: + port: 8090 + spring: profiles: - active: dev + active: DB datasource: - url: jdbc:mysql://localhost:3306/kdt # 데이터베이스 URL + driver-class-name: com.mysql.cj.jdbc.Driver # 데이터베이스 드라이버 클래스명 + url: jdbc:mysql://localhost:3306/kdt?serverTimezone=UTC&characterEncoding=UTF-8 # 데이터베이스 URL username: test # 데이터베이스 사용자 이름 password: test1234! # 데이터베이스 비밀번호 - driver-class-name: com.mysql.cj.jdbc.Driver # 데이터베이스 드라이버 클래스명 \ No newline at end of file + + thymeleaf: + suffix: .html + cache: false + enabled: true + prefix: classpath:/templates/ diff --git a/src/main/resources/application_prod.yml b/src/main/resources/application_prod.yml deleted file mode 100644 index 67bea42de5..0000000000 --- a/src/main/resources/application_prod.yml +++ /dev/null @@ -1,2 +0,0 @@ -voucher_file: src/main/resources/csvFiles/voucherList.csv -blackList_file: src/main/resources/csvFiles/customer_blacklist.csv \ No newline at end of file diff --git a/src/main/resources/templates/customer.html b/src/main/resources/templates/customer.html new file mode 100644 index 0000000000..c766c3b309 --- /dev/null +++ b/src/main/resources/templates/customer.html @@ -0,0 +1,109 @@ + + + + + Customer Management + + + + + + + + +
+ +

Customer Management

+ + +

All Customers

+ + + + + + + + + + + + + + + + + +
Customer IDNameIs BlackAction
Customer IDNameIs Black + Delete +
+ + +

Add Customer

+
+
+ + +
+
+ + +
+ +
+ + +

Customers : None Have Wallet

+ + + + + + + + + + + + + + + + + + + +
Customer IDNameIs BlackAction
Customer IDNameIs Black + + + +
+ + +
+ + + + + diff --git a/src/main/resources/templates/voucher.html b/src/main/resources/templates/voucher.html new file mode 100644 index 0000000000..8a7aeabcba --- /dev/null +++ b/src/main/resources/templates/voucher.html @@ -0,0 +1,94 @@ + + + + + Voucher Management + + + + + + + + +
+

Voucher Management

+ + +
+
+ + +
+ +
+ + +

Voucher List

+ + + + + + + + + + + + + + + + + +
Voucher IDVoucher PolicyDiscount AmountAction
Voucher IDVoucher PolicyDiscount Amount + Details + Edit + Delete +
+ + +

Create Voucher

+
+
+ + +
+
+ + +
+ +
+
+ + + + + diff --git a/src/main/resources/templates/voucher_details.html b/src/main/resources/templates/voucher_details.html new file mode 100644 index 0000000000..cb8d02b4fa --- /dev/null +++ b/src/main/resources/templates/voucher_details.html @@ -0,0 +1,35 @@ + + + + + Voucher Details + + + + +
+

Voucher Details

+ +

Voucher Information

+ + + + + + + + + + + + + +
Voucher ID:Voucher ID
Voucher Policy:Voucher Policy
Discount Amount:Discount Amount
+ + Back to Voucher List +
+ + + + + diff --git a/src/main/resources/templates/voucher_edit.html b/src/main/resources/templates/voucher_edit.html new file mode 100644 index 0000000000..038d7d68b3 --- /dev/null +++ b/src/main/resources/templates/voucher_edit.html @@ -0,0 +1,40 @@ + + + + + Edit Voucher + + + + + +
+ +

Edit Voucher

+ +
+
+ + +
+ +
+ + +
+ + +
+ +
+ + Cancel +
+ + + + + diff --git a/src/main/resources/templates/wallet.html b/src/main/resources/templates/wallet.html new file mode 100644 index 0000000000..5923790eef --- /dev/null +++ b/src/main/resources/templates/wallet.html @@ -0,0 +1,61 @@ + + + + + Wallet Management + + + + + + + + +
+

Wallet Management

+ + +

All Wallets

+ + + + + + + + + + + + + + + +
Wallet IDCustomer IDAction
Wallet IDCustomer ID + Details + Delete +
+
+ + + + + diff --git a/src/main/resources/templates/wallet_details.html b/src/main/resources/templates/wallet_details.html new file mode 100644 index 0000000000..fbc598aeec --- /dev/null +++ b/src/main/resources/templates/wallet_details.html @@ -0,0 +1,81 @@ + + + + + Wallet Details + + + + + + + +
+

Wallet Details

+ + + + + + + + + + + + + + + +
Wallet IDWallet ID
Customer IDCustomer ID
Vouchers + +
+ +

Add Voucher to Wallet

+
+
+ + +
+ +
+ + +
+ + Back to Wallet List +
+ + + + + diff --git a/src/test/java/org/prgrms/kdtspringdemo/customer/controller/CustomerControllerTest.java b/src/test/java/org/prgrms/kdtspringdemo/customer/controller/CustomerControllerTest.java deleted file mode 100644 index e33ff1b073..0000000000 --- a/src/test/java/org/prgrms/kdtspringdemo/customer/controller/CustomerControllerTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.prgrms.kdtspringdemo.customer.controller; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.prgrms.kdtspringdemo.customer.service.CustomerService; - -import java.io.IOException; - - -class CustomerControllerTest { - private CustomerController customerController; - private CustomerService customerService; - - @Test - @DisplayName("블랙리스트 회원 리스트 조회") - void printAllBlackListCustomer() throws IOException { - customerService = new CustomerService(); - customerController = new CustomerController(customerService); - customerController.printAllBlackListCustomer(); - } - -} \ No newline at end of file diff --git a/src/test/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepositoryTest.java b/src/test/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepositoryTest.java index a06fd58919..13d5e4c7d1 100644 --- a/src/test/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepositoryTest.java +++ b/src/test/java/org/prgrms/kdtspringdemo/customer/repository/JdbcCustomerRepositoryTest.java @@ -5,6 +5,7 @@ import org.prgrms.kdtspringdemo.customer.domain.Customer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -23,6 +24,7 @@ import static org.hamcrest.Matchers.*; @SpringJUnitConfig +@EnableAutoConfiguration @ActiveProfiles("DB") class JdbcCustomerRepositoryTest { @Configuration @@ -56,7 +58,7 @@ void init() { @DisplayName("데이터베이스에 고객을 추가합니다.") void insert() { //given - Customer customer = new Customer(UUID.randomUUID(), "tester01", true); + Customer customer = new Customer(UUID.randomUUID(), "eugene", true); //when Customer insertCustomer = jdbcCustomerRepository.insert(customer); diff --git a/src/test/java/org/prgrms/kdtspringdemo/customer/service/CustomerServiceTest.java b/src/test/java/org/prgrms/kdtspringdemo/customer/service/CustomerServiceTest.java new file mode 100644 index 0000000000..dbeb2cf15b --- /dev/null +++ b/src/test/java/org/prgrms/kdtspringdemo/customer/service/CustomerServiceTest.java @@ -0,0 +1,69 @@ +package org.prgrms.kdtspringdemo.customer.service; + +import com.zaxxer.hikari.HikariDataSource; +import org.junit.jupiter.api.Test; +import org.prgrms.kdtspringdemo.customer.domain.Customer; +import org.prgrms.kdtspringdemo.customer.repository.CustomerRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import javax.sql.DataSource; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +@SpringJUnitConfig +@ActiveProfiles("DB") +class CustomerServiceTest { + + @Configuration + @ComponentScan(basePackages = {"org.prgrms.kdtspringdemo.customer"}) + static class Config { + @Bean + public DataSource dataSource() { + return DataSourceBuilder.create() + .url("jdbc:mysql://localhost/kdt") + .username("test") + .password("test1234!") + .type(HikariDataSource.class) + .build(); + } + + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + } + @Autowired + private CustomerService customerService; + + @Test + void insert() { + //given + Customer customer = new Customer(UUID.randomUUID(), "eugene2", false); + + //when + Customer insertCustomer = customerService.insert(customer); + + //then + assertThat(customer.getCustomerId(), is(insertCustomer.getCustomerId())); + } + + @Test + void findAll() { + } + + @Test + void getBlackListCustomers() { + } +} \ No newline at end of file diff --git a/src/test/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepositoryTest.java b/src/test/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepositoryTest.java index a4cf191379..9326b3e87d 100644 --- a/src/test/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepositoryTest.java +++ b/src/test/java/org/prgrms/kdtspringdemo/voucher/repository/FileVoucherRepositoryTest.java @@ -1,5 +1,6 @@ package org.prgrms.kdtspringdemo.voucher.repository; +import com.zaxxer.hikari.HikariDataSource; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import org.junit.jupiter.api.*; @@ -9,12 +10,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import javax.sql.DataSource; import java.io.FileWriter; import java.io.IOException; import java.util.List; @@ -24,11 +29,11 @@ import static org.hamcrest.Matchers.*; @SpringJUnitConfig -@ActiveProfiles("dev") +@ActiveProfiles({"dev"}) class FileVoucherRepositoryTest { @Configuration - @ComponentScan() + @ComponentScan({"org,prgrms.kdtspringdemo.voucher"}) static class Config { } @Autowired @@ -38,7 +43,7 @@ static class Config { @BeforeEach - void init() throws IOException { + void init() { try(FileWriter fileWriter = new FileWriter(filePath); CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader("voucherId", "amount", "voucherType"));) { csvPrinter.printRecord(UUID.randomUUID().toString(), 100, "fixeddiscount"); diff --git a/src/test/java/org/prgrms/kdtspringdemo/voucher/service/VoucherServiceTest.java b/src/test/java/org/prgrms/kdtspringdemo/voucher/service/VoucherServiceTest.java index f721c120fb..d262aafa32 100644 --- a/src/test/java/org/prgrms/kdtspringdemo/voucher/service/VoucherServiceTest.java +++ b/src/test/java/org/prgrms/kdtspringdemo/voucher/service/VoucherServiceTest.java @@ -1,55 +1,39 @@ package org.prgrms.kdtspringdemo.voucher.service; -import org.apache.commons.csv.CSVFormat; -import org.apache.commons.csv.CSVPrinter; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.prgrms.kdtspringdemo.dto.VoucherViewDto; import org.prgrms.kdtspringdemo.voucher.domain.PercentDiscountPolicy; import org.prgrms.kdtspringdemo.voucher.domain.Voucher; -import org.prgrms.kdtspringdemo.voucher.domain.VoucherPolicy; import org.prgrms.kdtspringdemo.voucher.domain.VoucherTypeFunction; -import org.prgrms.kdtspringdemo.voucher.repository.FileVoucherRepository; -import org.prgrms.kdtspringdemo.voucher.repository.VoucherRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.event.annotation.BeforeTestClass; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import java.io.FileWriter; -import java.io.IOException; import java.util.List; import java.util.NoSuchElementException; -import java.util.UUID; -import static org.junit.jupiter.api.Assertions.*; -import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertThrows; @SpringJUnitConfig -@ActiveProfiles("local") +@ActiveProfiles("DB") class VoucherServiceTest { - @Configuration - @ComponentScan(basePackages = {"org.prgrms.kdtspringdemo"}) - static class Config { - } - - @Autowired - private VoucherService voucherService; private final String filePath = "src/test/resources/test_voucherList"; private final Logger logger = LoggerFactory.getLogger(VoucherServiceTest.class); + @Autowired + private VoucherService voucherService; @BeforeEach void init() { - try(FileWriter fileWriter = new FileWriter(filePath); - CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader("voucherId", "amount", "voucherType"));) { - csvPrinter.printRecord(UUID.randomUUID().toString(), 100, "fixeddiscount"); - } catch (IOException e) { - logger.error(e.getMessage()); - } + voucherService.deleteAll(); } @Test @@ -80,14 +64,13 @@ void getVoucherTypeFunctionError() { void createVoucher() { //given VoucherTypeFunction voucherType = VoucherTypeFunction.PERCENT_DISCOUNT_POLICY; - UUID voucherId = UUID.randomUUID(); long amount = 20L; //when - Voucher createdVoucher = voucherService.createVoucher(voucherType, voucherId, amount); + VoucherViewDto createdVoucher = voucherService.createVoucher(voucherType, amount); //then - assertThat(createdVoucher.getVoucherId(), is(voucherId)); + //assertThat(createdVoucher.getVoucherId(), is(voucherId)); assertThat(createdVoucher.getVoucherPolicy(), instanceOf(PercentDiscountPolicy.class)); } @@ -95,11 +78,11 @@ void createVoucher() { @DisplayName("바우처 리스트를 반환합니다.") void getVoucherList() { //given - voucherService.createVoucher(VoucherTypeFunction.PERCENT_DISCOUNT_POLICY, UUID.randomUUID(), 20L); - voucherService.createVoucher(VoucherTypeFunction.FIXED_DISCOUNT_POLICY, UUID.randomUUID(), 400L); + voucherService.createVoucher(VoucherTypeFunction.PERCENT_DISCOUNT_POLICY, 20L); + voucherService.createVoucher(VoucherTypeFunction.FIXED_DISCOUNT_POLICY, 400L); //when - List voucherList = voucherService.findAll().get(); + List voucherList = voucherService.getVoucherViewDtoList(); //then assertThat(voucherList.size(), is(2)); @@ -109,7 +92,7 @@ void getVoucherList() { @DisplayName("voucherId 로 바우처를 반환합니다.") void getVoucher() { //given - Voucher createdVoucher = voucherService.createVoucher(VoucherTypeFunction.FIXED_DISCOUNT_POLICY, UUID.randomUUID(), 2000L); + VoucherViewDto createdVoucher = voucherService.createVoucher(VoucherTypeFunction.FIXED_DISCOUNT_POLICY, 2000L); //when Voucher voucher = voucherService.findById(createdVoucher.getVoucherId()); @@ -118,4 +101,9 @@ void getVoucher() { assertThat(createdVoucher.getVoucherId(), is(voucher.getVoucherId())); assertThat(createdVoucher.getVoucherPolicy().getClass(), sameInstance(voucher.getVoucherPolicy().getClass())); } + + @Configuration + @ComponentScan(basePackages = {"org.prgrms.kdtspringdemo"}) + static class Config { + } } \ No newline at end of file diff --git a/src/test/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepositoryTest.java b/src/test/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepositoryTest.java index 4a39a48ed1..876f239dfe 100644 --- a/src/test/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepositoryTest.java +++ b/src/test/java/org/prgrms/kdtspringdemo/wallet/repository/JdbcWalletRepositoryTest.java @@ -1,10 +1,20 @@ package org.prgrms.kdtspringdemo.wallet.repository; import com.zaxxer.hikari.HikariDataSource; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.prgrms.kdtspringdemo.customer.CustomerFunction; import org.prgrms.kdtspringdemo.customer.domain.Customer; +import org.prgrms.kdtspringdemo.customer.repository.CustomerRepository; import org.prgrms.kdtspringdemo.customer.repository.JdbcCustomerRepository; +import org.prgrms.kdtspringdemo.voucher.domain.FixedDiscountPolicy; +import org.prgrms.kdtspringdemo.voucher.domain.Voucher; +import org.prgrms.kdtspringdemo.voucher.repository.JdbcVoucherRepository; +import org.prgrms.kdtspringdemo.voucher.repository.VoucherRepository; import org.prgrms.kdtspringdemo.wallet.domain.Wallet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; @@ -19,15 +29,15 @@ import java.util.UUID; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; @SpringJUnitConfig -@ActiveProfiles({"DB"}) +@ActiveProfiles("DB") class JdbcWalletRepositoryTest { + @Configuration - @ComponentScan(basePackages = {"org.prgrms.kdtspringdemo.wallet","org.prgrms.kdtspringdemo.customer"}) + @ComponentScan(basePackages = "org.prgrms.kdtspringdemo") static class Config { @Bean public DataSource dataSource() { @@ -44,20 +54,35 @@ public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } } + @Autowired JdbcWalletRepository jdbcWalletRepository; - + @Autowired + JdbcVoucherRepository jdbcVoucherRepository; @Autowired JdbcCustomerRepository jdbcCustomerRepository; + private final Logger logger = LoggerFactory.getLogger(JdbcWalletRepositoryTest.class); + Customer insertCustomer; + Voucher insertVoucher; + + @BeforeEach + void init() { + jdbcWalletRepository.deleteAll(); + jdbcCustomerRepository.deleteAll(); + Customer customer = new Customer(UUID.randomUUID(), "tester01", true); + jdbcCustomerRepository.insert(customer); + insertCustomer = customer; + Voucher voucher = new Voucher(UUID.randomUUID(), new FixedDiscountPolicy(1000L)); + jdbcVoucherRepository.insert(voucher); + insertVoucher = voucher; + } + @Test + @DisplayName("지갑을 추가합니다.") void insert() { //given - Customer customer = new Customer(UUID.randomUUID(), "tester01", true); - jdbcCustomerRepository.insert(customer); - List vouchers = new ArrayList<>(); - vouchers.add(UUID.randomUUID()); - Wallet wallet = new Wallet(UUID.randomUUID(), customer.getCustomerId(), vouchers); + Wallet wallet = new Wallet(UUID.randomUUID(), insertCustomer.getCustomerId()); //when Wallet insertWallet = jdbcWalletRepository.insert(wallet); @@ -67,14 +92,59 @@ void insert() { } @Test - void findAllVoucherByCustomerId() { + @DisplayName("walletId로 지갑을 검색합니다.") + void findWalletByWalletId() { + //given + Wallet wallet = jdbcWalletRepository.insert(new Wallet(UUID.randomUUID(), insertCustomer.getCustomerId())); + + //when + Wallet findWallet = jdbcWalletRepository.findById(wallet.getWalletId()).get(); + + //then + assertThat(findWallet.getCustomerId(), is(wallet.getCustomerId())); } @Test + @DisplayName("customerId로 voucherId 목록을 조회합니다.") + void findVouchersByCustomerId() { + //given + List vouchers = new ArrayList<>(); + vouchers.add(UUID.randomUUID()); + jdbcWalletRepository.insert(new Wallet(UUID.randomUUID(), insertCustomer.getCustomerId())); + + //when + List voucherList = jdbcWalletRepository.findVouchersByCustomerId(insertCustomer.getCustomerId()); + + //then + assertThat(voucherList, samePropertyValuesAs(vouchers)); + } + + @Test + @DisplayName("wallet안의 voucherId") void deleteVoucherByVoucherId() { + //given + UUID voucherId = UUID.randomUUID(); + Wallet insertWallet = jdbcWalletRepository.insert(new Wallet(UUID.randomUUID(), insertCustomer.getCustomerId())); + + //when + jdbcWalletRepository.deleteVoucherByVoucherId(insertCustomer.getCustomerId(), voucherId); + + //then } @Test + @DisplayName("해당 voucher를 가진 customer 조회") void findCustomerByVoucherId() { + //given + //Customer customer2 = jdbcCustomerRepository.insert(new Customer(UUID.randomUUID(), "tester02", false)); + + jdbcWalletRepository.insert(new Wallet(UUID.randomUUID(), insertCustomer.getCustomerId())); + //jdbcWalletRepository.insert(new Wallet(UUID.randomUUID(), customer2.getCustomerId())); + + //when + Customer findCustomers = jdbcWalletRepository.findCustomerByVoucherId(insertVoucher.getVoucherId()).get(); + + //then + //assertThat(findCustomers.size(), is(2)); } } \ No newline at end of file diff --git a/src/test/resources/test_voucherList b/src/test/resources/test_voucherList index 181e823d8f..2b1f14f953 100644 --- a/src/test/resources/test_voucherList +++ b/src/test/resources/test_voucherList @@ -1,2 +1,2 @@ voucherId,amount,voucherType -e25c52e3-434d-47ee-82d4-869222fd99aa,100,fixeddiscount +42d5bd64-c37f-4a4c-8dfb-af3956f5b342,100,fixeddiscount