diff --git a/voucher-manager/build.gradle b/voucher-manager/build.gradle index 3912a40870..0a5abced84 100644 --- a/voucher-manager/build.gradle +++ b/voucher-manager/build.gradle @@ -23,6 +23,8 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-jdbc' + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.beryx:text-io:3.4.1' testImplementation 'org.assertj:assertj-core:3.24.2' diff --git "a/voucher-manager/doc/\355\235\220\353\246\204\353\217\204.png" "b/voucher-manager/doc/\355\235\220\353\246\204\353\217\204.png" deleted file mode 100644 index 75c19cc637..0000000000 Binary files "a/voucher-manager/doc/\355\235\220\353\246\204\353\217\204.png" and /dev/null differ diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/VoucherManagerCommandLineRunner.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/VoucherManagerCommandLineRunner.java deleted file mode 100644 index fa5513d6f6..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/VoucherManagerCommandLineRunner.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.wonu606.vouchermanager; - -import com.wonu606.vouchermanager.consoleInterface.CustomerConsoleInterface; -import com.wonu606.vouchermanager.consoleInterface.FrontConsoleInterface; -import com.wonu606.vouchermanager.consoleInterface.VoucherConsoleInterface; -import org.springframework.boot.CommandLineRunner; -import org.springframework.stereotype.Component; - -@Component -public class VoucherManagerCommandLineRunner implements CommandLineRunner { - private final FrontConsoleInterface frontConsoleInterface; - - public VoucherManagerCommandLineRunner(FrontConsoleInterface frontConsoleInterface) { - this.frontConsoleInterface = frontConsoleInterface; - } - - @Override - public void run(String... args) { - frontConsoleInterface.run(); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/AbstractConsoleIO.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/AbstractConsoleIo.java similarity index 75% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/io/AbstractConsoleIO.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/console/AbstractConsoleIo.java index 595d30a5dd..aec2bba410 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/AbstractConsoleIO.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/AbstractConsoleIo.java @@ -1,13 +1,11 @@ -package com.wonu606.vouchermanager.io; +package com.wonu606.vouchermanager.console; -import com.wonu606.vouchermanager.menu.VoucherMenu; - -public abstract class AbstractConsoleIO { +public abstract class AbstractConsoleIo { protected final ConsoleInput input; protected final ConsolePrinter printer; - public AbstractConsoleIO(ConsoleInput input, ConsolePrinter printer) { + public AbstractConsoleIo(ConsoleInput input, ConsolePrinter printer) { this.input = input; this.printer = printer; } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/ConsoleInput.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/ConsoleInput.java similarity index 94% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/io/ConsoleInput.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/console/ConsoleInput.java index 5d6bc0215e..36124e50da 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/ConsoleInput.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/ConsoleInput.java @@ -1,4 +1,4 @@ -package com.wonu606.vouchermanager.io; +package com.wonu606.vouchermanager.console; import java.util.List; import org.beryx.textio.TextIO; diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/ConsolePrinter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/ConsolePrinter.java similarity index 90% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/io/ConsolePrinter.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/console/ConsolePrinter.java index 403e4e686a..3aa67cb3c3 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/ConsolePrinter.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/ConsolePrinter.java @@ -1,4 +1,4 @@ -package com.wonu606.vouchermanager.io; +package com.wonu606.vouchermanager.console; import org.beryx.textio.TextIoFactory; import org.beryx.textio.TextTerminal; diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/VoucherManagerCommandLineRunner.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/VoucherManagerCommandLineRunner.java new file mode 100644 index 0000000000..fccc112ec4 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/VoucherManagerCommandLineRunner.java @@ -0,0 +1,19 @@ +package com.wonu606.vouchermanager.console; + +import com.wonu606.vouchermanager.console.cableadapter.FrontCableAdapter; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class VoucherManagerCommandLineRunner implements CommandLineRunner { + private final FrontCableAdapter frontCableAdapter; + + public VoucherManagerCommandLineRunner(FrontCableAdapter frontCableAdapter) { + this.frontCableAdapter = frontCableAdapter; + } + + @Override + public void run(String... args) { + frontCableAdapter.run(); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/ConsoleMenu.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/ConsoleMenu.java similarity index 93% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/ConsoleMenu.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/ConsoleMenu.java index b298f3092e..5f4aa0efc6 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/ConsoleMenu.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/ConsoleMenu.java @@ -1,4 +1,4 @@ -package com.wonu606.vouchermanager.menu; +package com.wonu606.vouchermanager.console.cableadapter; import java.util.Arrays; import java.util.List; diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/FrontCableAdapter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/FrontCableAdapter.java new file mode 100644 index 0000000000..c046f7d89d --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/FrontCableAdapter.java @@ -0,0 +1,58 @@ +package com.wonu606.vouchermanager.console.cableadapter; + +import com.wonu606.vouchermanager.console.controllercable.customer.CustomerControllerCable; +import com.wonu606.vouchermanager.console.controllercable.voucher.VoucherControllerCable; +import org.springframework.stereotype.Component; + +@Component +public class FrontCableAdapter { + + private final CustomerControllerCable customerControllerCable; + private final VoucherControllerCable voucherControllerCable; + private final FrontConsoleIo consoleIo; + + public FrontCableAdapter(CustomerControllerCable customerControllerCable, + VoucherControllerCable voucherControllerCable, FrontConsoleIo consoleIo) { + this.customerControllerCable = customerControllerCable; + this.voucherControllerCable = voucherControllerCable; + this.consoleIo = consoleIo; + } + + public void run() { + ConsoleMenu menu = ConsoleMenu.START; + while (menu.isNotExit()) { + try { + menu = consoleIo.selectMenu(); + executeMenuAction(menu); + } catch (Exception exception) { + consoleIo.displayMessage(exception.getMessage()); + } + } + terminal(); + } + + private void executeMenuAction(ConsoleMenu menu) { + switch (menu) { + case EXIT: + return; + case CUSTOMER: + customerControllerCable.run(); + return; + case VOUCHER: + voucherControllerCable.run(); + return; + + default: + throw new IllegalArgumentException("수행할 수 없는 메뉴입니다."); + } + } + + private void terminal() { + consoleIo.displayMessage("곧 프로그램을 종료합니다."); + try { + Thread.sleep(3000); + } catch (InterruptedException ignored) { + } + consoleIo.terminal(); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/FrontConsoleIO.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/FrontConsoleIo.java similarity index 61% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/io/FrontConsoleIO.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/FrontConsoleIo.java index d63feb258e..2d191d2417 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/FrontConsoleIO.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/cableadapter/FrontConsoleIo.java @@ -1,12 +1,14 @@ -package com.wonu606.vouchermanager.io; +package com.wonu606.vouchermanager.console.cableadapter; -import com.wonu606.vouchermanager.menu.ConsoleMenu; +import com.wonu606.vouchermanager.console.AbstractConsoleIo; +import com.wonu606.vouchermanager.console.ConsoleInput; +import com.wonu606.vouchermanager.console.ConsolePrinter; import org.springframework.stereotype.Component; @Component -public class FrontConsoleIO extends AbstractConsoleIO { +public class FrontConsoleIo extends AbstractConsoleIo { - public FrontConsoleIO(ConsoleInput input, ConsolePrinter printer) { + public FrontConsoleIo(ConsoleInput input, ConsolePrinter printer) { super(input, printer); } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/CustomerControllerCable.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/CustomerControllerCable.java new file mode 100644 index 0000000000..71e40a0f8c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/CustomerControllerCable.java @@ -0,0 +1,75 @@ +package com.wonu606.vouchermanager.console.controllercable.customer; + +import com.wonu606.vouchermanager.console.controllercable.customer.io.CustomerConsoleIo; +import com.wonu606.vouchermanager.controller.customer.CustomerController; +import com.wonu606.vouchermanager.controller.customer.request.CustomerCreateRequest; +import com.wonu606.vouchermanager.controller.customer.request.OwnedVouchersRequest; +import com.wonu606.vouchermanager.controller.customer.request.WalletDeleteRequest; +import org.springframework.stereotype.Component; + +@Component +public class CustomerControllerCable { + + private final CustomerConsoleIo consoleIo; + private final CustomerController controller; + + public CustomerControllerCable(CustomerConsoleIo consoleIo, + CustomerController controller) { + this.consoleIo = consoleIo; + this.controller = controller; + } + + public void run() { + CustomerControllerMenu menu = CustomerControllerMenu.START; + while (menu.isNotExit()) { + try { + menu = consoleIo.selectMenu(); + executeMenuAction(menu); + } catch (Exception exception) { + consoleIo.displayMessage(exception.getMessage()); + } + } + } + + private void executeMenuAction(CustomerControllerMenu menu) { + switch (menu) { + case EXIT: + return; + case CREATE: + CustomerCreateRequest customerCreateRequest = createCustomerCreateRequest(); + controller.createCustomer(customerCreateRequest); + return; + + case LIST: + String customerListResponse = controller.getCustomerList(); + consoleIo.displayCustomerList(customerListResponse); + return; + + case VOUCHER_LIST: + String searchedCustomerId = consoleIo.readString("CustomerId"); + String ownedVoucherResponse = controller.getOwnedVouchersByCustomer( + new OwnedVouchersRequest(searchedCustomerId)); + consoleIo.displayVoucherList(ownedVoucherResponse); + return; + + case DELETE: + String deletionCustomerId = consoleIo.readString( + "CustomerId To be Deleted"); + String deletionVoucherId = consoleIo.readString( + "VoucherId To be Deleted" + ); + controller.deleteWallet( + new WalletDeleteRequest(deletionVoucherId, deletionCustomerId)); + return; + + default: + throw new IllegalArgumentException("수행할 수 없는 메뉴입니다."); + } + } + + private CustomerCreateRequest createCustomerCreateRequest() { + String creationEmail = consoleIo.readString("Email"); + String creationNickName = consoleIo.readString("Nickname"); + return new CustomerCreateRequest(creationEmail, creationNickName); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/CustomerMenu.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/CustomerControllerMenu.java similarity index 80% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/CustomerMenu.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/CustomerControllerMenu.java index 3e79818567..b588ba821a 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/CustomerMenu.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/CustomerControllerMenu.java @@ -1,11 +1,11 @@ -package com.wonu606.vouchermanager.menu; +package com.wonu606.vouchermanager.console.controllercable.customer; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -public enum CustomerMenu { +public enum CustomerControllerMenu { START, EXIT, CREATE, @@ -13,7 +13,7 @@ public enum CustomerMenu { VOUCHER_LIST, DELETE; - public static CustomerMenu getTypeByName(String name) { + public static CustomerControllerMenu getTypeByName(String name) { return Arrays.stream(values()) .filter(c -> Objects.equals(c.name(), name.toUpperCase())) .findFirst() diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/io/CustomerConsoleIo.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/io/CustomerConsoleIo.java new file mode 100644 index 0000000000..bd5899d1fd --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/customer/io/CustomerConsoleIo.java @@ -0,0 +1,38 @@ +package com.wonu606.vouchermanager.console.controllercable.customer.io; + +import com.wonu606.vouchermanager.console.AbstractConsoleIo; +import com.wonu606.vouchermanager.console.ConsoleInput; +import com.wonu606.vouchermanager.console.ConsolePrinter; +import com.wonu606.vouchermanager.console.controllercable.customer.CustomerControllerMenu; +import org.springframework.stereotype.Component; + +@Component +public class CustomerConsoleIo extends AbstractConsoleIo { + + public CustomerConsoleIo(ConsoleInput input, ConsolePrinter printer) { + super(input, printer); + } + + @Override + public CustomerControllerMenu selectMenu() { + displayMenu(); + String menuSelection = input.readString(CustomerControllerMenu.getAllNames(), "Menu"); + return CustomerControllerMenu.getTypeByName(menuSelection); + } + + protected void displayMenu() { + String lineFormat = "Type %s to %s the program.\n"; + CustomerControllerMenu.getAllNames().forEach(n -> + printer.displayMessage(String.format(lineFormat, n, n))); + } + + public void displayCustomerList(String response) { + printer.displayMessage("=== 보유한 Customer 리스트 ==="); + printer.displayMessage(response); + } + + public void displayVoucherList(String response) { + printer.displayMessage("=== 보유한 바우처 리스트 ==="); + printer.displayMessage(response); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/VoucherControllerCable.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/VoucherControllerCable.java new file mode 100644 index 0000000000..27d216e77b --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/VoucherControllerCable.java @@ -0,0 +1,75 @@ +package com.wonu606.vouchermanager.console.controllercable.voucher; + +import com.wonu606.vouchermanager.console.controllercable.voucher.io.VoucherConsoleIo; +import com.wonu606.vouchermanager.controller.voucherwallet.VoucherWalletController; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.VoucherCreateRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.OwnedCustomersRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.WalletAssignRequest; +import org.springframework.stereotype.Component; + +@Component +public class VoucherControllerCable { + + private final VoucherConsoleIo consoleIo; + private final VoucherWalletController controller; + + public VoucherControllerCable(VoucherConsoleIo consoleIo, + VoucherWalletController controller) { + this.consoleIo = consoleIo; + this.controller = controller; + } + + public void run() { + VoucherControllerMenu menu = VoucherControllerMenu.START; + while (menu.isNotExit()) { + try { + menu = consoleIo.selectMenu(); + executeMenuAction(menu); + } catch (Exception exception) { + consoleIo.displayMessage(exception.getMessage()); + } + } + } + + private void executeMenuAction(VoucherControllerMenu menu) { + switch (menu) { + case EXIT: + return; + + case LIST: + String voucherResponse = controller.getVoucherList(); + consoleIo.displayVoucherList(voucherResponse); + return; + + case CREATE: + VoucherCreateRequest voucherCreateRequest = createVoucherCreateRequest(); + controller.createVoucher(voucherCreateRequest); + return; + case ASSIGN: + WalletAssignRequest voucherWalletCreateRequest = createVoucherWalletCreateRequest(); + controller.assignWallet(voucherWalletCreateRequest); + return; + + case CUSTOMER_LIST: + String voucherId = consoleIo.readString("VoucherId"); + String ownedCustomerResponse = controller.getOwnedCustomersByVoucher( + new OwnedCustomersRequest(voucherId)); + consoleIo.displayCustomerList(ownedCustomerResponse); + return; + + default: + throw new IllegalArgumentException("수행할 수 없는 메뉴입니다."); + } + } + + private WalletAssignRequest createVoucherWalletCreateRequest() { + String uuid = consoleIo.readString("Voucher UUID"); + return new WalletAssignRequest(uuid); + } + + private VoucherCreateRequest createVoucherCreateRequest() { + String type = consoleIo.selectVoucherType(); + double discountValue = consoleIo.readDouble("discount"); + return new VoucherCreateRequest(type, discountValue); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/VoucherMenu.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/VoucherControllerMenu.java similarity index 80% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/VoucherMenu.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/VoucherControllerMenu.java index 72b3467c39..c69c0b3ff6 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/menu/VoucherMenu.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/VoucherControllerMenu.java @@ -1,11 +1,11 @@ -package com.wonu606.vouchermanager.menu; +package com.wonu606.vouchermanager.console.controllercable.voucher; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -public enum VoucherMenu { +public enum VoucherControllerMenu { START, EXIT, @@ -14,7 +14,7 @@ public enum VoucherMenu { ASSIGN, CUSTOMER_LIST; - public static VoucherMenu getTypeByName(String name) { + public static VoucherControllerMenu getTypeByName(String name) { return Arrays.stream(values()) .filter(v -> Objects.equals(v.name(), name.toUpperCase())) .findFirst() diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/io/VoucherConsoleIo.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/io/VoucherConsoleIo.java new file mode 100644 index 0000000000..185c366963 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/io/VoucherConsoleIo.java @@ -0,0 +1,49 @@ +package com.wonu606.vouchermanager.console.controllercable.voucher.io; + +import com.wonu606.vouchermanager.console.AbstractConsoleIo; +import com.wonu606.vouchermanager.console.ConsoleInput; +import com.wonu606.vouchermanager.console.ConsolePrinter; +import com.wonu606.vouchermanager.console.controllercable.voucher.VoucherControllerMenu; +import org.springframework.stereotype.Component; + +@Component +public class VoucherConsoleIo extends AbstractConsoleIo { + + public VoucherConsoleIo(ConsoleInput input, ConsolePrinter printer) { + super(input, printer); + } + + @Override + public VoucherControllerMenu selectMenu() { + displayMenu(); + String menuSelection = input.readString(VoucherControllerMenu.getAllNames(), "Menu"); + return VoucherControllerMenu.getTypeByName(menuSelection); + } + + public String selectVoucherType() { + displayVoucherTypes(); + return input.readString(VoucherConsoleType.getAllNames(), "Type"); + } + + public void displayVoucherList(String voucherList) { + printer.displayMessage("=== 바우처 리스트 ==="); + printer.displayMessage(voucherList); + } + + public void displayCustomerList(String response) { + printer.displayMessage("=== 보유한 Customer 리스트 ==="); + printer.displayMessage(response); + } + + protected void displayMenu() { + String lineFormat = "Type %s to %s the program.\n"; + VoucherControllerMenu.getAllNames().forEach(n -> + printer.displayMessage(String.format(lineFormat, n, n))); + } + + private void displayVoucherTypes() { + printer.displayMessage("=== 바우처 타입 ==="); + VoucherConsoleType.getAllNames() + .forEach(n -> printer.displayMessage("[" + n + "]")); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/io/VoucherConsoleType.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/io/VoucherConsoleType.java new file mode 100644 index 0000000000..1e2176cb67 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/console/controllercable/voucher/io/VoucherConsoleType.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.console.controllercable.voucher.io; + +import com.wonu606.vouchermanager.service.voucher.factory.VoucherType; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public enum VoucherConsoleType { + FIXED, + PERCENT; + + public static VoucherType getVoucherTypeByName(String name) { + return Arrays.stream(VoucherType.values()) + .filter(v -> v.name().equals(name.toUpperCase())) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 바우처 타입입니다.")); + } + + public static List getAllNames() { + return Arrays.stream(VoucherType.values()) + .map(v -> v.name().toLowerCase()) + .collect(Collectors.toList()); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/CustomerConsoleInterface.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/CustomerConsoleInterface.java deleted file mode 100644 index 495ec38393..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/CustomerConsoleInterface.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.wonu606.vouchermanager.consoleInterface; - -import com.wonu606.vouchermanager.controller.CustomerController; -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerDto; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.io.CustomerConsoleIO; -import com.wonu606.vouchermanager.menu.CustomerMenu; -import java.util.List; -import org.springframework.stereotype.Component; - -@Component -public class CustomerConsoleInterface { - - private final CustomerConsoleIO customerConsoleIO; - private final CustomerController customerController; - - public CustomerConsoleInterface(CustomerConsoleIO customerConsoleIO, - CustomerController customerController) { - this.customerConsoleIO = customerConsoleIO; - this.customerController = customerController; - } - - public void run() { - CustomerMenu menu = CustomerMenu.START; - while (menu.isNotExit()) { - try { - menu = customerConsoleIO.selectMenu(); - executeMenuAction(menu); - } catch (Exception exception) { - customerConsoleIO.displayMessage(exception.getMessage()); - } - } - } - - private void executeMenuAction(CustomerMenu menu) { - switch (menu) { - case EXIT: - return; - case CREATE: - CustomerDto customerDtoToCreate = createCustomerDto(); - customerController.createCustomer(customerDtoToCreate); - return; - - case LIST: - List customerList = customerController.getCustomerList(); - customerConsoleIO.displayCustomerList(customerList); - return; - - case VOUCHER_LIST: - String emailAddress = customerConsoleIO.readString("Email Address"); - List voucherListOwned = - customerController.getVouchersOwnedByCustomer(emailAddress); - customerConsoleIO.displayVoucherList(voucherListOwned); - return; - - case DELETE: - String emailAddressToDelete = customerConsoleIO.readString( - "Email Address To be Deleted"); - String voucherIdToDelete = customerConsoleIO.readString( - "Voucher UUID To be Deleted" - ); - customerController.deleteWallet(emailAddressToDelete, voucherIdToDelete); - return; - - default: - throw new IllegalArgumentException("수행할 수 없는 메뉴입니다."); - } - } - - private CustomerDto createCustomerDto() { - String emailAddress = customerConsoleIO.readString("Email Address"); - String nickName = customerConsoleIO.readString("Nickname"); - return new CustomerDto(emailAddress, nickName); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/FrontConsoleInterface.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/FrontConsoleInterface.java deleted file mode 100644 index b336a0c86f..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/FrontConsoleInterface.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.wonu606.vouchermanager.consoleInterface; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerDto; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.io.FrontConsoleIO; -import com.wonu606.vouchermanager.menu.ConsoleMenu; -import com.wonu606.vouchermanager.menu.CustomerMenu; -import java.util.List; -import org.springframework.stereotype.Component; - -@Component -public class FrontConsoleInterface { - - private final CustomerConsoleInterface customerConsoleInterface; - private final VoucherConsoleInterface voucherConsoleInterface; - private final FrontConsoleIO frontConsoleIO; - - public FrontConsoleInterface(CustomerConsoleInterface customerConsoleInterface, - VoucherConsoleInterface voucherConsoleInterface, FrontConsoleIO frontConsoleIO) { - this.customerConsoleInterface = customerConsoleInterface; - this.voucherConsoleInterface = voucherConsoleInterface; - this.frontConsoleIO = frontConsoleIO; - } - - public void run() { - ConsoleMenu menu = ConsoleMenu.START; - while (menu.isNotExit()) { - try { - menu = frontConsoleIO.selectMenu(); - executeMenuAction(menu); - } catch (Exception exception) { - frontConsoleIO.displayMessage(exception.getMessage()); - } - } - terminal(); - } - - private void executeMenuAction(ConsoleMenu menu) { - switch (menu) { - case EXIT: - return; - case CUSTOMER: - customerConsoleInterface.run(); - return; - case VOUCHER: - voucherConsoleInterface.run(); - return; - - default: - throw new IllegalArgumentException("수행할 수 없는 메뉴입니다."); - } - } - - private void terminal() { - frontConsoleIO.displayMessage("곧 프로그램을 종료합니다."); - try { - Thread.sleep(3000); - } catch (InterruptedException ignored) { - } - frontConsoleIO.terminal(); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/VoucherConsoleInterface.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/VoucherConsoleInterface.java deleted file mode 100644 index daf085c9ef..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/consoleInterface/VoucherConsoleInterface.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.wonu606.vouchermanager.consoleInterface; - -import com.wonu606.vouchermanager.controller.VoucherController; -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWalletDto; -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherDto; -import com.wonu606.vouchermanager.io.VoucherConsoleIO; -import com.wonu606.vouchermanager.menu.VoucherMenu; -import java.util.List; -import java.util.UUID; -import org.springframework.stereotype.Component; - -@Component -public class VoucherConsoleInterface { - - private final VoucherConsoleIO voucherConsoleIO; - private final VoucherController controller; - - public VoucherConsoleInterface(VoucherConsoleIO voucherConsoleIO, - VoucherController controller) { - this.voucherConsoleIO = voucherConsoleIO; - this.controller = controller; - } - - public void run() { - VoucherMenu menu = VoucherMenu.START; - while (menu.isNotExit()) { - try { - menu = voucherConsoleIO.selectMenu(); - executeMenuAction(menu); - } catch (Exception exception) { - voucherConsoleIO.displayMessage(exception.getMessage()); - } - } - } - - private void executeMenuAction(VoucherMenu menu) { - switch (menu) { - case EXIT: - return; - - case LIST: - List voucherList = controller.getVoucherList(); - voucherConsoleIO.displayVoucherList(voucherList); - return; - - case CREATE: - VoucherDto voucherDto = createVoucherDto(); - controller.createVoucher(voucherDto); - return; - case ASSIGN: - CustomerVoucherWalletDto walletDto = createWalletDto(); - controller.assignWallet(walletDto); - return; - - case CUSTOMER_LIST: - String voucherUuId = voucherConsoleIO.readString("Voucher UUID"); - List customerList = controller.getCustomersOwnedByVoucherId(voucherUuId); - voucherConsoleIO.displayCustomerList(customerList); - return; - - default: - throw new IllegalArgumentException("수행할 수 없는 메뉴입니다."); - } - } - - private CustomerVoucherWalletDto createWalletDto() { - String uuid = voucherConsoleIO.readString("Voucher UUID"); - String emailAddress = voucherConsoleIO.readString("Customer EmailAddress"); - return new CustomerVoucherWalletDto(UUID.fromString(uuid), emailAddress); - } - - private VoucherDto createVoucherDto() { - String type = voucherConsoleIO.selectVoucherType(); - double discountValue = voucherConsoleIO.readDouble("discount"); - return new VoucherDto(type, discountValue); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/CustomerController.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/CustomerController.java deleted file mode 100644 index 7fe71a16f9..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/CustomerController.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.wonu606.vouchermanager.controller; - -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWalletDto; -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerDto; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.service.customer.CustomerService; -import com.wonu606.vouchermanager.service.customerVoucherWallet.CustomerVoucherWalletService; -import com.wonu606.vouchermanager.service.voucher.VoucherService; -import java.util.List; -import java.util.UUID; -import org.springframework.stereotype.Component; - -@Component -public class CustomerController { - - private final CustomerService customerService; - private final VoucherService voucherService; - private final CustomerVoucherWalletService walletService; - - public CustomerController(CustomerService customerService, VoucherService voucherService, - CustomerVoucherWalletService walletService) { - this.customerService = customerService; - this.voucherService = voucherService; - this.walletService = walletService; - } - - public Customer createCustomer(CustomerDto customerDto) { - Customer customer = customerService.createCustomer(customerDto); - return customerService.saveCustomer(customer); - } - - public List getCustomerList() { - return customerService.getCustomerList(); - } - - public List getVouchersOwnedByCustomer(String emailAddress) { - List voucherIdList = walletService.findVoucherIdListByCustomerEmailAddress(new EmailAddress(emailAddress)); - return voucherService.getVoucherList(voucherIdList); - } - - public void deleteWallet(String emailAddress, String voucherId) { - walletService.deleteByWallet( - new CustomerVoucherWalletDto(UUID.fromString(voucherId), emailAddress)); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/VoucherController.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/VoucherController.java deleted file mode 100644 index f499debf9d..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/VoucherController.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.wonu606.vouchermanager.controller; - -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWalletDto; -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherDto; -import com.wonu606.vouchermanager.service.customer.CustomerService; -import com.wonu606.vouchermanager.service.customerVoucherWallet.CustomerVoucherWalletService; -import com.wonu606.vouchermanager.service.voucher.VoucherService; -import java.util.List; -import java.util.UUID; -import java.util.stream.Collectors; -import org.springframework.stereotype.Component; - -@Component -public class VoucherController { - - private final VoucherService voucherService; - private final CustomerService customerService; - private final CustomerVoucherWalletService walletService; - - public VoucherController(VoucherService voucherService, CustomerService customerService, - CustomerVoucherWalletService walletService) { - this.voucherService = voucherService; - this.customerService = customerService; - this.walletService = walletService; - } - - public Voucher createVoucher(VoucherDto voucherDto) { - return voucherService.createVoucher(voucherDto); - } - - public List getVoucherList() { - return voucherService.getVoucherList(); - } - - public List getCustomersOwnedByVoucherId(String voucherId) { - List customerEmailAddressList = - walletService.findEmailAddressesByVoucherId(UUID.fromString(voucherId)); - return customerService.getCustomerList( - customerEmailAddressList.stream() - .map(EmailAddress::new) - .collect(Collectors.toList())); - } - - public void assignWallet(CustomerVoucherWalletDto walletDto) { - walletService.save(walletDto); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/CustomerController.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/CustomerController.java new file mode 100644 index 0000000000..3c5d3d8c44 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/CustomerController.java @@ -0,0 +1,99 @@ +package com.wonu606.vouchermanager.controller.customer; + +import com.wonu606.vouchermanager.controller.customer.converter.CustomerControllerConverterManager; +import com.wonu606.vouchermanager.controller.customer.request.CustomerCreateRequest; +import com.wonu606.vouchermanager.controller.customer.request.OwnedVouchersRequest; +import com.wonu606.vouchermanager.controller.customer.request.WalletDeleteRequest; +import com.wonu606.vouchermanager.controller.customer.request.WalletRegisterRequest; +import com.wonu606.vouchermanager.controller.customer.response.CustomerResponse; +import com.wonu606.vouchermanager.controller.customer.response.OwnedVoucherResponse; +import com.wonu606.vouchermanager.service.customer.CustomerService; +import com.wonu606.vouchermanager.service.customer.param.CustomerCreateParam; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.service.customer.result.CustomerResult; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/customers") +public class CustomerController { + + private final CustomerService service; + private final CustomerControllerConverterManager converterManager; + + public CustomerController(CustomerService service) { + this.service = service; + converterManager = new CustomerControllerConverterManager(); + } + + @GetMapping("/create") + public String createCustomerForm() { + return "customers/create-form"; + } + + @PostMapping("/create") + public String createCustomer(@ModelAttribute CustomerCreateRequest request) { + CustomerCreateParam param = converterManager.convert(request, CustomerCreateParam.class); + service.createCustomer(param); + + return "redirect:/customers/list"; + } + + @GetMapping("/list") + public String getCustomerList(Model model) { + List results = service.getCustomerList(); + + List responses = results.stream() + .map(rs -> converterManager.convert(rs, CustomerResponse.class)) + .collect(Collectors.toList()); + + model.addAttribute("responses", responses); + return "customers/list"; + } + + @GetMapping("/owned-vouchers") + public String getOwnedVouchersByCustomerForm() { + return "customers/owned-vouchers-form"; + } + + @PostMapping("/owned-vouchers") + public String getOwnedVouchersByCustomer(@ModelAttribute OwnedVouchersRequest request, + Model model) { + OwnedVouchersParam param = converterManager.convert(request, OwnedVouchersParam.class); + List results = service.findOwnedVouchersByCustomer(param); + + List responses = results.stream() + .map(rs -> converterManager.convert(rs, OwnedVoucherResponse.class)) + .collect(Collectors.toList()); + + model.addAttribute("responses", responses); + model.addAttribute("customerId", request.getCustomerId()); // Add customer ID to the model + + return "/customers/owned-vouchers-list"; + } + + @PostMapping("/wallet/delete") + public String deleteWallet(@ModelAttribute WalletDeleteRequest request) { + WalletDeleteParam param = converterManager.convert(request, WalletDeleteParam.class); + service.deleteWallet(param); + + return "redirect:/customers/owned-vouchers"; + } + + @PostMapping("/wallet/register") + public String registerToWallet(@ModelAttribute WalletRegisterRequest request) { + WalletRegisterParam param = converterManager.convert(request, WalletRegisterParam.class); + service.registerToWallet(param); + + return "redirect:/customers/owned-vouchers"; // redirect to the owned vouchers page after registration + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/CustomerRestController.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/CustomerRestController.java new file mode 100644 index 0000000000..14230ab32a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/CustomerRestController.java @@ -0,0 +1,88 @@ +package com.wonu606.vouchermanager.controller.customer; + +import com.wonu606.vouchermanager.controller.customer.converter.CustomerControllerConverterManager; +import com.wonu606.vouchermanager.controller.customer.request.CustomerCreateRequest; +import com.wonu606.vouchermanager.controller.customer.request.OwnedVouchersRequest; +import com.wonu606.vouchermanager.controller.customer.request.WalletDeleteRequest; +import com.wonu606.vouchermanager.controller.customer.request.WalletRegisterRequest; +import com.wonu606.vouchermanager.controller.customer.response.CustomerCreateResponse; +import com.wonu606.vouchermanager.controller.customer.response.CustomerResponse; +import com.wonu606.vouchermanager.controller.customer.response.OwnedVoucherResponse; +import com.wonu606.vouchermanager.service.customer.CustomerService; +import com.wonu606.vouchermanager.service.customer.param.CustomerCreateParam; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.service.customer.result.CustomerCreateResult; +import com.wonu606.vouchermanager.service.customer.result.CustomerResult; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/api/customers", produces = MediaType.APPLICATION_JSON_VALUE) +public class CustomerRestController { + + private final CustomerService service; + private final CustomerControllerConverterManager converterManager; + + public CustomerRestController(CustomerService service) { + this.service = service; + converterManager = new CustomerControllerConverterManager(); + } + + @PostMapping("/create") + public ResponseEntity createCustomer( + @RequestBody CustomerCreateRequest request) { + CustomerCreateParam param = converterManager.convert(request, CustomerCreateParam.class); + CustomerCreateResult result = service.createCustomer(param); + + CustomerCreateResponse response = converterManager.convert(result, + CustomerCreateResponse.class); + return new ResponseEntity<>(response, HttpStatus.CREATED); + } + + @GetMapping("/list") + public ResponseEntity> getCustomerList() { + List results = service.getCustomerList(); + List responses = results.stream() + .map(rs -> converterManager.convert(rs, CustomerResponse.class)) + .collect(Collectors.toList()); + return new ResponseEntity<>(responses, HttpStatus.OK); + } + + @PostMapping("/owned-vouchers") + public ResponseEntity> getOwnedVouchersByCustomer( + @RequestBody OwnedVouchersRequest request) { + OwnedVouchersParam param = converterManager.convert(request, OwnedVouchersParam.class); + List results = service.findOwnedVouchersByCustomer(param); + + List responses = results.stream() + .map(rs -> converterManager.convert(rs, OwnedVoucherResponse.class)) + .collect(Collectors.toList()); + + return new ResponseEntity<>(responses, HttpStatus.OK); + } + + @PostMapping("/wallet/delete") + public ResponseEntity deleteWallet(@RequestBody WalletDeleteRequest request) { + WalletDeleteParam param = converterManager.convert(request, WalletDeleteParam.class); + service.deleteWallet(param); + return new ResponseEntity<>(HttpStatus.OK); + } + + @PostMapping("/wallet/register") + public ResponseEntity registerToWallet(@RequestBody WalletRegisterRequest request) { + WalletRegisterParam param = converterManager.convert(request, WalletRegisterParam.class); + service.registerToWallet(param); + return new ResponseEntity<>(HttpStatus.CREATED); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerControllerConverterManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerControllerConverterManager.java new file mode 100644 index 0000000000..130c6b815f --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerControllerConverterManager.java @@ -0,0 +1,32 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.ArrayList; +import java.util.List; + +public class CustomerControllerConverterManager { + + private final List> converterList; + + public CustomerControllerConverterManager() { + converterList = new ArrayList<>(); + converterList.add(new CustomerCreateParamConverter()); + converterList.add(new CustomerCreateResponseConverter()); + converterList.add(new CustomerResponseConverter()); + converterList.add(new OwnedVouchersParamConverter()); + converterList.add(new OwnedVoucherResponseConverter()); + converterList.add(new WalletDeleteParamConverter()); + converterList.add(new WalletRegisterParamConverter()); + } + + @SuppressWarnings("unchecked") + public T convert(S source, Class targetType) { + for (TypedConverter converter : converterList) { + if (converter.canConvert(source.getClass(), targetType)) { + return ((TypedConverter) converter).convert(source); + } + } + throw new IllegalArgumentException( + source.getClass() + "타입을 " + targetType + "로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerCreateParamConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerCreateParamConverter.java new file mode 100644 index 0000000000..4445bfb155 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerCreateParamConverter.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.controller.customer.request.CustomerCreateRequest; +import com.wonu606.vouchermanager.service.customer.param.CustomerCreateParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class CustomerCreateParamConverter implements + TypedConverter { + + public CustomerCreateParam convert(CustomerCreateRequest request) { + return new CustomerCreateParam(request.getEmail(), request.getNickname()); + } + + @Override + public Class getSourceType() { + return CustomerCreateRequest.class; + } + + @Override + public Class getTargetType() { + return CustomerCreateParam.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerCreateResponseConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerCreateResponseConverter.java new file mode 100644 index 0000000000..65d6ca04c9 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerCreateResponseConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.controller.customer.response.CustomerCreateResponse; +import com.wonu606.vouchermanager.service.customer.result.CustomerCreateResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class CustomerCreateResponseConverter implements + TypedConverter { + + @Override + public CustomerCreateResponse convert(CustomerCreateResult result) { + return new CustomerCreateResponse(result.isTaskSuccess()); + } + + @Override + public Class getSourceType() { + return CustomerCreateResult.class; + } + + @Override + public Class getTargetType() { + return CustomerCreateResponse.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerResponseConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerResponseConverter.java new file mode 100644 index 0000000000..d9807d654e --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/CustomerResponseConverter.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.controller.customer.response.CustomerResponse; +import com.wonu606.vouchermanager.service.customer.result.CustomerResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class CustomerResponseConverter implements TypedConverter { + + @Override + public CustomerResponse convert(CustomerResult result) { + return new CustomerResponse(result.getEmail(), result.getNickname()); + } + + @Override + public Class getSourceType() { + return CustomerResult.class; + } + + @Override + public Class getTargetType() { + return CustomerResponse.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/OwnedVoucherResponseConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/OwnedVoucherResponseConverter.java new file mode 100644 index 0000000000..e0ee35b70a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/OwnedVoucherResponseConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.controller.customer.response.OwnedVoucherResponse; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class OwnedVoucherResponseConverter implements + TypedConverter { + + @Override + public OwnedVoucherResponse convert(OwnedVoucherResult result) { + return new OwnedVoucherResponse(result.getVoucherUuid()); + } + + @Override + public Class getSourceType() { + return OwnedVoucherResult.class; + } + + @Override + public Class getTargetType() { + return OwnedVoucherResponse.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/OwnedVouchersParamConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/OwnedVouchersParamConverter.java new file mode 100644 index 0000000000..9356c7b972 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/OwnedVouchersParamConverter.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.controller.customer.request.OwnedVouchersRequest; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class OwnedVouchersParamConverter implements + TypedConverter { + + public OwnedVouchersParam convert(OwnedVouchersRequest request) { + return new OwnedVouchersParam(request.getCustomerId()); + } + + @Override + public Class getSourceType() { + return OwnedVouchersRequest.class; + } + + @Override + public Class getTargetType() { + return OwnedVouchersParam.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/WalletDeleteParamConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/WalletDeleteParamConverter.java new file mode 100644 index 0000000000..c0e613f030 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/WalletDeleteParamConverter.java @@ -0,0 +1,26 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.controller.customer.request.WalletDeleteRequest; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.UUID; + +public class WalletDeleteParamConverter implements + TypedConverter { + + @Override + public WalletDeleteParam convert(WalletDeleteRequest request) { + return new WalletDeleteParam(UUID.fromString(request.getVoucherId()), + request.getCustomerId()); + } + + @Override + public Class getSourceType() { + return WalletDeleteRequest.class; + } + + @Override + public Class getTargetType() { + return WalletDeleteParam.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/WalletRegisterParamConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/WalletRegisterParamConverter.java new file mode 100644 index 0000000000..e60afa2f68 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/converter/WalletRegisterParamConverter.java @@ -0,0 +1,27 @@ +package com.wonu606.vouchermanager.controller.customer.converter; + +import com.wonu606.vouchermanager.controller.customer.request.WalletRegisterRequest; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.UUID; + +public class WalletRegisterParamConverter implements + TypedConverter { + + @Override + public Class getSourceType() { + return WalletRegisterRequest.class; + } + + @Override + public Class getTargetType() { + return WalletRegisterParam.class; + } + + @Override + public WalletRegisterParam convert(WalletRegisterRequest request) { + return new WalletRegisterParam( + UUID.fromString(request.getVoucherId()), + request.getCustomerId()); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/CustomerCreateRequest.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/CustomerCreateRequest.java new file mode 100644 index 0000000000..5ba3b41ca5 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/CustomerCreateRequest.java @@ -0,0 +1,31 @@ +package com.wonu606.vouchermanager.controller.customer.request; + +public class CustomerCreateRequest { + + private String email; + private String nickname; + + public CustomerCreateRequest() { + } + + public CustomerCreateRequest(String email, String nickname) { + this.email = email; + this.nickname = nickname; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getNickname() { + return nickname; + } + + public void setNickname(String nickname) { + this.nickname = nickname; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/OwnedVouchersRequest.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/OwnedVouchersRequest.java new file mode 100644 index 0000000000..e8c6ae421c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/OwnedVouchersRequest.java @@ -0,0 +1,21 @@ +package com.wonu606.vouchermanager.controller.customer.request; + +public class OwnedVouchersRequest { + + private String customerId; + + public OwnedVouchersRequest() { + } + + public OwnedVouchersRequest(String customerId) { + this.customerId = customerId; + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/WalletDeleteRequest.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/WalletDeleteRequest.java new file mode 100644 index 0000000000..56533b2147 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/WalletDeleteRequest.java @@ -0,0 +1,31 @@ +package com.wonu606.vouchermanager.controller.customer.request; + +public class WalletDeleteRequest { + + private String voucherId; + private String customerId; + + public WalletDeleteRequest() { + } + + public WalletDeleteRequest(String voucherId, String customerId) { + this.voucherId = voucherId; + this.customerId = customerId; + } + + public String getVoucherId() { + return voucherId; + } + + public void setVoucherId(String voucherId) { + this.voucherId = voucherId; + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/WalletRegisterRequest.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/WalletRegisterRequest.java new file mode 100644 index 0000000000..00ccaaf3f9 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/request/WalletRegisterRequest.java @@ -0,0 +1,31 @@ +package com.wonu606.vouchermanager.controller.customer.request; + +public class WalletRegisterRequest { + + private String voucherId; + private String customerId; + + public WalletRegisterRequest() { + } + + public WalletRegisterRequest(String voucherId, String customerId) { + this.voucherId = voucherId; + this.customerId = customerId; + } + + public String getVoucherId() { + return voucherId; + } + + public void setVoucherId(String voucherId) { + this.voucherId = voucherId; + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/CustomerCreateResponse.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/CustomerCreateResponse.java new file mode 100644 index 0000000000..ada797a34f --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/CustomerCreateResponse.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.controller.customer.response; + +public class CustomerCreateResponse { + + private final Boolean success; + + public CustomerCreateResponse(Boolean success) { + this.success = success; + } + + public Boolean getSuccess() { + return success; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/CustomerResponse.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/CustomerResponse.java new file mode 100644 index 0000000000..28527d4ccc --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/CustomerResponse.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.controller.customer.response; + +public class CustomerResponse { + + private final String email; + private final String nickname; + + public CustomerResponse(String email, String nickname) { + this.email = email; + this.nickname = nickname; + } + + public String getEmail() { + return email; + } + + public String getNickname() { + return nickname; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/OwnedVoucherResponse.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/OwnedVoucherResponse.java new file mode 100644 index 0000000000..57cb30e81c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/customer/response/OwnedVoucherResponse.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.controller.customer.response; + +public class OwnedVoucherResponse { + + private final String voucherId; + + public OwnedVoucherResponse(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/VoucherWalletController.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/VoucherWalletController.java new file mode 100644 index 0000000000..5f8e2684ff --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/VoucherWalletController.java @@ -0,0 +1,94 @@ +package com.wonu606.vouchermanager.controller.voucherwallet; + +import com.wonu606.vouchermanager.controller.voucherwallet.converter.VoucherWalletControllerConverterManager; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.OwnedCustomersRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.VoucherCreateRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.WalletAssignRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.response.OwnedCustomerResponse; +import com.wonu606.vouchermanager.controller.voucherwallet.response.VoucherResponse; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; +import com.wonu606.vouchermanager.service.voucherwallet.VoucherWalletService; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedCustomersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletAssignParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedCustomerResult; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("vouchers") +public class VoucherWalletController { + + private final VoucherWalletService service; + private final VoucherWalletControllerConverterManager converterManager; + + public VoucherWalletController(VoucherWalletService service) { + this.service = service; + converterManager = new VoucherWalletControllerConverterManager(); + } + + @GetMapping("/create") + public String createVoucherForm(Model model) { + model.addAttribute("voucherCreateRequest", new VoucherCreateRequest("", 0.0)); + return "/vouchers/create-form"; + } + + @PostMapping("/create") + public String createVoucher(@ModelAttribute VoucherCreateRequest request) { + VoucherCreateParam param = converterManager.convert(request, VoucherCreateParam.class); + service.createVoucher(param); + + return "redirect:/vouchers/list"; + } + + + @GetMapping("/list") + public String getVoucherList(Model model) { + List results = service.getVoucherList(); + + List voucherList = results.stream() + .map(rs -> converterManager.convert(rs, VoucherResponse.class)) + .collect(Collectors.toList()); + model.addAttribute("voucherList", voucherList); + return "vouchers/list"; + } + + @GetMapping("/owned-customers") + public String ownedCustomersList(Model model) { + model.addAttribute("ownedCustomersRequest", new OwnedCustomersRequest()); + return "vouchers/owned-customers-form"; + } + + @PostMapping("/owned-customers") + public String getOwnedCustomersByVoucher(@ModelAttribute OwnedCustomersRequest request, + Model model) { + OwnedCustomersParam param = converterManager.convert(request, OwnedCustomersParam.class); + List results = service.findOwnedCustomersByVoucher(param); + + List ownedCustomerResponses = results.stream() + .map(rs -> converterManager.convert(rs, OwnedCustomerResponse.class)) + .collect(Collectors.toList()); + model.addAttribute("ownedCustomerResponses", ownedCustomerResponses); + return "/vouchers/owned-customers-list"; + } + + @GetMapping("/assign-wallet") + public String assignWallet(Model model) { + model.addAttribute("walletAssignRequest", new WalletAssignRequest()); + return "/vouchers/assign-wallet-form"; + } + + @PostMapping("/assign-wallet") + public String assignWallet(@ModelAttribute WalletAssignRequest walletAssignRequest) { + WalletAssignParam param = converterManager.convert(walletAssignRequest, + WalletAssignParam.class); + service.assignWallet(param); + return "redirect:/vouchers/list"; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/VoucherWalletRestController.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/VoucherWalletRestController.java new file mode 100644 index 0000000000..dd050e9de5 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/VoucherWalletRestController.java @@ -0,0 +1,76 @@ +package com.wonu606.vouchermanager.controller.voucherwallet; + +import com.wonu606.vouchermanager.controller.voucherwallet.converter.VoucherWalletControllerConverterManager; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.OwnedCustomersRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.VoucherCreateRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.WalletAssignRequest; +import com.wonu606.vouchermanager.controller.voucherwallet.response.OwnedCustomerResponse; +import com.wonu606.vouchermanager.controller.voucherwallet.response.VoucherResponse; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; +import com.wonu606.vouchermanager.service.voucherwallet.VoucherWalletService; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedCustomersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletAssignParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedCustomerResult; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/api/vouchers", produces = MediaType.APPLICATION_JSON_VALUE) +public class VoucherWalletRestController { + + private final VoucherWalletService service; + private final VoucherWalletControllerConverterManager converterManager; + + public VoucherWalletRestController(VoucherWalletService service) { + this.service = service; + converterManager = new VoucherWalletControllerConverterManager(); + } + + @PostMapping("/create") + public ResponseEntity createVoucher(@RequestBody VoucherCreateRequest request) { + VoucherCreateParam param = converterManager.convert(request, VoucherCreateParam.class); + service.createVoucher(param); + return new ResponseEntity<>(HttpStatus.CREATED); + } + + @GetMapping("/list") + public ResponseEntity> getVoucherList() { + List results = service.getVoucherList(); + + List voucherResponses = results.stream() + .map(rs -> converterManager.convert(rs, VoucherResponse.class)) + .collect(Collectors.toList()); + + return new ResponseEntity<>(voucherResponses, HttpStatus.OK); + } + + @PostMapping("/owned-customers") + public ResponseEntity> getOwnedCustomersByVoucher( + @RequestBody OwnedCustomersRequest request) { + OwnedCustomersParam param = converterManager.convert(request, OwnedCustomersParam.class); + List results = service.findOwnedCustomersByVoucher(param); + + List ownedCustomerResponses = results.stream() + .map(rs -> converterManager.convert(rs, OwnedCustomerResponse.class)) + .collect(Collectors.toList()); + + return new ResponseEntity<>(ownedCustomerResponses, HttpStatus.OK); + } + + @PostMapping("/assign-wallet") + public ResponseEntity assignWallet(@RequestBody WalletAssignRequest walletAssignRequest) { + WalletAssignParam param = converterManager.convert(walletAssignRequest, + WalletAssignParam.class); + service.assignWallet(param); + return new ResponseEntity<>(HttpStatus.OK); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/OwnedCustomerResponseConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/OwnedCustomerResponseConverter.java new file mode 100644 index 0000000000..5ed84a288a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/OwnedCustomerResponseConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.converter; + +import com.wonu606.vouchermanager.controller.voucherwallet.response.OwnedCustomerResponse; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedCustomerResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class OwnedCustomerResponseConverter implements + TypedConverter { + + @Override + public OwnedCustomerResponse convert(OwnedCustomerResult result) { + return new OwnedCustomerResponse(result.getCustomerEmail()); + } + + @Override + public Class getSourceType() { + return OwnedCustomerResult.class; + } + + @Override + public Class getTargetType() { + return OwnedCustomerResponse.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/OwnedCustomersParamConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/OwnedCustomersParamConverter.java new file mode 100644 index 0000000000..261a7e5b72 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/OwnedCustomersParamConverter.java @@ -0,0 +1,25 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.converter; + +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.OwnedCustomersRequest; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedCustomersParam; +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.UUID; + +public class OwnedCustomersParamConverter implements + TypedConverter { + + @Override + public OwnedCustomersParam convert(OwnedCustomersRequest request) { + return new OwnedCustomersParam(UUID.fromString(request.getVoucherId())); + } + + @Override + public Class getSourceType() { + return OwnedCustomersRequest.class; + } + + @Override + public Class getTargetType() { + return OwnedCustomersParam.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherCreateParamConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherCreateParamConverter.java new file mode 100644 index 0000000000..8ba8971f3a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherCreateParamConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.converter; + +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.VoucherCreateRequest; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class VoucherCreateParamConverter implements + TypedConverter { + + @Override + public VoucherCreateParam convert(VoucherCreateRequest request) { + return new VoucherCreateParam(request.getType(), request.getDiscountValue()); + } + + @Override + public Class getSourceType() { + return VoucherCreateRequest.class; + } + + @Override + public Class getTargetType() { + return VoucherCreateParam.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherCreateResponseConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherCreateResponseConverter.java new file mode 100644 index 0000000000..11797e2c1d --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherCreateResponseConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.converter; + +import com.wonu606.vouchermanager.controller.voucherwallet.response.VoucherCreateResponse; +import com.wonu606.vouchermanager.service.voucher.result.VoucherCreateResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class VoucherCreateResponseConverter implements + TypedConverter { + + @Override + public VoucherCreateResponse convert(VoucherCreateResult result) { + return new VoucherCreateResponse(result.getTaskSuccess()); + } + + @Override + public Class getSourceType() { + return VoucherCreateResult.class; + } + + @Override + public Class getTargetType() { + return VoucherCreateResponse.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherResponseConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherResponseConverter.java new file mode 100644 index 0000000000..78e25a3210 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherResponseConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.converter; + +import com.wonu606.vouchermanager.controller.voucherwallet.response.VoucherResponse; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class VoucherResponseConverter implements TypedConverter { + + @Override + public VoucherResponse convert(VoucherResult result) { + return new VoucherResponse(result.getUuid(), result.getVoucherClassType(), + result.getDiscountValue()); + } + + @Override + public Class getSourceType() { + return VoucherResult.class; + } + + @Override + public Class getTargetType() { + return VoucherResponse.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherWalletControllerConverterManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherWalletControllerConverterManager.java new file mode 100644 index 0000000000..77d1b03327 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/VoucherWalletControllerConverterManager.java @@ -0,0 +1,31 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.converter; + +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.ArrayList; +import java.util.List; + +public class VoucherWalletControllerConverterManager { + + private final List> converterList; + + public VoucherWalletControllerConverterManager() { + converterList = new ArrayList<>(); + converterList.add(new VoucherCreateParamConverter()); + converterList.add(new VoucherCreateResponseConverter()); + converterList.add(new VoucherResponseConverter()); + converterList.add(new OwnedCustomersParamConverter()); + converterList.add(new OwnedCustomerResponseConverter()); + converterList.add(new WalletAssignParamConverter()); + } + + @SuppressWarnings("unchecked") + public T convert(S source, Class targetType) { + for (TypedConverter converter : converterList) { + if (converter.canConvert(source.getClass(), targetType)) { + return ((TypedConverter) converter).convert(source); + } + } + throw new IllegalArgumentException( + source.getClass() + "타입을 " + targetType + "로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/WalletAssignParamConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/WalletAssignParamConverter.java new file mode 100644 index 0000000000..4361bc893a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/converter/WalletAssignParamConverter.java @@ -0,0 +1,25 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.converter; + +import com.wonu606.vouchermanager.controller.voucherwallet.reqeust.WalletAssignRequest; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletAssignParam; +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.UUID; + +public class WalletAssignParamConverter implements + TypedConverter { + + @Override + public WalletAssignParam convert(WalletAssignRequest request) { + return new WalletAssignParam(UUID.fromString(request.getVoucherId())); + } + + @Override + public Class getSourceType() { + return WalletAssignRequest.class; + } + + @Override + public Class getTargetType() { + return WalletAssignParam.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/OwnedCustomersRequest.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/OwnedCustomersRequest.java new file mode 100644 index 0000000000..aa3de602f4 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/OwnedCustomersRequest.java @@ -0,0 +1,21 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.reqeust; + +public class OwnedCustomersRequest { + + private String voucherId; + + public OwnedCustomersRequest() { + } + + public OwnedCustomersRequest(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } + + public void setVoucherId(String voucherId) { + this.voucherId = voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/VoucherCreateRequest.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/VoucherCreateRequest.java new file mode 100644 index 0000000000..7fa700cd80 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/VoucherCreateRequest.java @@ -0,0 +1,31 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.reqeust; + +public class VoucherCreateRequest { + + private String type; + private double discountValue; + + public VoucherCreateRequest() { + } + + public VoucherCreateRequest(String type, double discountValue) { + this.type = type; + this.discountValue = discountValue; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public double getDiscountValue() { + return discountValue; + } + + public void setDiscountValue(double discountValue) { + this.discountValue = discountValue; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/WalletAssignRequest.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/WalletAssignRequest.java new file mode 100644 index 0000000000..d09a0f4324 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/reqeust/WalletAssignRequest.java @@ -0,0 +1,21 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.reqeust; + +public class WalletAssignRequest { + + private String voucherId; + + public WalletAssignRequest() { + } + + public WalletAssignRequest(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } + + public void setVoucherId(String voucherId) { + this.voucherId = voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/OwnedCustomerResponse.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/OwnedCustomerResponse.java new file mode 100644 index 0000000000..0e31f31429 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/OwnedCustomerResponse.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.response; + +public class OwnedCustomerResponse { + + private final String email; + + public OwnedCustomerResponse(String email) { + this.email = email; + } + + public String getEmail() { + return email; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/VoucherCreateResponse.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/VoucherCreateResponse.java new file mode 100644 index 0000000000..4546c0e929 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/VoucherCreateResponse.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.response; + +public class VoucherCreateResponse { + + private final boolean success; + + public VoucherCreateResponse(boolean success) { + this.success = success; + } + + public boolean isSuccess() { + return success; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/VoucherResponse.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/VoucherResponse.java new file mode 100644 index 0000000000..7f0c9800aa --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/controller/voucherwallet/response/VoucherResponse.java @@ -0,0 +1,26 @@ +package com.wonu606.vouchermanager.controller.voucherwallet.response; + +public class VoucherResponse { + + private final String uuid; + private final String Type; + private final Double value; + + public VoucherResponse(String uuid, String type, Double value) { + this.uuid = uuid; + Type = type; + this.value = value; + } + + public String getUuid() { + return uuid; + } + + public String getType() { + return Type; + } + + public Double getValue() { + return value; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWallet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWallet.java deleted file mode 100644 index 5433fc9e04..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWallet.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.wonu606.vouchermanager.domain.CustomerVoucherWallet; - -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.util.Objects; -import java.util.UUID; - -public class CustomerVoucherWallet { - - private final UUID voucherId; - private final EmailAddress emailAddress; - - public CustomerVoucherWallet(UUID voucherId, EmailAddress emailAddress) { - this.voucherId = voucherId; - this.emailAddress = emailAddress; - } - - public UUID getVoucherId() { - return voucherId; - } - - public String getEmailAddress() { - return emailAddress.getAddress(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - CustomerVoucherWallet that = (CustomerVoucherWallet) o; - return Objects.equals(getVoucherId(), that.getVoucherId()) - && Objects.equals(getEmailAddress(), that.getEmailAddress()); - } - - @Override - public int hashCode() { - return Objects.hash(getVoucherId(), getEmailAddress()); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWalletDto.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWalletDto.java deleted file mode 100644 index 07ee4ea6f5..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWalletDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.wonu606.vouchermanager.domain.CustomerVoucherWallet; - -import java.util.UUID; - -public class CustomerVoucherWalletDto { - private final UUID voucherId; - private final String emailAddress; - - public CustomerVoucherWalletDto(UUID voucherId, String emailAddress) { - this.voucherId = voucherId; - this.emailAddress = emailAddress; - } - - public UUID getVoucherId() { - return voucherId; - } - - public String getEmailAddress() { - return emailAddress; - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWalletResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWalletResultSet.java deleted file mode 100644 index ad92fdb86b..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/CustomerVoucherWallet/CustomerVoucherWalletResultSet.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.wonu606.vouchermanager.domain.CustomerVoucherWallet; - -import java.util.UUID; - -public class CustomerVoucherWalletResultSet { - private final UUID voucherId; - private final String emailAddress; - - public CustomerVoucherWalletResultSet(UUID voucherId, String emailAddress) { - this.voucherId = voucherId; - this.emailAddress = emailAddress; - } - - public UUID getVoucherId() { - return voucherId; - } - - public String getEmailAddress() { - return emailAddress; - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/Customer.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/Customer.java index e83db9d3e2..110a3ca80e 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/Customer.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/Customer.java @@ -1,20 +1,20 @@ package com.wonu606.vouchermanager.domain.customer; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; +import com.wonu606.vouchermanager.domain.customer.email.Email; import java.util.Objects; public class Customer { - private final EmailAddress emailAddress; + private final Email email; private final String nickname; - public Customer(EmailAddress emailAddress, String nickname) { - this.emailAddress = emailAddress; + public Customer(Email email, String nickname) { + this.email = email; this.nickname = nickname; } public String getEmailAddress() { - return emailAddress.getAddress(); + return email.getAddress(); } public String getNickname() { @@ -42,7 +42,7 @@ public int hashCode() { @Override public String toString() { return "Customer{" + - "emailAddress=" + emailAddress + + "emailAddress=" + email + ", nickname='" + nickname + '\'' + '}'; } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/CustomerDto.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/CustomerDto.java deleted file mode 100644 index 2cf49c8232..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/CustomerDto.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.wonu606.vouchermanager.domain.customer; - -public class CustomerDto { - - private final String emailAddress; - private final String nickname; - - public CustomerDto(String emailAddress, String nickname) { - this.emailAddress = emailAddress; - this.nickname = nickname; - } - - public String getEmailAddress() { - return emailAddress; - } - - public String getNickname() { - return nickname; - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/CustomerResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/CustomerResultSet.java deleted file mode 100644 index cc12e35655..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/CustomerResultSet.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.wonu606.vouchermanager.domain.customer; - -import java.time.LocalDateTime; - -public class CustomerResultSet { - - private final String emailAddress; - private final String nickname; - private final LocalDateTime lastUpdated; - private final LocalDateTime createdDate; - - public CustomerResultSet(String emailAddress, String nickname, LocalDateTime lastUpdated, - LocalDateTime createdDate) { - this.emailAddress = emailAddress; - this.nickname = nickname; - this.lastUpdated = lastUpdated; - this.createdDate = createdDate; - } - - public String getEmailAddress() { - return emailAddress; - } - - public String getNickname() { - return nickname; - } - - public LocalDateTime getLastUpdated() { - return lastUpdated; - } - - public LocalDateTime getCreatedDate() { - return createdDate; - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/emailAddress/EmailAddress.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/email/Email.java similarity index 86% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/emailAddress/EmailAddress.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/email/Email.java index e689c5b4bb..ac0be37609 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/emailAddress/EmailAddress.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/email/Email.java @@ -1,16 +1,16 @@ -package com.wonu606.vouchermanager.domain.customer.emailAddress; +package com.wonu606.vouchermanager.domain.customer.email; import java.util.Objects; import java.util.regex.Pattern; -public class EmailAddress { +public class Email { private static final Pattern ADDRESS_PATTERN = Pattern.compile( "^[A-Za-z0-9]+@[A-Za-z0-9-]+.[A-Za-z]+(.[A-Za-z]+)*$"); private final String address; - public EmailAddress(String address) { + public Email(String address) { validateAddress(address); this.address = address; } @@ -37,7 +37,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) { return false; } - EmailAddress that = (EmailAddress) o; + Email that = (Email) o; return Objects.equals(getAddress(), that.getAddress()); } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/emailAddress/EmailAddressDto.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/emailAddress/EmailAddressDto.java deleted file mode 100644 index 4756dfcb92..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/customer/emailAddress/EmailAddressDto.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.wonu606.vouchermanager.domain.customer.emailAddress; - -public class EmailAddressDto { - String emailAddress; - - public EmailAddressDto(String emailAddress) { - this.emailAddress = emailAddress; - } - - public String getEmailAddress() { - return emailAddress; - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucher/VoucherDto.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucher/VoucherDto.java deleted file mode 100644 index 2a7e1ef454..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucher/VoucherDto.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.wonu606.vouchermanager.domain.voucher; - -public class VoucherDto { - - private final String type; - private final double discountValue; - - public VoucherDto(String type, double discountValue) { - this.type = type; - this.discountValue = discountValue; - } - - public String getType() { - return type; - } - - public double getDiscountValue() { - return discountValue; - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucherwallet/VoucherWallet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucherwallet/VoucherWallet.java new file mode 100644 index 0000000000..41e362ad43 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucherwallet/VoucherWallet.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.domain.voucherwallet; + +import java.util.Objects; +import java.util.UUID; + +public class VoucherWallet { + + private final UUID voucherUuid; + private final String email; + + public VoucherWallet(UUID voucherUuid, String email) { + this.voucherUuid = voucherUuid; + this.email = email; + } + + public UUID getVoucherUuid() { + return voucherUuid; + } + + public String getEmailAddress() { + return email; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/CustomerConsoleIO.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/CustomerConsoleIO.java deleted file mode 100644 index 86d7c7dd8c..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/CustomerConsoleIO.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.wonu606.vouchermanager.io; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.menu.CustomerMenu; -import java.util.List; -import org.springframework.stereotype.Component; - -@Component -public class CustomerConsoleIO extends AbstractConsoleIO { - - public CustomerConsoleIO(ConsoleInput input, ConsolePrinter printer) { - super(input, printer); - } - - @Override - public CustomerMenu selectMenu() { - displayMenu(); - String menuSelection = input.readString(CustomerMenu.getAllNames(), "Menu"); - return CustomerMenu.getTypeByName(menuSelection); - } - - protected void displayMenu() { - String lineFormat = "Type %s to %s the program.\n"; - CustomerMenu.getAllNames().forEach(n -> - printer.displayMessage(String.format(lineFormat, n, n))); - } - - public void displayCustomerList(List customerList) { - printer.displayMessage("=== 보유한 Customer 리스트 ==="); - customerList.forEach(c -> printer.displayMessage(c.toString())); - } - - public void displayVoucherList(List voucherList) { - printer.displayMessage("=== 보유한 바우처 리스트 ==="); - voucherList.forEach(v -> printer.displayMessage(v.toString())); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/VoucherConsoleIO.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/VoucherConsoleIO.java deleted file mode 100644 index 983321faa1..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/io/VoucherConsoleIO.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.wonu606.vouchermanager.io; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.menu.VoucherMenu; -import com.wonu606.vouchermanager.service.voucher.VoucherType; -import java.util.List; -import org.springframework.stereotype.Component; - -@Component -public class VoucherConsoleIO extends AbstractConsoleIO { - - public VoucherConsoleIO(ConsoleInput input, ConsolePrinter printer) { - super(input, printer); - } - - @Override - public VoucherMenu selectMenu() { - displayMenu(); - String menuSelection = input.readString(VoucherMenu.getAllNames(), "Menu"); - return VoucherMenu.getTypeByName(menuSelection); - } - - public String selectVoucherType() { - displayVoucherTypes(); - return input.readString(VoucherType.getAllNames(), "Type"); - } - - public void displayVoucherList(List voucherList) { - printer.displayMessage("=== 바우처 리스트 ==="); - voucherList.forEach(v -> printer.displayMessage(v.toString())); - } - - public void displayCustomerList(List customerList) { - printer.displayMessage("=== 보유한 Customer 리스트 ==="); - customerList.forEach(c -> printer.displayMessage(c.toString())); - } - - protected void displayMenu() { - String lineFormat = "Type %s to %s the program.\n"; - VoucherMenu.getAllNames().forEach(n -> - printer.displayMessage(String.format(lineFormat, n, n))); - } - - private void displayVoucherTypes() { - printer.displayMessage("=== 바우처 타입 ==="); - VoucherType.getAllNames() - .forEach(n -> printer.displayMessage("[" + n + "]")); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerJdbcRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerJdbcRepository.java new file mode 100644 index 0000000000..216483b89d --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerJdbcRepository.java @@ -0,0 +1,38 @@ +package com.wonu606.vouchermanager.repository.customer; + +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.repository.customer.reader.CustomerReader; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import com.wonu606.vouchermanager.repository.customer.store.CustomerStore; +import java.util.List; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +@Transactional +public class CustomerJdbcRepository implements CustomerRepository { + + CustomerReader reader; + CustomerStore store; + + public CustomerJdbcRepository(CustomerReader reader, CustomerStore store) { + this.reader = reader; + this.store = store; + } + + @Override + public List findAll() { + return reader.findAll(); + } + + @Override + public CustomerCreateResultSet insert(CustomerCreateQuery query) { + return store.insert(query); + } + + @Override + public void deleteByCustomerId(String email) { + store.deleteByCustomerId(email); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerRepository.java index a3ca98218d..655c87b971 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerRepository.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerRepository.java @@ -1,21 +1,15 @@ package com.wonu606.vouchermanager.repository.customer; -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; import java.util.List; -import java.util.Optional; public interface CustomerRepository { - Customer save(Customer customer); + List findAll(); - Optional findByEmailAddress(EmailAddress emailAddress); + CustomerCreateResultSet insert(CustomerCreateQuery query); - List findAll(); - - List findAllByEmailAddresses(List emailAddresses); - - void deleteByEmailAddress(EmailAddress emailAddress); - - void deleteAll(); + void deleteByCustomerId(String email); } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerResultSetRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerResultSetRepository.java deleted file mode 100644 index 43727ea9d0..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/CustomerResultSetRepository.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.wonu606.vouchermanager.repository.customer; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerResultSet; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.util.List; -import java.util.Optional; - -public interface CustomerResultSetRepository { - - Customer save(Customer customer); - - Optional findByEmailAddress(EmailAddress emailAddress); - - List findAll(); - - void deleteByEmailAddress(EmailAddress emailAddress); - - void deleteAll(); - - List findAllByEmailAddresses(List emailAddresses); -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/JdbcCustomerResultSetRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/JdbcCustomerResultSetRepository.java deleted file mode 100644 index d11547b7ff..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/JdbcCustomerResultSetRepository.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.wonu606.vouchermanager.repository.customer; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerResultSet; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.time.LocalDateTime; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; -import javax.sql.DataSource; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.stereotype.Component; - -@Component -public class JdbcCustomerResultSetRepository implements CustomerResultSetRepository { - - private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; - - public JdbcCustomerResultSetRepository(DataSource dataSource) { - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); - } - - - @Override - public Customer save(Customer customer) { - if (isCustomerEmailAddressPresent(customer)) { - updateCustomer(customer); - } - insertCustomer(customer); - - return customer; - } - - @Override - public Optional findByEmailAddress(EmailAddress emailAddress) { - String selectSql = "SELECT * FROM customer WHERE email_address = :email_address"; - Map params = new HashMap<>(); - params.put("email_address", emailAddress.getAddress()); - try { - CustomerResultSet customerResultSet = namedParameterJdbcTemplate. - queryForObject(selectSql, params, customerResultSetRowMapper()); - return Optional.ofNullable(customerResultSet); - } catch (EmptyResultDataAccessException e) { - return Optional.empty(); - } - } - - @Override - public List findAll() { - String selectSql = "SELECT * FROM customer"; - return namedParameterJdbcTemplate.query(selectSql, customerResultSetRowMapper()); - } - - @Override - public void deleteByEmailAddress(EmailAddress emailAddress) { - String deleteSql = "DELETE FROM customer WHERE email_address = :email_address"; - Map params = new HashMap<>(); - params.put("email_address", emailAddress.getAddress()); - namedParameterJdbcTemplate.update(deleteSql, params); - } - - @Override - public void deleteAll() { - String deleteSql = "DELETE FROM customer"; - namedParameterJdbcTemplate.update(deleteSql, new HashMap<>()); - } - - @Override - public List findAllByEmailAddresses(List emailAddresses) { - if (emailAddresses.isEmpty()) { - return Collections.emptyList(); - } - - String selectSql = "SELECT * FROM customer WHERE email_address IN (:email_address)"; - - MapSqlParameterSource parameters = new MapSqlParameterSource(); - parameters.addValue("email_address", - emailAddresses.stream() - .map(EmailAddress::getAddress) - .collect(Collectors.toList())); - return namedParameterJdbcTemplate.query( - selectSql, parameters, customerResultSetRowMapper()); - } - - private boolean isCustomerEmailAddressPresent(Customer customer) { - String selectSql = "SELECT count(*) FROM customer WHERE email_address = :email_address"; - Map params = new HashMap<>(); - params.put("email_address", customer.getEmailAddress()); - Integer count = namedParameterJdbcTemplate.queryForObject(selectSql, params, Integer.class); - return count != null && count >= 0; - } - - private void updateCustomer(Customer customer) { - String updateSql = "UPDATE customer SET nickname = :nickname WHERE email_address = :email_address"; - Map params = new HashMap<>(); - params.put("email_address", customer.getEmailAddress()); - params.put("nickname", customer.getNickname()); - namedParameterJdbcTemplate.update(updateSql, params); - } - - private void insertCustomer(Customer customer) { - String insertSql = "INSERT INTO customer (email_address, nickname) VALUES (:email_address, :nickname)"; - Map params = new HashMap<>(); - params.put("email_address", customer.getEmailAddress()); - params.put("nickname", customer.getNickname()); - namedParameterJdbcTemplate.update(insertSql, params); - } - - private RowMapper customerResultSetRowMapper() { - return (resultSet, rowNum) -> { - String emailAddress = resultSet.getString("email_address"); - String nickname = resultSet.getString("nickname"); - LocalDateTime lastUpdated = resultSet.getTimestamp("last_updated").toLocalDateTime(); - LocalDateTime createdDate = resultSet.getTimestamp("created_date").toLocalDateTime(); - - return new CustomerResultSet(emailAddress, nickname, lastUpdated, createdDate); - }; - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/MappingCustomerRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/MappingCustomerRepository.java deleted file mode 100644 index 3ac4888db3..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/MappingCustomerRepository.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.wonu606.vouchermanager.repository.customer; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerResultSet; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import org.springframework.stereotype.Component; - -@Component -public class MappingCustomerRepository implements CustomerRepository { - - private final CustomerResultSetRepository customerResultSetRepository; - - public MappingCustomerRepository(CustomerResultSetRepository customerResultSetRepository) { - this.customerResultSetRepository = customerResultSetRepository; - } - - @Override - public Customer save(Customer customer) { - return customerResultSetRepository.save(customer); - } - - @Override - public Optional findByEmailAddress(EmailAddress emailAddress) { - return customerResultSetRepository.findByEmailAddress(emailAddress) - .map(this::convertResultSetToEntity); - } - - @Override - public List findAll() { - return customerResultSetRepository.findAll() - .stream().map(this::convertResultSetToEntity) - .collect(Collectors.toList()); - } - - @Override - public List findAllByEmailAddresses(List emailAddresses) { - List customerResultSets = - customerResultSetRepository.findAllByEmailAddresses(emailAddresses); - return customerResultSets.stream() - .map(this::convertResultSetToEntity) - .collect(Collectors.toList()); - } - - @Override - public void deleteByEmailAddress(EmailAddress emailAddress) { - customerResultSetRepository.deleteByEmailAddress(emailAddress); - } - - @Override - public void deleteAll() { - customerResultSetRepository.deleteAll(); - } - - private Customer convertResultSetToEntity(CustomerResultSet customerResultSet) { - return new Customer( - new EmailAddress(customerResultSet.getEmailAddress()), - customerResultSet.getNickname()); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/query/CustomerCreateQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/query/CustomerCreateQuery.java new file mode 100644 index 0000000000..3e2d4e25c1 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/query/CustomerCreateQuery.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.repository.customer.query; + +public class CustomerCreateQuery { + + private final String email; + private final String nickname; + + public CustomerCreateQuery(String email, String nickname) { + this.email = email; + this.nickname = nickname; + } + + public String getEmail() { + return email; + } + + public String getNickname() { + return nickname; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/CustomerJdbcReader.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/CustomerJdbcReader.java new file mode 100644 index 0000000000..fc06c2e002 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/CustomerJdbcReader.java @@ -0,0 +1,27 @@ +package com.wonu606.vouchermanager.repository.customer.reader; + +import com.wonu606.vouchermanager.repository.customer.reader.rowmapper.CustomerReaderRowMapperManager; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import java.util.List; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class CustomerJdbcReader implements CustomerReader { + + private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; + private final CustomerReaderRowMapperManager rowMapperManager; + + public CustomerJdbcReader(NamedParameterJdbcTemplate namedParameterJdbcTemplate, + CustomerReaderRowMapperManager rowMapperManager) { + this.namedParameterJdbcTemplate = namedParameterJdbcTemplate; + this.rowMapperManager = rowMapperManager; + } + + @Override + public List findAll() { + String selectionSql = "SELECT email, nickname FROM customer"; + return namedParameterJdbcTemplate.query(selectionSql, + rowMapperManager.getRowMapperForType(CustomerResultSet.class)); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/CustomerReader.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/CustomerReader.java new file mode 100644 index 0000000000..7dd9f48abf --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/CustomerReader.java @@ -0,0 +1,9 @@ +package com.wonu606.vouchermanager.repository.customer.reader; + +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import java.util.List; + +public interface CustomerReader { + + List findAll(); +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/rowmapper/CustomerReaderRowMapperManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/rowmapper/CustomerReaderRowMapperManager.java new file mode 100644 index 0000000000..931816e02d --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/rowmapper/CustomerReaderRowMapperManager.java @@ -0,0 +1,27 @@ +package com.wonu606.vouchermanager.repository.customer.reader.rowmapper; + +import com.wonu606.vouchermanager.util.TypedRowMapper; +import java.util.ArrayList; +import java.util.List; +import org.springframework.stereotype.Component; + +@Component +public class CustomerReaderRowMapperManager { + + private final List> rowMapperList; + + public CustomerReaderRowMapperManager() { + rowMapperList = new ArrayList<>(); + rowMapperList.add(new CustomerResultSetRowMapper()); + } + + @SuppressWarnings("unchecked") + public TypedRowMapper getRowMapperForType(Class targetType) { + for (TypedRowMapper rowMapper : rowMapperList) { + if (rowMapper.canConvert(targetType)) { + return (TypedRowMapper) rowMapper; + } + } + throw new IllegalArgumentException(targetType.getName() + "으로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/rowmapper/CustomerResultSetRowMapper.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/rowmapper/CustomerResultSetRowMapper.java new file mode 100644 index 0000000000..0a6b71f2a6 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/reader/rowmapper/CustomerResultSetRowMapper.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.repository.customer.reader.rowmapper; + +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import com.wonu606.vouchermanager.util.TypedRowMapper; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.springframework.stereotype.Component; + +@Component +public class CustomerResultSetRowMapper implements TypedRowMapper { + + @Override + public CustomerResultSet mapRow(ResultSet rs, int rowNum) throws SQLException { + String email = rs.getString("email"); + String nickname = rs.getString("nickname"); + + return new CustomerResultSet(email, nickname); + } + + @Override + public Class getTargetType() { + return CustomerResultSet.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/resultset/CustomerCreateResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/resultset/CustomerCreateResultSet.java new file mode 100644 index 0000000000..f76bcaad5f --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/resultset/CustomerCreateResultSet.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.customer.resultset; + +public class CustomerCreateResultSet { + + private final Integer taskSuccess; + + public CustomerCreateResultSet(Integer taskSuccess) { + this.taskSuccess = taskSuccess; + } + + public Integer getTaskSuccess() { + return taskSuccess; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/resultset/CustomerResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/resultset/CustomerResultSet.java new file mode 100644 index 0000000000..3f287c1062 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/resultset/CustomerResultSet.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.repository.customer.resultset; + +public class CustomerResultSet { + + private final String email; + private final String nickname; + + public CustomerResultSet(String email, String nickname) { + this.email = email; + this.nickname = nickname; + } + + public String getEmail() { + return email; + } + + public String getNickname() { + return nickname; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/store/CustomerJdbcStore.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/store/CustomerJdbcStore.java new file mode 100644 index 0000000000..2675c0fb9a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/store/CustomerJdbcStore.java @@ -0,0 +1,35 @@ +package com.wonu606.vouchermanager.repository.customer.store; + +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; +import java.util.HashMap; +import java.util.Map; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class CustomerJdbcStore implements CustomerStore { + + private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + public CustomerJdbcStore(NamedParameterJdbcTemplate namedParameterJdbcTemplate) { + this.namedParameterJdbcTemplate = namedParameterJdbcTemplate; + } + + @Override + public CustomerCreateResultSet insert(CustomerCreateQuery query) { + String insertionSql = "INSERT INTO customer (email, nickname) VALUES (:email, :nickname)"; + Map params = new HashMap<>(); + params.put("email", query.getEmail()); + params.put("nickname", query.getNickname()); + return new CustomerCreateResultSet(namedParameterJdbcTemplate.update(insertionSql, params)); + } + + @Override + public void deleteByCustomerId(String email) { + String deletionSql = "DELETE FROM customer WHERE email = :email"; + Map params = new HashMap<>(); + params.put("email", email); + namedParameterJdbcTemplate.update(deletionSql, params); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/store/CustomerStore.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/store/CustomerStore.java new file mode 100644 index 0000000000..c9ce92bb0f --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customer/store/CustomerStore.java @@ -0,0 +1,11 @@ +package com.wonu606.vouchermanager.repository.customer.store; + +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; + +public interface CustomerStore { + + CustomerCreateResultSet insert(CustomerCreateQuery query); + + void deleteByCustomerId(String email); +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/CustomerVoucherWalletRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/CustomerVoucherWalletRepository.java deleted file mode 100644 index b1f4c2bb0a..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/CustomerVoucherWalletRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.wonu606.vouchermanager.repository.customerVoucherWallet; - -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWallet; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.util.List; -import java.util.UUID; - -public interface CustomerVoucherWalletRepository { - - List findIdByCustomerEmailAddress(EmailAddress emailAddress); - - void deleteByWallet(CustomerVoucherWallet customerVoucherWallet); - - CustomerVoucherWallet save(CustomerVoucherWallet customerVoucherWallet); - - List findEmailAddressesByVoucherId(UUID voucherId); -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/JdbcCustomerVoucherWalletRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/JdbcCustomerVoucherWalletRepository.java deleted file mode 100644 index d944556723..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/JdbcCustomerVoucherWalletRepository.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.wonu606.vouchermanager.repository.customerVoucherWallet; - -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWallet; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.util.List; -import java.util.UUID; -import javax.sql.DataSource; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Component; - -@Component -public class JdbcCustomerVoucherWalletRepository implements CustomerVoucherWalletRepository { - - private final JdbcTemplate jdbcTemplate; - - public JdbcCustomerVoucherWalletRepository(DataSource dataSource) { - this.jdbcTemplate = new JdbcTemplate(dataSource); - } - - @Override - public List findIdByCustomerEmailAddress(EmailAddress emailAddress) { - String selectSql = "SELECT voucher_id FROM customer_voucher_wallet WHERE customer_email_address = ?"; - return jdbcTemplate.query(selectSql, - (rs, rowNum) -> UUID.fromString(rs.getString("voucher_id")), - emailAddress.getAddress()); - } - - @Override - public void deleteByWallet(CustomerVoucherWallet wallet) { - String deleteSql = "DELETE FROM customer_voucher_wallet WHERE customer_email_address = ? AND voucher_id = ?"; - jdbcTemplate.update(deleteSql, - wallet.getEmailAddress(), - wallet.getVoucherId().toString()); - } - - @Override - public CustomerVoucherWallet save(CustomerVoucherWallet wallet) { - insertCustomerVoucherWallet(wallet); - return wallet; - } - - @Override - public List findEmailAddressesByVoucherId(UUID voucherId) { - String selectSql = "SELECT customer_email_address FROM customer_voucher_wallet WHERE voucher_id = ?"; - return jdbcTemplate.query(selectSql, - (rs, rowNum) -> rs.getString("customer_email_address"), - voucherId.toString()); - } - - private void insertCustomerVoucherWallet(CustomerVoucherWallet wallet) { - String insertSql = "INSERT INTO customer_voucher_wallet (customer_email_address, voucher_id) VALUES (?, ?)"; - jdbcTemplate.update(insertSql, - wallet.getEmailAddress(), - wallet.getVoucherId().toString()); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/JdbcVoucherResultSetRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/JdbcVoucherResultSetRepository.java deleted file mode 100644 index 3d2906bf29..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/JdbcVoucherResultSetRepository.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.wonu606.vouchermanager.repository.voucher; - -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherResultSet; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Collectors; -import javax.sql.DataSource; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.stereotype.Component; - -@Component -public class JdbcVoucherResultSetRepository implements VoucherResultSetRepository { - - private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; - - public JdbcVoucherResultSetRepository(DataSource dataSource) { - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); - } - - @Override - public Voucher save(Voucher voucher) { - if (isVoucherIdPresent(voucher)) { - updateVoucher(voucher); - } - insertVoucher(voucher); - return voucher; - } - - @Override - public Optional findById(UUID id) { - String selectSql = "SELECT * FROM voucher WHERE voucher_id = :voucher_id"; - Map params = new HashMap<>(); - params.put("voucher_id", id.toString()); - try { - VoucherResultSet voucherResultSet = - namedParameterJdbcTemplate.queryForObject(selectSql, - params, - voucherResultSetRowMapper()); - return Optional.ofNullable(voucherResultSet); - } catch (EmptyResultDataAccessException e) { - return Optional.empty(); - } - } - - @Override - public List findAll() { - String selectSql = "SELECT * FROM voucher"; - return namedParameterJdbcTemplate.query(selectSql, voucherResultSetRowMapper()); - } - - @Override - public void deleteById(UUID id) { - String deleteSql = "DELETE FROM voucher WHERE voucher_id = :voucher_id"; - Map params = new HashMap<>(); - params.put("voucher_id", id.toString()); - namedParameterJdbcTemplate.update(deleteSql, params); - } - - @Override - public void deleteAll() { - String deleteSql = "DELETE FROM voucher"; - namedParameterJdbcTemplate.update(deleteSql, new HashMap<>()); - } - - @Override - public List findAllByUuids(List uuidList) { - if (uuidList.isEmpty()) { - return Collections.emptyList(); - } - List uuidStringList = uuidList.stream().map(UUID::toString).collect(Collectors.toList()); - - String selectSql = "SELECT * FROM voucher WHERE voucher_id IN (:uuidList)"; - - MapSqlParameterSource parameters = new MapSqlParameterSource(); - parameters.addValue("uuidList", uuidStringList); - - return namedParameterJdbcTemplate.query(selectSql, parameters, voucherResultSetRowMapper()); - } - - private RowMapper voucherResultSetRowMapper() { - return (resultSet, rowNum) -> { - String voucherSimpleName = resultSet.getString("voucher_type"); - UUID uuid = UUID.fromString(resultSet.getString("voucher_id")); - double discountValue = resultSet.getDouble("discount_value"); - - return new VoucherResultSet(uuid, voucherSimpleName, discountValue); - }; - } - - private boolean isVoucherIdPresent(Voucher voucher) { - String selectSql = "SELECT count(*) FROM voucher WHERE voucher_id = :voucher_id"; - Map params = new HashMap<>(); - params.put("voucher_id", voucher.getUuid().toString()); - - Integer count = namedParameterJdbcTemplate.queryForObject(selectSql, params, Integer.class); - return count != null && count >= 0; - } - - private void updateVoucher(Voucher voucher) { - String updateSql = "UPDATE voucher SET voucher_type = :voucher_type, discount_value = :discount_value WHERE voucher_id = :voucher_id"; - Map params = new HashMap<>(); - params.put("voucher_type", voucher.getClass().getSimpleName()); - params.put("voucher_id", voucher.getUuid().toString()); - params.put("discount_value", voucher.getDiscountValue()); - - namedParameterJdbcTemplate.update(updateSql, params); - } - - private void insertVoucher(Voucher voucher) { - String insertSql = "INSERT INTO voucher (voucher_id, voucher_type, discount_value) VALUES (:voucher_id, :voucher_type, :discount_value)"; - Map params = new HashMap<>(); - params.put("voucher_type", voucher.getClass().getSimpleName()); - params.put("voucher_id", voucher.getUuid().toString()); - params.put("discount_value", voucher.getDiscountValue()); - - namedParameterJdbcTemplate.update(insertSql, params); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/LocalMemoryVoucherVoucherRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/LocalMemoryVoucherVoucherRepository.java deleted file mode 100644 index 1fda500a66..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/LocalMemoryVoucherVoucherRepository.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.wonu606.vouchermanager.repository.voucher; - -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import org.springframework.dao.DuplicateKeyException; - -public class LocalMemoryVoucherVoucherRepository implements VoucherRepository { - - private final Map voucherStore = new ConcurrentHashMap<>(); - - @Override - public Voucher save(Voucher voucher) { - if (voucherStore.containsKey(voucher.getUuid())) { - throw new DuplicateKeyException("이미 존재하는 바우처의 uuid입니다. [uuid]: " + voucher.getUuid()); - } - - voucherStore.put(voucher.getUuid(), voucher); - return voucher; - } - - @Override - public Optional findById(UUID uuid) { - return Optional.ofNullable(voucherStore.get(uuid)); - } - - @Override - public List findAll() { - return new ArrayList<>(voucherStore.values()); - } - - @Override - public List findAllByUuIds(List uuidList) { - throw new UnsupportedOperationException(); - } - - @Override - public void deleteById(UUID uuid) { - voucherStore.remove(uuid); - } - - @Override - public void deleteAll() { - voucherStore.clear(); - } -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/MappingVoucherRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/MappingVoucherRepository.java deleted file mode 100644 index ab9be409c0..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/MappingVoucherRepository.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.wonu606.vouchermanager.repository.voucher; - -import com.wonu606.vouchermanager.domain.voucher.FixedAmountVoucher; -import com.wonu606.vouchermanager.domain.voucher.PercentageVoucher; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherResultSet; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.FixedAmountValue; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.PercentageDiscountValue; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Collectors; -import org.springframework.stereotype.Component; - -@Component -public class MappingVoucherRepository implements VoucherRepository { - - private final VoucherResultSetRepository voucherResultSetRepository; - - public MappingVoucherRepository(VoucherResultSetRepository voucherResultSetRepository) { - this.voucherResultSetRepository = voucherResultSetRepository; - } - - @Override - public Voucher save(Voucher voucher) { - return voucherResultSetRepository.save(voucher); - } - - @Override - public Optional findById(UUID id) { - return voucherResultSetRepository.findById(id) - .map(this::convertResultSetToEntity); - } - - @Override - public List findAll() { - return voucherResultSetRepository.findAll() - .stream().map(this::convertResultSetToEntity) - .collect(Collectors.toList()); - } - - @Override - public List findAllByUuIds(List uuidList) { - List voucherResultSetList = voucherResultSetRepository.findAllByUuids(uuidList); - return voucherResultSetList.stream() - .map(this::convertResultSetToEntity) - .collect(Collectors.toList()); - } - - @Override - public void deleteById(UUID id) { - voucherResultSetRepository.deleteById(id); - } - - @Override - public void deleteAll() { - voucherResultSetRepository.deleteAll(); - } - - private Voucher convertResultSetToEntity(VoucherResultSet voucherResultSet) { - String simpleName = voucherResultSet.getSimpleName(); - - if (isFixedAmountVoucher(simpleName)) { - return createFixedAmountVoucher(voucherResultSet); - } else if (isPercentageVoucher(simpleName)) { - return createPercentageVoucher(voucherResultSet); - } - throw new IllegalStateException("VoucherResult를 Voucher로 변환할 수 없습니다: " + simpleName); - } - - private boolean isFixedAmountVoucher(String simpleName) { - return FixedAmountVoucher.class.getSimpleName().equals(simpleName); - } - - private FixedAmountVoucher createFixedAmountVoucher(VoucherResultSet voucherResultSet) { - return new FixedAmountVoucher(voucherResultSet.getUuid(), - new FixedAmountValue(voucherResultSet.getDiscountValue())); - } - - private boolean isPercentageVoucher(String simpleName) { - return PercentageVoucher.class.getSimpleName().equals(simpleName); - } - - private PercentageVoucher createPercentageVoucher(VoucherResultSet voucherResultSet) { - return new PercentageVoucher(voucherResultSet.getUuid(), - new PercentageDiscountValue(voucherResultSet.getDiscountValue())); - } - -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherJdbcRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherJdbcRepository.java new file mode 100644 index 0000000000..ed5c4b7a2b --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherJdbcRepository.java @@ -0,0 +1,46 @@ +package com.wonu606.vouchermanager.repository.voucher; + +import com.wonu606.vouchermanager.repository.voucher.query.VoucherDeleteQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherFindQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.reader.VoucherReader; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import com.wonu606.vouchermanager.repository.voucher.store.VoucherStore; +import java.util.List; +import java.util.Optional; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +@Transactional +public class VoucherJdbcRepository implements VoucherRepository { + + VoucherReader reader; + VoucherStore store; + + public VoucherJdbcRepository(VoucherReader reader, VoucherStore store) { + this.reader = reader; + this.store = store; + } + + @Override + public VoucherInsertResultSet insert(VoucherInsertQuery query) { + return store.insert(query); + } + + @Override + public Optional findById(VoucherFindQuery query) { + return reader.findById(query); + } + + @Override + public List findAll() { + return reader.findAll(); + } + + @Override + public void deleteById(VoucherDeleteQuery query) { + store.deleteById(query); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherRepository.java index 032d4dbc93..1d5b79ba1c 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherRepository.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherRepository.java @@ -1,21 +1,20 @@ package com.wonu606.vouchermanager.repository.voucher; -import com.wonu606.vouchermanager.domain.voucher.Voucher; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherDeleteQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherFindQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; import java.util.List; import java.util.Optional; -import java.util.UUID; public interface VoucherRepository { - Voucher save(Voucher voucher); + VoucherInsertResultSet insert(VoucherInsertQuery query); - Optional findById(UUID id); + Optional findById(VoucherFindQuery query); - List findAll(); + List findAll(); - List findAllByUuIds(List uuidList); - - void deleteById(UUID id); - - void deleteAll(); + void deleteById(VoucherDeleteQuery query); } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherResultSetRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherResultSetRepository.java deleted file mode 100644 index 649401d15d..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/VoucherResultSetRepository.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.wonu606.vouchermanager.repository.voucher; - -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherResultSet; -import java.util.List; -import java.util.Optional; -import java.util.UUID; - -public interface VoucherResultSetRepository { - - Voucher save(Voucher voucher); - - Optional findById(UUID id); - - List findAll(); - - void deleteById(UUID id); - - void deleteAll(); - - List findAllByUuids(List uuidList); -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherDeleteQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherDeleteQuery.java new file mode 100644 index 0000000000..10a56b8fa9 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherDeleteQuery.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucher.query; + +public class VoucherDeleteQuery { + + private final String voucherId; + + public VoucherDeleteQuery(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherFindQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherFindQuery.java new file mode 100644 index 0000000000..ca383c2874 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherFindQuery.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucher.query; + +public class VoucherFindQuery { + + private final String voucherId; + + public VoucherFindQuery(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherInsertQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherInsertQuery.java new file mode 100644 index 0000000000..3800cbaa02 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/query/VoucherInsertQuery.java @@ -0,0 +1,27 @@ +package com.wonu606.vouchermanager.repository.voucher.query; + +public class VoucherInsertQuery { + + private final String voucherClassSimpleName; + private final String voucherId; + private final Double discountValue; + + public VoucherInsertQuery(String voucherClassSimpleName, String voucherId, + Double discountValue) { + this.voucherClassSimpleName = voucherClassSimpleName; + this.voucherId = voucherId; + this.discountValue = discountValue; + } + + public String getVoucherClassSimpleName() { + return voucherClassSimpleName; + } + + public String getVoucherId() { + return voucherId; + } + + public Double getDiscountValue() { + return discountValue; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/VoucherJdbcReader.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/VoucherJdbcReader.java new file mode 100644 index 0000000000..858e79d92a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/VoucherJdbcReader.java @@ -0,0 +1,47 @@ +package com.wonu606.vouchermanager.repository.voucher.reader; + +import com.wonu606.vouchermanager.repository.voucher.query.VoucherFindQuery; +import com.wonu606.vouchermanager.repository.voucher.reader.rowmapper.VoucherReaderRowMapperManager; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class VoucherJdbcReader implements VoucherReader { + + private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; + private final VoucherReaderRowMapperManager rowMapperManager; + + public VoucherJdbcReader(NamedParameterJdbcTemplate namedParameterJdbcTemplate, + VoucherReaderRowMapperManager rowMapperManager) { + this.namedParameterJdbcTemplate = namedParameterJdbcTemplate; + this.rowMapperManager = rowMapperManager; + } + + @Override + public Optional findById(VoucherFindQuery query) { + String selectionSql = "SELECT voucher_type, voucher_id, discount_value FROM voucher WHERE voucher_id = :voucher_id"; + Map params = new HashMap<>(); + params.put("voucher_id", query.getVoucherId()); + + try { + VoucherResultSet resultSet = namedParameterJdbcTemplate.queryForObject(selectionSql, + params, rowMapperManager.getRowMapperForType(VoucherResultSet.class)); + return Optional.ofNullable(resultSet); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); + } + } + + @Override + public List findAll() { + String selectSql = "SELECT voucher_type, voucher_id, discount_value FROM voucher"; + return namedParameterJdbcTemplate.query(selectSql, + rowMapperManager.getRowMapperForType(VoucherResultSet.class)); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/VoucherReader.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/VoucherReader.java new file mode 100644 index 0000000000..bd223fbc66 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/VoucherReader.java @@ -0,0 +1,13 @@ +package com.wonu606.vouchermanager.repository.voucher.reader; + +import com.wonu606.vouchermanager.repository.voucher.query.VoucherFindQuery; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import java.util.List; +import java.util.Optional; + +public interface VoucherReader { + + Optional findById(VoucherFindQuery query); + + List findAll(); +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/rowmapper/VoucherReaderRowMapperManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/rowmapper/VoucherReaderRowMapperManager.java new file mode 100644 index 0000000000..c169b09615 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/rowmapper/VoucherReaderRowMapperManager.java @@ -0,0 +1,27 @@ +package com.wonu606.vouchermanager.repository.voucher.reader.rowmapper; + +import com.wonu606.vouchermanager.util.TypedRowMapper; +import java.util.ArrayList; +import java.util.List; +import org.springframework.stereotype.Component; + +@Component +public class VoucherReaderRowMapperManager { + + private final List> rowMapperList; + + public VoucherReaderRowMapperManager() { + rowMapperList = new ArrayList<>(); + rowMapperList.add(new VoucherResultSetRowMapper()); + } + + @SuppressWarnings("unchecked") + public TypedRowMapper getRowMapperForType(Class targetType) { + for (TypedRowMapper rowMapper : rowMapperList) { + if (rowMapper.canConvert(targetType)) { + return (TypedRowMapper) rowMapper; + } + } + throw new IllegalArgumentException(targetType.getName() + "으로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/rowmapper/VoucherResultSetRowMapper.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/rowmapper/VoucherResultSetRowMapper.java new file mode 100644 index 0000000000..bca19638f7 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/reader/rowmapper/VoucherResultSetRowMapper.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.repository.voucher.reader.rowmapper; + +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import com.wonu606.vouchermanager.util.TypedRowMapper; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.springframework.stereotype.Component; + +@Component +public class VoucherResultSetRowMapper implements TypedRowMapper { + + @Override + public VoucherResultSet mapRow(ResultSet rs, int rowNum) throws SQLException { + String voucherSimpleName = rs.getString("voucher_type"); + String uuid = rs.getString("voucher_id"); + double discountValue = rs.getDouble("discount_value"); + return new VoucherResultSet(voucherSimpleName, uuid, discountValue); + } + + @Override + public Class getTargetType() { + return VoucherResultSet.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucher/VoucherResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherCreateResultSet.java similarity index 57% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucher/VoucherResultSet.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherCreateResultSet.java index c666f16843..fa4dda2605 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/domain/voucher/VoucherResultSet.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherCreateResultSet.java @@ -1,14 +1,14 @@ -package com.wonu606.vouchermanager.domain.voucher; +package com.wonu606.vouchermanager.repository.voucher.resultset; import java.util.UUID; -public class VoucherResultSet { +public class VoucherCreateResultSet { private final UUID uuid; private final String simpleName; - private final double discountValue; + private final Double discountValue; - public VoucherResultSet(UUID uuid, String simpleName, double discountValue) { + public VoucherCreateResultSet(UUID uuid, String simpleName, Double discountValue) { this.uuid = uuid; this.simpleName = simpleName; this.discountValue = discountValue; @@ -22,7 +22,7 @@ public String getSimpleName() { return simpleName; } - public double getDiscountValue() { + public Double getDiscountValue() { return discountValue; } } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherInsertResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherInsertResultSet.java new file mode 100644 index 0000000000..806ecafb9c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherInsertResultSet.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucher.resultset; + +public class VoucherInsertResultSet { + + private final Integer affectedRowsCount; + + public VoucherInsertResultSet(Integer affectedRowsCount) { + this.affectedRowsCount = affectedRowsCount; + } + + public Integer getAffectedRowsCount() { + return affectedRowsCount; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherResultSet.java new file mode 100644 index 0000000000..e487983d2e --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/resultset/VoucherResultSet.java @@ -0,0 +1,26 @@ +package com.wonu606.vouchermanager.repository.voucher.resultset; + +public class VoucherResultSet { + + private final String uuid; + private final String voucherClassType; + private final Double discountValue; + + public VoucherResultSet(String uuid, String voucherClassType, Double discountValue) { + this.voucherClassType = voucherClassType; + this.uuid = uuid; + this.discountValue = discountValue; + } + + public String getVoucherClassType() { + return voucherClassType; + } + + public String getUuid() { + return uuid; + } + + public Double getDiscountValue() { + return discountValue; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/store/VoucherJdbcStore.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/store/VoucherJdbcStore.java new file mode 100644 index 0000000000..e03780ade3 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/store/VoucherJdbcStore.java @@ -0,0 +1,39 @@ +package com.wonu606.vouchermanager.repository.voucher.store; + +import com.wonu606.vouchermanager.repository.voucher.query.VoucherDeleteQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; +import java.util.HashMap; +import java.util.Map; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class VoucherJdbcStore implements VoucherStore { + + private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + public VoucherJdbcStore(NamedParameterJdbcTemplate namedParameterJdbcTemplate) { + this.namedParameterJdbcTemplate = namedParameterJdbcTemplate; + } + + @Override + public VoucherInsertResultSet insert(VoucherInsertQuery query) { + String insertionSql = "INSERT INTO voucher (voucher_id, voucher_type, discount_value) VALUES (:voucher_id, :voucher_type, :discount_value)"; + Map params = new HashMap<>(); + params.put("voucher_type", query.getVoucherClassSimpleName()); + params.put("voucher_id", query.getVoucherId()); + params.put("discount_value", query.getDiscountValue()); + + return new VoucherInsertResultSet(namedParameterJdbcTemplate.update(insertionSql, params)); + } + + @Override + public void deleteById(VoucherDeleteQuery query) { + String deleteSql = "DELETE FROM voucher WHERE voucher_id = :voucher_id"; + Map params = new HashMap<>(); + params.put("voucher_id", query.getVoucherId()); + + namedParameterJdbcTemplate.update(deleteSql, params); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/store/VoucherStore.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/store/VoucherStore.java new file mode 100644 index 0000000000..53265e467c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucher/store/VoucherStore.java @@ -0,0 +1,12 @@ +package com.wonu606.vouchermanager.repository.voucher.store; + +import com.wonu606.vouchermanager.repository.voucher.query.VoucherDeleteQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; + +public interface VoucherStore { + + VoucherInsertResultSet insert(VoucherInsertQuery query); + + void deleteById(VoucherDeleteQuery query); +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletJdbcRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletJdbcRepository.java new file mode 100644 index 0000000000..673ea66e59 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletJdbcRepository.java @@ -0,0 +1,52 @@ +package com.wonu606.vouchermanager.repository.voucherwallet; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedCustomersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedVouchersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.reader.VoucherWalletReader; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.store.VoucherWalletStore; +import java.util.List; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +@Transactional +public class VoucherWalletJdbcRepository implements VoucherWalletRepository { + + private final VoucherWalletReader reader; + private final VoucherWalletStore store; + + public VoucherWalletJdbcRepository(VoucherWalletReader reader, VoucherWalletStore store) { + this.reader = reader; + this.store = store; + } + + @Override + public List findOwnedVouchersByCustomer(OwnedVouchersQuery query) { + return reader.findOwnedVouchersByCustomer(query); + } + + @Override + public void delete(WalletDeleteQuery query) { + store.delete(query); + } + + public WalletInsertResultSet insert(WalletInsertQuery query) { + return store.insert(query); + } + + @Override + public List findOwnedCustomersByVoucher(OwnedCustomersQuery query) { + return reader.findOwnedCustomersByVoucher(query); + } + + @Override + public void register(WalletRegisterQuery query) { + store.register(query); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletRepository.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletRepository.java new file mode 100644 index 0000000000..c2d5e9b054 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletRepository.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.repository.voucherwallet; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedCustomersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedVouchersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; +import java.util.List; + +public interface VoucherWalletRepository { + + List findOwnedVouchersByCustomer(OwnedVouchersQuery query); + + void delete(WalletDeleteQuery query); + + List findOwnedCustomersByVoucher(OwnedCustomersQuery query); + + WalletInsertResultSet insert(WalletInsertQuery wallet); + + void register(WalletRegisterQuery query); +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/OwnedCustomersQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/OwnedCustomersQuery.java new file mode 100644 index 0000000000..ac46914e24 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/OwnedCustomersQuery.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.query; + +public class OwnedCustomersQuery { + + private final String voucherId; + + public OwnedCustomersQuery(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/OwnedVouchersQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/OwnedVouchersQuery.java new file mode 100644 index 0000000000..856e8a591a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/OwnedVouchersQuery.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.query; + +public class OwnedVouchersQuery { + + private final String customerId; + + public OwnedVouchersQuery(String customerId) { + this.customerId = customerId; + } + + public String getCustomerId() { + return customerId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletDeleteQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletDeleteQuery.java new file mode 100644 index 0000000000..48c0a33575 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletDeleteQuery.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.query; + +public class WalletDeleteQuery { + + private final String customerId; + private final String voucherId; + + public WalletDeleteQuery(String customerId, String voucherId) { + this.customerId = customerId; + this.voucherId = voucherId; + } + + public String getCustomerId() { + return customerId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletInsertQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletInsertQuery.java new file mode 100644 index 0000000000..6ce2eee446 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletInsertQuery.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.query; + +public class WalletInsertQuery { + + private final String voucherId; + + public WalletInsertQuery(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletRegisterQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletRegisterQuery.java new file mode 100644 index 0000000000..32a6435345 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletRegisterQuery.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.query; + +public class WalletRegisterQuery { + + private final String customerId; + private final String voucherId; + + public WalletRegisterQuery(String customerId, String voucherId) { + this.customerId = customerId; + this.voucherId = voucherId; + } + + public String getCustomerId() { + return customerId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletUpdateQuery.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletUpdateQuery.java new file mode 100644 index 0000000000..b1a36b7f8c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/query/WalletUpdateQuery.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.query; + +public class WalletUpdateQuery { + + private final String customerId; + private final String voucherId; + + public WalletUpdateQuery(String customerId, String voucherId) { + this.customerId = customerId; + this.voucherId = voucherId; + } + + public String getCustomerId() { + return customerId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/VoucherWalletJdbcReader.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/VoucherWalletJdbcReader.java new file mode 100644 index 0000000000..afc97f1087 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/VoucherWalletJdbcReader.java @@ -0,0 +1,45 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.reader; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedCustomersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedVouchersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.reader.rowmapper.VoucherWalletReaderRowMapperManager; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class VoucherWalletJdbcReader implements VoucherWalletReader { + + private final NamedParameterJdbcTemplate jdbcTemplate; + private final VoucherWalletReaderRowMapperManager rowMapperManager; + + public VoucherWalletJdbcReader(NamedParameterJdbcTemplate jdbcTemplate, + VoucherWalletReaderRowMapperManager rowMapperManager) { + this.jdbcTemplate = jdbcTemplate; + this.rowMapperManager = rowMapperManager; + } + + @Override + public List findOwnedVouchersByCustomer(OwnedVouchersQuery query) { + String selectionSql = "SELECT voucher_id FROM voucher_wallet WHERE customer_id = :customer_id"; + Map params = new HashMap<>(); + params.put("customer_id", query.getCustomerId()); + + return jdbcTemplate.query(selectionSql, params, + rowMapperManager.getRowMapperForType(OwnedVoucherResultSet.class)); + } + + @Override + public List findOwnedCustomersByVoucher(OwnedCustomersQuery query) { + String selectionSql = "SELECT customer_id FROM voucher_wallet WHERE voucher_id = :voucher_id AND customer_id IS NOT NULL"; + Map params = new HashMap<>(); + params.put("voucher_id", query.getVoucherId()); + + return jdbcTemplate.query(selectionSql, params, + rowMapperManager.getRowMapperForType(OwnedCustomerResultSet.class)); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/VoucherWalletReader.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/VoucherWalletReader.java new file mode 100644 index 0000000000..53e3cad671 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/VoucherWalletReader.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.reader; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedCustomersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedVouchersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import java.util.List; + +public interface VoucherWalletReader { + + List findOwnedVouchersByCustomer(OwnedVouchersQuery query); + + List findOwnedCustomersByVoucher(OwnedCustomersQuery query); +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/OwnedCustomerResultSetRowMapper.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/OwnedCustomerResultSetRowMapper.java new file mode 100644 index 0000000000..f38efdd143 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/OwnedCustomerResultSetRowMapper.java @@ -0,0 +1,19 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.reader.rowmapper; + +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.util.TypedRowMapper; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class OwnedCustomerResultSetRowMapper implements TypedRowMapper { + + @Override + public OwnedCustomerResultSet mapRow(ResultSet rs, int rowNum) throws SQLException { + return new OwnedCustomerResultSet(rs.getString("customer_id")); + } + + @Override + public Class getTargetType() { + return OwnedCustomerResultSet.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/OwnedVoucherResultSetRowMapper.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/OwnedVoucherResultSetRowMapper.java new file mode 100644 index 0000000000..34adc7cecb --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/OwnedVoucherResultSetRowMapper.java @@ -0,0 +1,19 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.reader.rowmapper; + +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import com.wonu606.vouchermanager.util.TypedRowMapper; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class OwnedVoucherResultSetRowMapper implements TypedRowMapper { + + @Override + public OwnedVoucherResultSet mapRow(ResultSet rs, int rowNum) throws SQLException { + return new OwnedVoucherResultSet(rs.getString("voucher_id")); + } + + @Override + public Class getTargetType() { + return OwnedVoucherResultSet.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/VoucherWalletReaderRowMapperManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/VoucherWalletReaderRowMapperManager.java new file mode 100644 index 0000000000..7fecb5c5b3 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/reader/rowmapper/VoucherWalletReaderRowMapperManager.java @@ -0,0 +1,28 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.reader.rowmapper; + +import com.wonu606.vouchermanager.util.TypedRowMapper; +import java.util.ArrayList; +import java.util.List; +import org.springframework.stereotype.Component; + +@Component +public class VoucherWalletReaderRowMapperManager { + + private final List> rowMapperList; + + public VoucherWalletReaderRowMapperManager() { + rowMapperList = new ArrayList<>(); + rowMapperList.add(new OwnedCustomerResultSetRowMapper()); + rowMapperList.add(new OwnedVoucherResultSetRowMapper()); + } + + @SuppressWarnings("unchecked") + public TypedRowMapper getRowMapperForType(Class targetType) { + for (TypedRowMapper rowMapper : rowMapperList) { + if (rowMapper.canConvert(targetType)) { + return (TypedRowMapper) rowMapper; + } + } + throw new IllegalArgumentException(targetType.getName() + "으로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/OwnedCustomerResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/OwnedCustomerResultSet.java new file mode 100644 index 0000000000..6d1f3a7be7 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/OwnedCustomerResultSet.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.resultset; + +public class OwnedCustomerResultSet { + + private final String customerId; + + public OwnedCustomerResultSet(String customerId) { + this.customerId = customerId; + } + + public String getCustomerId() { + return customerId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/OwnedVoucherResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/OwnedVoucherResultSet.java new file mode 100644 index 0000000000..7fbce7d7c2 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/OwnedVoucherResultSet.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.resultset; + +public class OwnedVoucherResultSet { + + private final String voucherId; + + public OwnedVoucherResultSet(String voucherId) { + this.voucherId = voucherId; + } + + public String getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/WalletInsertResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/WalletInsertResultSet.java new file mode 100644 index 0000000000..b3ae0452db --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/WalletInsertResultSet.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.resultset; + +public class WalletInsertResultSet { + + private final Integer affectedRowsCount; + + public WalletInsertResultSet(Integer affectedRowsCount) { + this.affectedRowsCount = affectedRowsCount; + } + + public Integer getAffectedRowsCount() { + return affectedRowsCount; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/WalletUpdateResultSet.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/WalletUpdateResultSet.java new file mode 100644 index 0000000000..d1b34566fa --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/resultset/WalletUpdateResultSet.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.resultset; + +public class WalletUpdateResultSet { + + private final Integer affectedRowsCount; + + public WalletUpdateResultSet(Integer affectedRowsCount) { + this.affectedRowsCount = affectedRowsCount; + } + + public Integer getAffectedRowsCount() { + return affectedRowsCount; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/store/VoucherWalletJdbcStore.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/store/VoucherWalletJdbcStore.java new file mode 100644 index 0000000000..39ecb575a9 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/store/VoucherWalletJdbcStore.java @@ -0,0 +1,49 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.store; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; +import java.util.HashMap; +import java.util.Map; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class VoucherWalletJdbcStore implements VoucherWalletStore { + + private final NamedParameterJdbcTemplate jdbcTemplate; + + public VoucherWalletJdbcStore(NamedParameterJdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + public void delete(WalletDeleteQuery query) { + String deletionSql = "DELETE FROM voucher_wallet WHERE customer_id = :customer_id AND voucher_id = :voucher_id"; + Map params = new HashMap<>(); + params.put("customer_id", query.getCustomerId()); + params.put("voucher_id", query.getVoucherId()); + + jdbcTemplate.update(deletionSql, params); + } + + public WalletInsertResultSet insert(WalletInsertQuery query) { + String insertionSql = "INSERT INTO voucher_wallet (voucher_id, customer_id) VALUES (:voucher_id, NULL)"; + Map params = new HashMap<>(); + params.put("voucher_id", query.getVoucherId()); + + int resultSet = jdbcTemplate.update(insertionSql, params); + return new WalletInsertResultSet(resultSet); + } + + @Override + public void register(WalletRegisterQuery query) { + String updateQuery = "UPDATE voucher_wallet SET customer_id = :customer_id WHERE voucher_id = :voucher_id AND customer_id IS NULL LIMIT 1"; + Map params = new HashMap<>(); + params.put("customer_id", query.getCustomerId()); + params.put("voucher_id", query.getVoucherId()); + + jdbcTemplate.update(updateQuery, params); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/store/VoucherWalletStore.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/store/VoucherWalletStore.java new file mode 100644 index 0000000000..61ab9c71e7 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/repository/voucherwallet/store/VoucherWalletStore.java @@ -0,0 +1,15 @@ +package com.wonu606.vouchermanager.repository.voucherwallet.store; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; + +public interface VoucherWalletStore { + + void delete(WalletDeleteQuery query); + + WalletInsertResultSet insert(WalletInsertQuery wallet); + + void register(WalletRegisterQuery query); +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/CustomerService.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/CustomerService.java index e6b4fd9bc0..a1f83b515e 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/CustomerService.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/CustomerService.java @@ -1,14 +1,22 @@ package com.wonu606.vouchermanager.service.customer; import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerDto; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddressDto; -import com.wonu606.vouchermanager.domain.voucher.Voucher; import com.wonu606.vouchermanager.repository.customer.CustomerRepository; +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import com.wonu606.vouchermanager.service.customer.converter.CustomerServiceConverterManager; +import com.wonu606.vouchermanager.service.customer.factory.CustomerFactory; +import com.wonu606.vouchermanager.service.customer.param.CustomerCreateParam; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.service.customer.result.CustomerCreateResult; +import com.wonu606.vouchermanager.service.customer.result.CustomerResult; +import com.wonu606.vouchermanager.service.voucherwallet.VoucherWalletService; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; import java.util.List; -import java.util.Optional; -import java.util.UUID; +import java.util.stream.Collectors; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -16,36 +24,45 @@ @Transactional public class CustomerService { - private final CustomerRepository customerRepository; + private final VoucherWalletService voucherWalletService; + private final CustomerRepository repository; + private final CustomerFactory customerFactory; + private final CustomerServiceConverterManager converterManager; - public CustomerService(CustomerRepository customerRepository) { - this.customerRepository = customerRepository; + public CustomerService(VoucherWalletService voucherWalletService, CustomerRepository repository, + CustomerFactory customerFactory, CustomerServiceConverterManager converterManager) { + this.voucherWalletService = voucherWalletService; + this.repository = repository; + this.customerFactory = customerFactory; + this.converterManager = converterManager; } - public Customer createCustomer(CustomerDto customerDto) { - return convertDtoToCustomer(customerDto); - } + public CustomerCreateResult createCustomer(CustomerCreateParam param) { + Customer createdCustomer = customerFactory.create(param); + CustomerCreateQuery query = converterManager.convert(createdCustomer, + CustomerCreateQuery.class); - public Customer saveCustomer(Customer customer) { - return customerRepository.save(customer); + CustomerCreateResultSet resultSet = repository.insert(query); + return converterManager.convert(resultSet, CustomerCreateResult.class); } - public Optional findCustomerByEmailAddress(EmailAddressDto emailAddressDto) { - EmailAddress emailAddress = new EmailAddress(emailAddressDto.getEmailAddress()); - return customerRepository.findByEmailAddress(emailAddress); + public List getCustomerList() { + List resultSets = repository.findAll(); + + return resultSets.stream() + .map(rs -> converterManager.convert(rs, CustomerResult.class)) + .collect(Collectors.toList()); } - public List getCustomerList() { - return customerRepository.findAll(); + public List findOwnedVouchersByCustomer(OwnedVouchersParam param) { + return voucherWalletService.findOwnedVouchersByCustomer(param); } - public List getCustomerList(List emailAddresses) { - return customerRepository.findAllByEmailAddresses(emailAddresses); + public void deleteWallet(WalletDeleteParam param) { + voucherWalletService.deleteWallet(param); } - private static Customer convertDtoToCustomer(CustomerDto customerDto) { - return new Customer( - new EmailAddress(customerDto.getEmailAddress()), - customerDto.getNickname()); + public void registerToWallet(WalletRegisterParam param) { + voucherWalletService.registerToWallet(param); } } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerCreateQueryConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerCreateQueryConverter.java new file mode 100644 index 0000000000..25000f284b --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerCreateQueryConverter.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.service.customer.converter; + +import com.wonu606.vouchermanager.domain.customer.Customer; +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class CustomerCreateQueryConverter implements TypedConverter { + + @Override + public CustomerCreateQuery convert(Customer entity) { + return new CustomerCreateQuery(entity.getEmailAddress(), entity.getNickname()); + } + + @Override + public Class getSourceType() { + return Customer.class; + } + + @Override + public Class getTargetType() { + return CustomerCreateQuery.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerCreateResultConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerCreateResultConverter.java new file mode 100644 index 0000000000..974bee8e9b --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerCreateResultConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.customer.converter; + +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; +import com.wonu606.vouchermanager.service.customer.result.CustomerCreateResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class CustomerCreateResultConverter implements + TypedConverter { + + @Override + public CustomerCreateResult convert(CustomerCreateResultSet resultSet) { + return new CustomerCreateResult(resultSet.getTaskSuccess() == 1); + } + + @Override + public Class getSourceType() { + return CustomerCreateResultSet.class; + } + + @Override + public Class getTargetType() { + return CustomerCreateResult.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerResultConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerResultConverter.java new file mode 100644 index 0000000000..a0b3124c74 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerResultConverter.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.service.customer.converter; + +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import com.wonu606.vouchermanager.service.customer.result.CustomerResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class CustomerResultConverter implements TypedConverter { + + @Override + public CustomerResult convert(CustomerResultSet resultSet) { + return new CustomerResult(resultSet.getEmail(), resultSet.getNickname()); + } + + @Override + public Class getSourceType() { + return CustomerResultSet.class; + } + + @Override + public Class getTargetType() { + return CustomerResult.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerServiceConverterManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerServiceConverterManager.java new file mode 100644 index 0000000000..d39982500d --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/converter/CustomerServiceConverterManager.java @@ -0,0 +1,30 @@ +package com.wonu606.vouchermanager.service.customer.converter; + +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.ArrayList; +import java.util.List; +import org.springframework.stereotype.Component; + +@Component +public class CustomerServiceConverterManager { + + private final List> converterList; + + public CustomerServiceConverterManager() { + converterList = new ArrayList<>(); + converterList.add(new CustomerCreateQueryConverter()); + converterList.add(new CustomerCreateResultConverter()); + converterList.add(new CustomerResultConverter()); + } + + @SuppressWarnings("unchecked") + public T convert(S source, Class targetType) { + for (TypedConverter converter : converterList) { + if (converter.canConvert(source.getClass(), targetType)) { + return ((TypedConverter) converter).convert(source); + } + } + throw new IllegalArgumentException( + source.getClass() + "타입을 " + targetType + "로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/factory/CustomerFactory.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/factory/CustomerFactory.java new file mode 100644 index 0000000000..d403ac6bb0 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/factory/CustomerFactory.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.service.customer.factory; + +import com.wonu606.vouchermanager.domain.customer.Customer; +import com.wonu606.vouchermanager.domain.customer.email.Email; +import com.wonu606.vouchermanager.service.customer.param.CustomerCreateParam; +import org.springframework.stereotype.Component; + +@Component +public class CustomerFactory { + + public Customer create(CustomerCreateParam param) { + return new Customer(new Email(param.getEmail()), param.getNickname()); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/param/CustomerCreateParam.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/param/CustomerCreateParam.java new file mode 100644 index 0000000000..c3987ab284 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/param/CustomerCreateParam.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.service.customer.param; + +public class CustomerCreateParam { + + private final String email; + private final String nickname; + + public CustomerCreateParam(String email, String nickname) { + this.email = email; + this.nickname = nickname; + } + + public String getEmail() { + return email; + } + + public String getNickname() { + return nickname; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/param/WalletRegisterParam.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/param/WalletRegisterParam.java new file mode 100644 index 0000000000..baacef95c3 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/param/WalletRegisterParam.java @@ -0,0 +1,22 @@ +package com.wonu606.vouchermanager.service.customer.param; + +import java.util.UUID; + +public class WalletRegisterParam { + + private final UUID voucherId; + private final String customerId; + + public WalletRegisterParam(UUID voucherId, String customerId) { + this.voucherId = voucherId; + this.customerId = customerId; + } + + public UUID getVoucherId() { + return voucherId; + } + + public String getCustomerId() { + return customerId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/result/CustomerCreateResult.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/result/CustomerCreateResult.java new file mode 100644 index 0000000000..eb0dd94710 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/result/CustomerCreateResult.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.service.customer.result; + +public class CustomerCreateResult { + + private final boolean taskSuccess; + + public CustomerCreateResult(boolean taskSuccess) { + this.taskSuccess = taskSuccess; + } + + public boolean isTaskSuccess() { + return taskSuccess; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/result/CustomerResult.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/result/CustomerResult.java new file mode 100644 index 0000000000..d69033458a --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customer/result/CustomerResult.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.service.customer.result; + +public class CustomerResult { + + private final String email; + private final String nickname; + + public CustomerResult(String email, String nickname) { + this.email = email; + this.nickname = nickname; + } + + public String getEmail() { + return email; + } + + public String getNickname() { + return nickname; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customerVoucherWallet/CustomerVoucherWalletService.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customerVoucherWallet/CustomerVoucherWalletService.java deleted file mode 100644 index 1975ed562f..0000000000 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/customerVoucherWallet/CustomerVoucherWalletService.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.wonu606.vouchermanager.service.customerVoucherWallet; - -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWallet; -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWalletDto; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import com.wonu606.vouchermanager.repository.customerVoucherWallet.CustomerVoucherWalletRepository; -import java.util.List; -import java.util.UUID; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - -@Component -@Transactional -public class CustomerVoucherWalletService { - - private final CustomerVoucherWalletRepository customerVoucherWalletRepository; - - public CustomerVoucherWalletService( - CustomerVoucherWalletRepository customerVoucherWalletRepository) { - this.customerVoucherWalletRepository = customerVoucherWalletRepository; - } - - public List findVoucherIdListByCustomerEmailAddress(EmailAddress emailAddress) { - return customerVoucherWalletRepository.findIdByCustomerEmailAddress(emailAddress); - } - - public void deleteByWallet(CustomerVoucherWalletDto customerVoucherWalletDto) { - CustomerVoucherWallet customerVoucherWallet = convertDtoToWallet(customerVoucherWalletDto); - customerVoucherWalletRepository.deleteByWallet(customerVoucherWallet); - } - - public CustomerVoucherWallet save(CustomerVoucherWalletDto customerVoucherWalletDto) { - CustomerVoucherWallet customerVoucherWallet = convertDtoToWallet(customerVoucherWalletDto); - return customerVoucherWalletRepository.save(customerVoucherWallet); - } - - public List findEmailAddressesByVoucherId(UUID voucherId) { - return customerVoucherWalletRepository.findEmailAddressesByVoucherId(voucherId); - } - - private static CustomerVoucherWallet convertDtoToWallet( - CustomerVoucherWalletDto customerVoucherWalletDto) { - return new CustomerVoucherWallet( - customerVoucherWalletDto.getVoucherId(), - new EmailAddress(customerVoucherWalletDto.getEmailAddress()) - ); - } - -} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherService.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherService.java index 6d3fbb65af..66104a501c 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherService.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherService.java @@ -1,10 +1,17 @@ package com.wonu606.vouchermanager.service.voucher; import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherDto; import com.wonu606.vouchermanager.repository.voucher.VoucherRepository; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import com.wonu606.vouchermanager.service.voucher.converter.VoucherServiceConverterManager; +import com.wonu606.vouchermanager.service.voucher.factory.VoucherFactory; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; +import com.wonu606.vouchermanager.service.voucher.result.VoucherCreateResult; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; import java.util.List; -import java.util.UUID; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DuplicateKeyException; @@ -18,39 +25,43 @@ public class VoucherService { private static final Logger log = LoggerFactory.getLogger(VoucherService.class); private static final int MAX_RETRIES = 3; + private final VoucherRepository repository; private final VoucherFactory factory; - private final VoucherRepository voucherRepository; + private final VoucherServiceConverterManager converterManager; - - public VoucherService(VoucherFactory factory, VoucherRepository voucherRepository) { + public VoucherService(VoucherRepository repository, VoucherFactory factory, + VoucherServiceConverterManager converterManager) { + this.repository = repository; this.factory = factory; - this.voucherRepository = voucherRepository; - } - - public Voucher createVoucher(VoucherDto voucherDto) { - return createVoucher(voucherDto, 0); + this.converterManager = converterManager; } - public List getVoucherList() { - return voucherRepository.findAll(); + public VoucherCreateResult createVoucher(VoucherCreateParam param) { + return createVoucher(param, 0); } - public List getVoucherList(List voucherIdList) { - return voucherRepository.findAllByUuIds(voucherIdList); + public List getVoucherList() { + List resultSets = repository.findAll(); + return resultSets.stream() + .map(rs -> converterManager.convert(rs, VoucherResult.class)) + .collect(Collectors.toList()); } - private Voucher createVoucher(VoucherDto voucherDto, int retryCount) { + private VoucherCreateResult createVoucher(VoucherCreateParam voucherCreateParam, + int retryCount) { if (retryCount > MAX_RETRIES) { log.warn("uuid를 재성성하여 저장하는 시도 최대 횟수 {}를 초과하였습니다. ", MAX_RETRIES); throw new IllegalStateException(); } - Voucher voucher = factory.create(voucherDto); + Voucher voucher = factory.create(voucherCreateParam); + VoucherInsertQuery query = converterManager.convert(voucher, VoucherInsertQuery.class); try { - return voucherRepository.save(voucher); + VoucherInsertResultSet resultSet = repository.insert(query); + return converterManager.convert(resultSet, VoucherCreateResult.class); } catch (DuplicateKeyException e) { log.info("DuplicateKeyException가 발생하였습니다. ", e); - return createVoucher(voucherDto, retryCount + 1); + return createVoucher(voucherCreateParam, retryCount + 1); } } } diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherCreateResultConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherCreateResultConverter.java new file mode 100644 index 0000000000..fdbb7b7681 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherCreateResultConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucher.converter; + +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; +import com.wonu606.vouchermanager.service.voucher.result.VoucherCreateResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class VoucherCreateResultConverter implements + TypedConverter { + + @Override + public VoucherCreateResult convert(VoucherInsertResultSet param) { + return new VoucherCreateResult(param.getAffectedRowsCount() == 1); + } + + @Override + public Class getSourceType() { + return VoucherInsertResultSet.class; + } + + @Override + public Class getTargetType() { + return VoucherCreateResult.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherInsertQueryConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherInsertQueryConverter.java new file mode 100644 index 0000000000..52e1dd4d3d --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherInsertQueryConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucher.converter; + +import com.wonu606.vouchermanager.domain.voucher.Voucher; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class VoucherInsertQueryConverter implements TypedConverter { + + @Override + public VoucherInsertQuery convert(Voucher voucher) { + return new VoucherInsertQuery(voucher.getClass().getSimpleName(), + voucher.getUuid().toString(), voucher.getDiscountValue()); + } + + @Override + public Class getSourceType() { + return Voucher.class; + } + + @Override + public Class getTargetType() { + return VoucherInsertQuery.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherResultConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherResultConverter.java new file mode 100644 index 0000000000..4672a8adfd --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherResultConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucher.converter; + +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class VoucherResultConverter implements TypedConverter { + + @Override + public VoucherResult convert(VoucherResultSet resultSet) { + return new VoucherResult(resultSet.getUuid(), resultSet.getVoucherClassType(), + resultSet.getDiscountValue()); + } + + @Override + public Class getSourceType() { + return VoucherResultSet.class; + } + + @Override + public Class getTargetType() { + return VoucherResult.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherServiceConverterManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherServiceConverterManager.java new file mode 100644 index 0000000000..003b7a5bff --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/converter/VoucherServiceConverterManager.java @@ -0,0 +1,30 @@ +package com.wonu606.vouchermanager.service.voucher.converter; + +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.ArrayList; +import java.util.List; +import org.springframework.stereotype.Component; + +@Component +public class VoucherServiceConverterManager { + + private final List> converterList; + + public VoucherServiceConverterManager() { + converterList = new ArrayList<>(); + converterList.add(new VoucherInsertQueryConverter()); + converterList.add(new VoucherCreateResultConverter()); + converterList.add(new VoucherResultConverter()); + } + + @SuppressWarnings("unchecked") + public T convert(S source, Class targetType) { + for (TypedConverter converter : converterList) { + if (converter.canConvert(source.getClass(), targetType)) { + return ((TypedConverter) converter).convert(source); + } + } + throw new IllegalArgumentException( + source.getClass() + "타입을 " + targetType + "로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherFactory.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/VoucherFactory.java similarity index 71% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherFactory.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/VoucherFactory.java index 50377be012..75c453d6da 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherFactory.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/VoucherFactory.java @@ -1,12 +1,12 @@ -package com.wonu606.vouchermanager.service.voucher; +package com.wonu606.vouchermanager.service.voucher.factory; import com.wonu606.vouchermanager.domain.voucher.FixedAmountVoucher; import com.wonu606.vouchermanager.domain.voucher.PercentageVoucher; import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherDto; import com.wonu606.vouchermanager.domain.voucher.discountvalue.FixedAmountValue; import com.wonu606.vouchermanager.domain.voucher.discountvalue.PercentageDiscountValue; -import com.wonu606.vouchermanager.util.UUIDGenerator; +import com.wonu606.vouchermanager.service.voucher.factory.util.UUIDGenerator; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; import org.springframework.stereotype.Component; @Component @@ -18,17 +18,17 @@ public VoucherFactory(UUIDGenerator uuidGenerator) { this.uuidGenerator = uuidGenerator; } - public Voucher create(VoucherDto voucherDto) { - VoucherType creationType = VoucherType.getVoucherTypeByName(voucherDto.getType()); + public Voucher create(VoucherCreateParam voucherCreateParam) { + VoucherType creationType = VoucherType.getVoucherTypeByName(voucherCreateParam.getType()); switch (creationType) { case FIXED: FixedAmountValue fixedAmountValue = - new FixedAmountValue(voucherDto.getDiscountValue()); + new FixedAmountValue(voucherCreateParam.getDiscountValue()); return new FixedAmountVoucher(uuidGenerator.generateUUID(), fixedAmountValue); case PERCENT: PercentageDiscountValue percentageDiscountValue = - new PercentageDiscountValue(voucherDto.getDiscountValue()); + new PercentageDiscountValue(voucherCreateParam.getDiscountValue()); return new PercentageVoucher(uuidGenerator.generateUUID(), percentageDiscountValue); default: throw new IllegalArgumentException("바우처 팩토리에서 생성할 수 없는 타입입니다."); diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherType.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/VoucherType.java similarity index 91% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherType.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/VoucherType.java index 66d7d4364c..a82b40dbe4 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/VoucherType.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/VoucherType.java @@ -1,4 +1,4 @@ -package com.wonu606.vouchermanager.service.voucher; +package com.wonu606.vouchermanager.service.voucher.factory; import java.util.Arrays; import java.util.List; diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/UUIDGenerator.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/util/UUIDGenerator.java similarity index 74% rename from voucher-manager/src/main/java/com/wonu606/vouchermanager/util/UUIDGenerator.java rename to voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/util/UUIDGenerator.java index 40171d08d6..027d14ae27 100644 --- a/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/UUIDGenerator.java +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/factory/util/UUIDGenerator.java @@ -1,4 +1,4 @@ -package com.wonu606.vouchermanager.util; +package com.wonu606.vouchermanager.service.voucher.factory.util; import java.util.UUID; import org.springframework.stereotype.Component; diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/param/VoucherCreateParam.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/param/VoucherCreateParam.java new file mode 100644 index 0000000000..cf7fa65b49 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/param/VoucherCreateParam.java @@ -0,0 +1,20 @@ +package com.wonu606.vouchermanager.service.voucher.param; + +public class VoucherCreateParam { + + private final String type; + private final Double discountValue; + + public VoucherCreateParam(String type, Double discountValue) { + this.type = type; + this.discountValue = discountValue; + } + + public String getType() { + return type; + } + + public Double getDiscountValue() { + return discountValue; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/result/VoucherCreateResult.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/result/VoucherCreateResult.java new file mode 100644 index 0000000000..3e350d2efb --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/result/VoucherCreateResult.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.service.voucher.result; + +public class VoucherCreateResult { + + private final Boolean taskSuccess; + + public VoucherCreateResult(Boolean taskSuccess) { + this.taskSuccess = taskSuccess; + } + + public Boolean getTaskSuccess() { + return taskSuccess; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/result/VoucherResult.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/result/VoucherResult.java new file mode 100644 index 0000000000..834c0ec5b9 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucher/result/VoucherResult.java @@ -0,0 +1,26 @@ +package com.wonu606.vouchermanager.service.voucher.result; + +public class VoucherResult { + + private final String uuid; + private final String voucherClassType; + private final Double discountValue; + + public VoucherResult(String uuid, String voucherClassType, Double discountValue) { + this.uuid = uuid; + this.voucherClassType = voucherClassType; + this.discountValue = discountValue; + } + + public String getUuid() { + return uuid; + } + + public String getVoucherClassType() { + return voucherClassType; + } + + public Double getDiscountValue() { + return discountValue; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/VoucherWalletService.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/VoucherWalletService.java new file mode 100644 index 0000000000..0656254da9 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/VoucherWalletService.java @@ -0,0 +1,90 @@ +package com.wonu606.vouchermanager.service.voucherwallet; + +import com.wonu606.vouchermanager.repository.voucherwallet.VoucherWalletRepository; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedCustomersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedVouchersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.service.voucher.VoucherService; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; +import com.wonu606.vouchermanager.service.voucher.result.VoucherCreateResult; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; +import com.wonu606.vouchermanager.service.voucherwallet.converter.VoucherWalletServiceConverterManager; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedCustomersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletAssignParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedCustomerResult; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; +import com.wonu606.vouchermanager.service.voucherwallet.result.WalletAssignResult; +import java.util.List; +import java.util.stream.Collectors; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +@Transactional +public class VoucherWalletService { + + private final VoucherService voucherService; + private final VoucherWalletRepository voucherWalletRepository; + private final VoucherWalletServiceConverterManager converterManager; + + public VoucherWalletService(VoucherService voucherService, + VoucherWalletRepository voucherWalletRepository, + VoucherWalletServiceConverterManager converterManager) { + this.voucherService = voucherService; + this.voucherWalletRepository = voucherWalletRepository; + this.converterManager = converterManager; + } + + public List findOwnedVouchersByCustomer(OwnedVouchersParam param) { + OwnedVouchersQuery query = converterManager.convert(param, OwnedVouchersQuery.class); + List resultSets = + voucherWalletRepository.findOwnedVouchersByCustomer(query); + + return resultSets.stream() + .map(rs -> converterManager.convert(rs, OwnedVoucherResult.class)) + .collect(Collectors.toList()); + } + + public void deleteWallet(WalletDeleteParam param) { + WalletDeleteQuery query = converterManager.convert(param, WalletDeleteQuery.class); + voucherWalletRepository.delete(query); + } + + public WalletAssignResult assignWallet(WalletAssignParam param) { + WalletInsertQuery query = converterManager.convert(param, WalletInsertQuery.class); + + WalletInsertResultSet resultSet = voucherWalletRepository.insert(query); + return converterManager.convert(resultSet, WalletAssignResult.class); + } + + public List findOwnedCustomersByVoucher(OwnedCustomersParam param) { + OwnedCustomersQuery query = converterManager.convert(param, OwnedCustomersQuery.class); + List resultSets = + voucherWalletRepository.findOwnedCustomersByVoucher(query); + + return resultSets.stream() + .map(rs -> converterManager.convert(rs, OwnedCustomerResult.class)) + .collect(Collectors.toList()); + } + + public VoucherCreateResult createVoucher(VoucherCreateParam param) { + return voucherService.createVoucher(param); + } + + public List getVoucherList() { + return voucherService.getVoucherList(); + } + + public void registerToWallet(WalletRegisterParam param) { + WalletRegisterQuery query = converterManager.convert(param, WalletRegisterQuery.class); + voucherWalletRepository.register(query); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedCustomersQueryConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedCustomersQueryConverter.java new file mode 100644 index 0000000000..d80479bf9c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedCustomersQueryConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedCustomersQuery; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedCustomersParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class OwnedCustomersQueryConverter implements + TypedConverter { + + @Override + public OwnedCustomersQuery convert(OwnedCustomersParam param) { + return new OwnedCustomersQuery(param.getVoucherUuid().toString()); + } + + @Override + public Class getSourceType() { + return OwnedCustomersParam.class; + } + + @Override + public Class getTargetType() { + return OwnedCustomersQuery.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedCustomersResultConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedCustomersResultConverter.java new file mode 100644 index 0000000000..e7e1ab0d11 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedCustomersResultConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedCustomerResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class OwnedCustomersResultConverter implements + TypedConverter { + + @Override + public OwnedCustomerResult convert(OwnedCustomerResultSet param) { + return new OwnedCustomerResult(param.getCustomerId()); + } + + @Override + public Class getSourceType() { + return OwnedCustomerResultSet.class; + } + + @Override + public Class getTargetType() { + return OwnedCustomerResult.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedVoucherQueryConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedVoucherQueryConverter.java new file mode 100644 index 0000000000..2751ceedff --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedVoucherQueryConverter.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedVouchersQuery; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class OwnedVoucherQueryConverter implements + TypedConverter { + + public OwnedVouchersQuery convert(OwnedVouchersParam param) { + return new OwnedVouchersQuery(param.getEmail()); + } + + @Override + public Class getSourceType() { + return OwnedVouchersParam.class; + } + + @Override + public Class getTargetType() { + return OwnedVouchersQuery.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedVoucherResultConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedVoucherResultConverter.java new file mode 100644 index 0000000000..202cf8d88c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/OwnedVoucherResultConverter.java @@ -0,0 +1,23 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class OwnedVoucherResultConverter implements + TypedConverter { + + public OwnedVoucherResult convert(OwnedVoucherResultSet resultSet) { + return new OwnedVoucherResult(resultSet.getVoucherId()); + } + + @Override + public Class getSourceType() { + return OwnedVoucherResultSet.class; + } + + @Override + public Class getTargetType() { + return OwnedVoucherResult.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/VoucherWalletServiceConverterManager.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/VoucherWalletServiceConverterManager.java new file mode 100644 index 0000000000..b3c40b9ff5 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/VoucherWalletServiceConverterManager.java @@ -0,0 +1,35 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.util.TypedConverter; +import java.util.ArrayList; +import java.util.List; +import org.springframework.stereotype.Component; + +@Component +public class VoucherWalletServiceConverterManager { + + private final List> converterList; + + public VoucherWalletServiceConverterManager() { + converterList = new ArrayList<>(); + converterList.add(new OwnedVoucherQueryConverter()); + converterList.add(new OwnedVoucherResultConverter()); + converterList.add(new WalletDeleteQueryConverter()); + converterList.add(new WalletInsertQueryConverter()); + converterList.add(new WalletInsertResultConverter()); + converterList.add(new OwnedCustomersQueryConverter()); + converterList.add(new OwnedCustomersResultConverter()); + converterList.add(new WalletRegisterQueryConverter()); + } + + @SuppressWarnings("unchecked") + public T convert(S source, Class targetType) { + for (TypedConverter converter : converterList) { + if (converter.canConvert(source.getClass(), targetType)) { + return ((TypedConverter) converter).convert(source); + } + } + throw new IllegalArgumentException( + source.getClass() + "타입을 " + targetType + "로 변환할 수 없습니다."); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletDeleteQueryConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletDeleteQueryConverter.java new file mode 100644 index 0000000000..3a0d80afc0 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletDeleteQueryConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class WalletDeleteQueryConverter implements + TypedConverter { + + @Override + public WalletDeleteQuery convert(WalletDeleteParam param) { + return new WalletDeleteQuery(param.getCustomerEmail(), param.getVoucherUuid().toString()); + } + + @Override + public Class getSourceType() { + return WalletDeleteParam.class; + } + + @Override + public Class getTargetType() { + return WalletDeleteQuery.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletInsertQueryConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletInsertQueryConverter.java new file mode 100644 index 0000000000..b74c7a2464 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletInsertQueryConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletAssignParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class WalletInsertQueryConverter implements + TypedConverter { + + @Override + public WalletInsertQuery convert(WalletAssignParam param) { + return new WalletInsertQuery(param.getVoucherId().toString()); + } + + @Override + public Class getSourceType() { + return WalletAssignParam.class; + } + + @Override + public Class getTargetType() { + return WalletInsertQuery.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletInsertResultConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletInsertResultConverter.java new file mode 100644 index 0000000000..c886c46aad --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletInsertResultConverter.java @@ -0,0 +1,24 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; +import com.wonu606.vouchermanager.service.voucherwallet.result.WalletAssignResult; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class WalletInsertResultConverter implements + TypedConverter { + + @Override + public WalletAssignResult convert(WalletInsertResultSet param) { + return new WalletAssignResult(param.getAffectedRowsCount() == 1); + } + + @Override + public Class getSourceType() { + return WalletInsertResultSet.class; + } + + @Override + public Class getTargetType() { + return WalletAssignResult.class; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletRegisterQueryConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletRegisterQueryConverter.java new file mode 100644 index 0000000000..373b65794c --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/converter/WalletRegisterQueryConverter.java @@ -0,0 +1,26 @@ +package com.wonu606.vouchermanager.service.voucherwallet.converter; + +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.util.TypedConverter; + +public class WalletRegisterQueryConverter implements + TypedConverter { + + @Override + public Class getSourceType() { + return WalletRegisterParam.class; + } + + @Override + public Class getTargetType() { + return WalletRegisterQuery.class; + } + + @Override + public WalletRegisterQuery convert(WalletRegisterParam param) { + return new WalletRegisterQuery( + param.getCustomerId(), + param.getVoucherId().toString()); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/OwnedCustomersParam.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/OwnedCustomersParam.java new file mode 100644 index 0000000000..40c580e9c7 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/OwnedCustomersParam.java @@ -0,0 +1,16 @@ +package com.wonu606.vouchermanager.service.voucherwallet.param; + +import java.util.UUID; + +public class OwnedCustomersParam { + + private final UUID voucherUuid; + + public OwnedCustomersParam(UUID voucherUuid) { + this.voucherUuid = voucherUuid; + } + + public UUID getVoucherUuid() { + return voucherUuid; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/OwnedVouchersParam.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/OwnedVouchersParam.java new file mode 100644 index 0000000000..067c83ce93 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/OwnedVouchersParam.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.service.voucherwallet.param; + +public class OwnedVouchersParam { + + private final String email; + + public OwnedVouchersParam(String email) { + this.email = email; + } + + public String getEmail() { + return email; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/WalletAssignParam.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/WalletAssignParam.java new file mode 100644 index 0000000000..3d8e36545e --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/WalletAssignParam.java @@ -0,0 +1,16 @@ +package com.wonu606.vouchermanager.service.voucherwallet.param; + +import java.util.UUID; + +public class WalletAssignParam { + + private final UUID voucherId; + + public WalletAssignParam(UUID voucherId) { + this.voucherId = voucherId; + } + + public UUID getVoucherId() { + return voucherId; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/WalletDeleteParam.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/WalletDeleteParam.java new file mode 100644 index 0000000000..1570173a84 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/param/WalletDeleteParam.java @@ -0,0 +1,22 @@ +package com.wonu606.vouchermanager.service.voucherwallet.param; + +import java.util.UUID; + +public class WalletDeleteParam { + + private final UUID voucherUuid; + private final String customerEmail; + + public WalletDeleteParam(UUID voucherUuid, String customerEmail) { + this.voucherUuid = voucherUuid; + this.customerEmail = customerEmail; + } + + public UUID getVoucherUuid() { + return voucherUuid; + } + + public String getCustomerEmail() { + return customerEmail; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/OwnedCustomerResult.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/OwnedCustomerResult.java new file mode 100644 index 0000000000..5450e552e5 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/OwnedCustomerResult.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.service.voucherwallet.result; + +public class OwnedCustomerResult { + + private final String customerEmail; + + public OwnedCustomerResult(String customerEmail) { + this.customerEmail = customerEmail; + } + + public String getCustomerEmail() { + return customerEmail; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/OwnedVoucherResult.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/OwnedVoucherResult.java new file mode 100644 index 0000000000..64919a6ca8 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/OwnedVoucherResult.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.service.voucherwallet.result; + +public class OwnedVoucherResult { + + private final String voucherUuid; + + public OwnedVoucherResult(String voucherUuid) { + this.voucherUuid = voucherUuid; + } + + public String getVoucherUuid() { + return voucherUuid; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/WalletAssignResult.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/WalletAssignResult.java new file mode 100644 index 0000000000..bc4cbc1b50 --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/service/voucherwallet/result/WalletAssignResult.java @@ -0,0 +1,14 @@ +package com.wonu606.vouchermanager.service.voucherwallet.result; + +public class WalletAssignResult { + + private final Boolean taskSuccess; + + public WalletAssignResult(Boolean taskSuccess) { + this.taskSuccess = taskSuccess; + } + + public Boolean getTaskSuccess() { + return taskSuccess; + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/TypedConverter.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/TypedConverter.java new file mode 100644 index 0000000000..b931447d8d --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/TypedConverter.java @@ -0,0 +1,15 @@ +package com.wonu606.vouchermanager.util; + +import org.springframework.core.convert.converter.Converter; + +public interface TypedConverter extends Converter { + + Class getSourceType(); + + Class getTargetType(); + + default boolean canConvert(Class sourceType, Class targetType) { + return getSourceType().isAssignableFrom(sourceType) + && getTargetType().isAssignableFrom(targetType); + } +} diff --git a/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/TypedRowMapper.java b/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/TypedRowMapper.java new file mode 100644 index 0000000000..ea9d83db8b --- /dev/null +++ b/voucher-manager/src/main/java/com/wonu606/vouchermanager/util/TypedRowMapper.java @@ -0,0 +1,12 @@ +package com.wonu606.vouchermanager.util; + +import org.springframework.jdbc.core.RowMapper; + +public interface TypedRowMapper extends RowMapper { + + Class getTargetType(); + + default boolean canConvert(Class targetType) { + return getTargetType() == targetType; + } +} diff --git a/voucher-manager/src/main/resources/application.properties b/voucher-manager/src/main/resources/application.properties index 7256c72dbb..55853b6255 100644 --- a/voucher-manager/src/main/resources/application.properties +++ b/voucher-manager/src/main/resources/application.properties @@ -1,4 +1,4 @@ -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/springboot_basic -spring.datasource.username=root -spring.datasource.password=1234 +spring.datasource.driver-class-name=${SPRING_DATASOURCE_DRIVERCLASSNAME} +spring.datasource.url=${SPRING_DATASOURCE_URL} +spring.datasource.username=${SPRING_DATASOURCE_USERNAME} +spring.datasource.password=${SPRING_DATASOURCE_PASSWORD} diff --git a/voucher-manager/src/main/resources/logback.xml b/voucher-manager/src/main/resources/logback.xml index e3415d0269..9f099ee190 100644 --- a/voucher-manager/src/main/resources/logback.xml +++ b/voucher-manager/src/main/resources/logback.xml @@ -8,7 +8,7 @@ - + diff --git a/voucher-manager/src/main/resources/schema.sql b/voucher-manager/src/main/resources/schema.sql deleted file mode 100644 index a8e3a21c27..0000000000 --- a/voucher-manager/src/main/resources/schema.sql +++ /dev/null @@ -1,24 +0,0 @@ -CREATE TABLE voucher -( - voucher_id VARCHAR(36) PRIMARY KEY, - voucher_type VARCHAR(50) NOT NULL, - discount_value DECIMAL(10, 2) NOT NULL -); - -CREATE TABLE customer -( - email_address VARCHAR(100) PRIMARY KEY, - nickname VARCHAR(100), - last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -); - -CREATE TABLE customer_voucher_wallet -( - customer_email_address VARCHAR(100) NOT NULL, - voucher_id VARCHAR(36) NOT NULL, - - FOREIGN KEY (customer_email_address) REFERENCES customer (email_address) ON DELETE CASCADE, - FOREIGN KEY (voucher_id) REFERENCES voucher (voucher_id) ON DELETE CASCADE, - PRIMARY KEY (customer_email_address, voucher_id) -); diff --git a/voucher-manager/src/main/resources/templates/customers/create-form.html b/voucher-manager/src/main/resources/templates/customers/create-form.html new file mode 100644 index 0000000000..455b09194c --- /dev/null +++ b/voucher-manager/src/main/resources/templates/customers/create-form.html @@ -0,0 +1,25 @@ + + + + Create Customer + + + + +
+

Create New Customer

+ +
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/voucher-manager/src/main/resources/templates/customers/list.html b/voucher-manager/src/main/resources/templates/customers/list.html new file mode 100644 index 0000000000..6adb486052 --- /dev/null +++ b/voucher-manager/src/main/resources/templates/customers/list.html @@ -0,0 +1,31 @@ + + + + Customer List + + + + +
+

Customer List

+ + Create New Customer + Get Owned Vouchers + + + + + + + + + + + + + + +
EmailNickname
+
+ + diff --git a/voucher-manager/src/main/resources/templates/customers/owned-vouchers-form.html b/voucher-manager/src/main/resources/templates/customers/owned-vouchers-form.html new file mode 100644 index 0000000000..18b0914909 --- /dev/null +++ b/voucher-manager/src/main/resources/templates/customers/owned-vouchers-form.html @@ -0,0 +1,21 @@ + + + + Get Owned Vouchers + + + + +
+

Get Owned Vouchers by Customer

+ +
+
+ + +
+ +
+
+ + diff --git a/voucher-manager/src/main/resources/templates/customers/owned-vouchers-list.html b/voucher-manager/src/main/resources/templates/customers/owned-vouchers-list.html new file mode 100644 index 0000000000..54dc1b3588 --- /dev/null +++ b/voucher-manager/src/main/resources/templates/customers/owned-vouchers-list.html @@ -0,0 +1,47 @@ + + + + Owned Vouchers + + + + +
+

Owned Vouchers

+ + + + + + + + + + + + + + +
Voucher IdAction
+
+ + + +
+
+ +

Register a New Voucher

+ +
+
+ + + +
+ +
+ + Get Customers List +
+ + diff --git a/voucher-manager/src/main/resources/templates/vouchers/assign-wallet-form.html b/voucher-manager/src/main/resources/templates/vouchers/assign-wallet-form.html new file mode 100644 index 0000000000..6535fd1fdc --- /dev/null +++ b/voucher-manager/src/main/resources/templates/vouchers/assign-wallet-form.html @@ -0,0 +1,21 @@ + + + + Find Owned Customers by Voucher + + + +
+

Assign Wallet

+
+
+ + +
+ +
+
+ + diff --git a/voucher-manager/src/main/resources/templates/vouchers/create-form.html b/voucher-manager/src/main/resources/templates/vouchers/create-form.html new file mode 100644 index 0000000000..7192ac1edb --- /dev/null +++ b/voucher-manager/src/main/resources/templates/vouchers/create-form.html @@ -0,0 +1,29 @@ + + + + Create Voucher + + + +
+

Create Voucher

+
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/voucher-manager/src/main/resources/templates/vouchers/list.html b/voucher-manager/src/main/resources/templates/vouchers/list.html new file mode 100644 index 0000000000..8d4557c1ee --- /dev/null +++ b/voucher-manager/src/main/resources/templates/vouchers/list.html @@ -0,0 +1,34 @@ + + + + Vouchers + + + +
+

Vouchers

+ + + + + + + + + + + + + + + + +
IDTypeValue
+
+ + diff --git a/voucher-manager/src/main/resources/templates/vouchers/owned-customers-form.html b/voucher-manager/src/main/resources/templates/vouchers/owned-customers-form.html new file mode 100644 index 0000000000..331f75282d --- /dev/null +++ b/voucher-manager/src/main/resources/templates/vouchers/owned-customers-form.html @@ -0,0 +1,21 @@ + + + + Find Owned Customers by Voucher + + + +
+

Find Customers who Own a Specific Voucher

+
+
+ + +
+ +
+
+ + diff --git a/voucher-manager/src/main/resources/templates/vouchers/owned-customers-list.html b/voucher-manager/src/main/resources/templates/vouchers/owned-customers-list.html new file mode 100644 index 0000000000..3f7b9ecda1 --- /dev/null +++ b/voucher-manager/src/main/resources/templates/vouchers/owned-customers-list.html @@ -0,0 +1,28 @@ + + + + Owned Customers + + + +
+

Owned Customers

+ + + + + + + + + + + +
Email Address
+ +
+ + diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/consoleInterface/VoucherConsoleInterfaceTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/consoleInterface/VoucherConsoleInterfaceTest.java deleted file mode 100644 index 23ac6f4e2e..0000000000 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/consoleInterface/VoucherConsoleInterfaceTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.wonu606.vouchermanager.consoleInterface; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mock; - -import com.wonu606.vouchermanager.controller.VoucherController; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherDto; -import com.wonu606.vouchermanager.io.VoucherConsoleIO; -import com.wonu606.vouchermanager.menu.VoucherMenu; -import java.util.Arrays; -import java.util.List; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; - -@DisplayName("VoucherConsoleInterface 테스트") -public class VoucherConsoleInterfaceTest { - - private VoucherConsoleInterface consoleInterface; - - private VoucherController controllerMock; - - private VoucherConsoleIO voucherConsoleIOMock; - - - @BeforeEach - public void setUp() { - controllerMock = mock(VoucherController.class); - voucherConsoleIOMock = mock(VoucherConsoleIO.class); - consoleInterface = new VoucherConsoleInterface(voucherConsoleIOMock, controllerMock); - } - - @DisplayName("exit가 입력으로 주어지고_run을 하면_프로그램이 종료된다.") - @Test - public void GivenExitMenuSelected_WhenRun_ThenTerminatesProgram() { - // Given - given(voucherConsoleIOMock.selectMenu()).willReturn(VoucherMenu.EXIT); - - // When - consoleInterface.run(); - - // then - then(voucherConsoleIOMock).should().selectMenu(); - then(voucherConsoleIOMock).shouldHaveNoMoreInteractions(); - } - - @DisplayName("list가 입력으로 주어지고_run을 하면_Voucher들을 출력한다.") - @Test - public void GivenListMenuSelected_WhenRun_ThenDisplayVoucherList() { - // Given - List voucherList = Arrays.asList(mock(Voucher.class), mock(Voucher.class)); - given(voucherConsoleIOMock.selectMenu()).willReturn(VoucherMenu.LIST, VoucherMenu.EXIT); - given(controllerMock.getVoucherList()).willReturn(voucherList); - - // When - consoleInterface.run(); - - // Then - then(voucherConsoleIOMock).should().displayVoucherList(voucherList); - } - - @DisplayName("create가 입력으로 주어지고_run을 하면_Voucher를 생성하여 저장한다.") - @Test - public void GivenCreateMenuSelected_WhenRun_ThenCreateVoucher() { - // Given - VoucherDto expectedDto = new VoucherDto("FIXED", 10.0); - - given(voucherConsoleIOMock.selectMenu()).willReturn(VoucherMenu.CREATE, VoucherMenu.EXIT); - given(voucherConsoleIOMock.selectVoucherType()).willReturn("FIXED"); - given(voucherConsoleIOMock.readDouble("discount")).willReturn(10.0); - - given(controllerMock.createVoucher(expectedDto)).willReturn(mock(Voucher.class)); - - // When - consoleInterface.run(); - - // Then - ArgumentCaptor argument = ArgumentCaptor.forClass(VoucherDto.class); - then(controllerMock).should().createVoucher(argument.capture()); -// then(controllerMock).should().createVoucher(expectedDto); // error - - VoucherDto actualDto = argument.getValue(); - assertThat(actualDto.getType()).isEqualTo(expectedDto.getType()); - assertThat(actualDto.getDiscountValue()).isEqualTo(expectedDto.getDiscountValue()); - } -} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/CustomerTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/CustomerTest.java index 78d4650f77..9e15bf79e8 100644 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/CustomerTest.java +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/CustomerTest.java @@ -2,7 +2,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; +import com.wonu606.vouchermanager.domain.customer.email.Email; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -16,12 +16,12 @@ class CustomerTest { public void constructor_EmailAddress_Success() { // Given String validAddress = "test@example.com"; - EmailAddress emailAddress = new EmailAddress(validAddress); + Email email = new Email(validAddress); // When - Customer customer = new Customer(emailAddress, "tempNickName"); + Customer customer = new Customer(email, "tempNickName"); // Then - assertThat(customer.getEmailAddress()).isEqualTo(emailAddress.getAddress()); + assertThat(customer.getEmailAddress()).isEqualTo(email.getAddress()); } } diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/EmailAddressTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/EmailTest.java similarity index 78% rename from voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/EmailAddressTest.java rename to voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/EmailTest.java index 7716fcf9bf..09af14a1d7 100644 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/EmailAddressTest.java +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/domain/customer/EmailTest.java @@ -3,23 +3,23 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; +import com.wonu606.vouchermanager.domain.customer.email.Email; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @DisplayName("EmailAddress 테스트") -class EmailAddressTest { +class EmailTest { @DisplayName("생성 시_유효한 이메일주소이라면_객체를 생성한다.") @ParameterizedTest @ValueSource(strings = {"hello@daum.net", "hello@daum.co.kr", "test@example.com"}) public void constructor_ValidEmailAddress_Created(String validAddress) { // When - EmailAddress actualEmailAddress = new EmailAddress(validAddress); + Email actualEmail = new Email(validAddress); // Then - assertThat(actualEmailAddress.getAddress()).isEqualTo(validAddress); + assertThat(actualEmail.getAddress()).isEqualTo(validAddress); } @DisplayName("생성 시_잘못된 이메일주소이라면_예외가 발생한다.") @@ -27,7 +27,7 @@ public void constructor_ValidEmailAddress_Created(String validAddress) { @ValueSource(strings = {"hello.@daum.net", "hello@daum..co.kr", "test@example.com1"}) public void constructor_InvalidEmailAddress_Exception(String invalidAddress) { // When & Then - assertThatThrownBy(() -> new EmailAddress(invalidAddress)) + assertThatThrownBy(() -> new Email(invalidAddress)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("잘못된 이메일 주소입니다: " + invalidAddress); } diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/CustomerJdbcRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/CustomerJdbcRepositoryTest.java new file mode 100644 index 0000000000..54708353da --- /dev/null +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/CustomerJdbcRepositoryTest.java @@ -0,0 +1,92 @@ +package com.wonu606.vouchermanager.repository.customer; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.repository.customer.reader.CustomerJdbcReader; +import com.wonu606.vouchermanager.repository.customer.reader.CustomerReader; +import com.wonu606.vouchermanager.repository.customer.reader.rowmapper.CustomerReaderRowMapperManager; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import com.wonu606.vouchermanager.repository.customer.store.CustomerJdbcStore; +import com.wonu606.vouchermanager.repository.customer.store.CustomerStore; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; + +@JdbcTest +@DisplayName("CustomerResultSetJdbcRepository 테스트") +class CustomerJdbcRepositoryTest { + + private CustomerRepository repository; + + @Autowired + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + @BeforeEach + void setUp() { + CustomerReader reader = new CustomerJdbcReader(namedParameterJdbcTemplate, + new CustomerReaderRowMapperManager()); + CustomerStore store = new CustomerJdbcStore(namedParameterJdbcTemplate); + repository = new CustomerJdbcRepository(reader, store); + } + + @ParameterizedTest + @MethodSource("givenCustomerCreateQuery") + @DisplayName("insert_새 Customer이면_저장된다.") + void insert_NewCustomer_ReturnInsertedCustomer(CustomerCreateQuery query) { + // given + CustomerCreateResultSet expected = new CustomerCreateResultSet(1); + + // when + CustomerCreateResultSet actual = repository.insert( + new CustomerCreateQuery(query.getEmail(), query.getNickname())); + + // then + assertThat(actual.getTaskSuccess()).isEqualTo(expected.getTaskSuccess()); + } + + @ParameterizedTest + @MethodSource("givenCustomerCreateQuery") + @DisplayName("findAll_조건 없음_저장된 모든 Customer들을 반환한다.") + void findAll_NoConditions_ReturnAllCustomers(CustomerCreateQuery query) { + // given + repository.insert(query); + CustomerResultSet expectedResultSet = new CustomerResultSet(query.getEmail(), + query.getNickname()); + + // when + List allCustomers = repository.findAll(); + + // then + assertThat(allCustomers).hasSize(1); + assertThat(allCustomers).usingRecursiveComparison().isEqualTo(List.of(expectedResultSet)); + } + + @ParameterizedTest + @MethodSource("givenCustomerCreateQuery") + @DisplayName("deleteByCustomerId_존재하는 Customer이면_Customer를 제거한다.") + void deleteByCustomerId_ExistingCustomer_CustomerIsDeleted(CustomerCreateQuery query) { + // given + repository.insert(query); + + // then + repository.deleteByCustomerId(query.getEmail()); + List actualAllList = repository.findAll(); + + // when + assertThat(actualAllList).hasSize(0); + } + + static Stream givenCustomerCreateQuery() { + CustomerCreateQuery query = new CustomerCreateQuery("Linlin@onepiece.org", "Big Mom"); + return Stream.of(Arguments.of(query)); + } +} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/JdbcCustomerResultSetRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/JdbcCustomerResultSetRepositoryTest.java deleted file mode 100644 index 8791c14308..0000000000 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/JdbcCustomerResultSetRepositoryTest.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.wonu606.vouchermanager.repository.customer; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerResultSet; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.util.List; -import java.util.Optional; -import javax.sql.DataSource; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; - -@JdbcTest -@DisplayName("JdbcCustomerResultSetRepository 테스트") -class JdbcCustomerResultSetRepositoryTest { - - private JdbcCustomerResultSetRepository jdbcCustomerResultSetRepository; - - @Autowired - private DataSource dataSource; - - @BeforeEach - void setUp() { - jdbcCustomerResultSetRepository = new JdbcCustomerResultSetRepository(dataSource); - } - - @Test - @DisplayName("save_저장되어 있지 않은 Customer면이라면_Customer가 저장된다.") - void save_UnsavedCustomer_CustomerSaved() { - // given - Customer customer = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - - EmailAddress emailAddress = new EmailAddress(customer.getEmailAddress()); - - // when - jdbcCustomerResultSetRepository.save(customer); - var foundCustomer = jdbcCustomerResultSetRepository.findByEmailAddress( - emailAddress); - - // then - assertThat(foundCustomer).isPresent(); - assertThat(foundCustomer.get().getEmailAddress()).isEqualTo(customer.getEmailAddress()); - assertThat(foundCustomer.get().getNickname()).isEqualTo(customer.getNickname()); - } - - @Test - @DisplayName("findByEmailAddress_저장된 Customer라면_Customer가 반환된다.") - void findByEmailAddress_CustomerPresent_ReturnsSameCustomer() { - // given - Customer customer = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - jdbcCustomerResultSetRepository.save(customer); - - EmailAddress emailAddress = new EmailAddress(customer.getEmailAddress()); - - // when - var foundCustomer = - jdbcCustomerResultSetRepository.findByEmailAddress(emailAddress); - - // then - assertThat(foundCustomer).isPresent(); - assertThat(foundCustomer.get().getEmailAddress()).isEqualTo(customer.getEmailAddress()); - } - - @Test - @DisplayName("findByEmailAddress_저장되지 않은 Customer_Empty를 반환한다.") - void findByEmailAddress_UnsavedCustomer_ReturnsEmpty() { - // given - EmailAddress unsavedEmailAddress = new EmailAddress("unsavedEmailAddress@domain.com"); - - // when - var foundCustomer = - jdbcCustomerResultSetRepository.findByEmailAddress(unsavedEmailAddress); - - // then - assertThat(foundCustomer).isNotPresent(); - } - - @Test - @DisplayName("findAll_저장된 모든 Customer_저장된 모든 Customer들을 반환한다.") - void findAll_SavedCustomers_ReturnsAllCustomers() { - // given - Customer customer1 = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - Customer customer2 = new Customer( - new EmailAddress("loopy@onepiece.org"), "Pirate King"); - jdbcCustomerResultSetRepository.save(customer1); - jdbcCustomerResultSetRepository.save(customer2); - - // when - List allCustomers = jdbcCustomerResultSetRepository.findAll(); - - // then - assertThat(allCustomers).hasSize(2); - assertThat(allCustomers).extracting("emailAddress") - .contains(customer1.getEmailAddress(), customer2.getEmailAddress()); - } - - @Test - @DisplayName("deleteByEmailAddress_저장된 Customer_Customer를 제거한다.") - void deleteByEmailAddress_SavedCustomer_CustomerDeleted() { - // given - Customer customer = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - jdbcCustomerResultSetRepository.save(customer); - - EmailAddress emailAddress = new EmailAddress(customer.getEmailAddress()); - - // then - jdbcCustomerResultSetRepository.deleteByEmailAddress(emailAddress); - Optional foundCustomer = jdbcCustomerResultSetRepository - .findByEmailAddress(emailAddress); - - // when - assertThat(foundCustomer).isNotPresent(); - } - - @Test - @DisplayName("deleteAll_저장된 모든 Customer_모든 Customer를 제거한다.") - void deleteAll_MultipleCustomers_AllCustomersDeleted() { - // given - Customer customer1 = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - Customer customer2 = new Customer( - new EmailAddress("loopy@onepiece.org"), "Pirate King"); - jdbcCustomerResultSetRepository.save(customer1); - jdbcCustomerResultSetRepository.save(customer2); - - // then - jdbcCustomerResultSetRepository.deleteAll(); - List allCustomers = jdbcCustomerResultSetRepository.findAll(); - - // when - assertThat(allCustomers).isEmpty(); - } -} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/MappingCustomerRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/MappingCustomerRepositoryTest.java deleted file mode 100644 index 907e9eb860..0000000000 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customer/MappingCustomerRepositoryTest.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.wonu606.vouchermanager.repository.customer; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.CustomerResultSet; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -@DisplayName("MappingCustomerRepository 테스트") -class MappingCustomerRepositoryTest { - - private CustomerResultSetRepository customerResultSetRepository; - private MappingCustomerRepository customerRepository; - - static Stream givenCustomers() { - Customer customer1 = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - Customer customer2 = new Customer( - new EmailAddress("loopy@onepiece.org"), "Pirate King"); - - return Stream.of(Arguments.of(customer1, customer2)); - } - - @BeforeEach - void setUp() { - customerResultSetRepository = mock(CustomerResultSetRepository.class); - customerRepository = new MappingCustomerRepository(customerResultSetRepository); - } - - @Test - @DisplayName("save_Customer_Customer를 저장하고 반환한다.") - void save_VoucherProvided_ReturnVoucher() { - // given - Customer customer = mock(Customer.class); - - // then - customerRepository.save(customer); - - // when - verify(customerResultSetRepository, times(1)).save(customer); - } - - @Test - @DisplayName("findByEmailAddress_존재하는 이메일 주소라면_Customer로 변환하여 반환한다.") - void findByEmailAddress_ExistingEmailAddress_ReturnsExpectedCustomer() { - // given - Customer customer = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - CustomerResultSet enteredResultSet = new CustomerResultSet( - customer.getEmailAddress(), - customer.getNickname(), - null, - null - ); - - EmailAddress emailAddress = new EmailAddress(customer.getEmailAddress()); - - given(customerResultSetRepository.findByEmailAddress(emailAddress)) - .willReturn(Optional.of(enteredResultSet)); - - // when - Optional actualCustomer = customerRepository.findByEmailAddress(emailAddress); - - // then - assertThat(actualCustomer.isPresent()).isTrue(); - assertThat(actualCustomer.get()).isEqualTo(customer); - } - - @ParameterizedTest - @MethodSource("givenCustomers") - @DisplayName("findAll_빈 인수_저장된 모든 Customer를 반환한다.") - void findAll_EmptyArgument_SavedAllCustomer(Customer customer1, Customer customer2) { - // given - List expectedCustomerList = new ArrayList<>(); - expectedCustomerList.add(customer1); - expectedCustomerList.add(customer2); - - CustomerResultSet expectedResultSet1 = new CustomerResultSet( - customer1.getEmailAddress(), customer1.getNickname(), null, null); - CustomerResultSet expectedResultSet2 = new CustomerResultSet( - customer2.getEmailAddress(), customer2.getNickname(), null, null); - - List expectedVoucherResultSetList = new ArrayList<>(); - expectedVoucherResultSetList.add(expectedResultSet1); - expectedVoucherResultSetList.add(expectedResultSet2); - - given(customerResultSetRepository.findAll()).willReturn(expectedVoucherResultSetList); - - // then - List actualCustomerList = customerRepository.findAll(); - - // when - assertThat(actualCustomerList).hasSameSizeAs(expectedCustomerList); - assertThat(actualCustomerList).containsExactlyInAnyOrderElementsOf(expectedCustomerList); - } - - @Test - @DisplayName("deleteByEmailAddress_이메일 주소_같은 이메일 주소를 반환한다.") - void deleteByEmailAddress_EmailAddress_ReturnsSameEmailAddress() { - // given - EmailAddress expectedEmailAddress = new EmailAddress("Linlin@onepiece.org"); - - // when - customerRepository.deleteByEmailAddress(expectedEmailAddress); - - // then - verify(customerResultSetRepository, times(1)).deleteByEmailAddress(expectedEmailAddress); - } - - @Test - @DisplayName("deleteAll_빈 인자_ResultSetRepository의 deleeteAll을 실행한다.") - void deleteAll_EmptyArgument_ExecuteVoucherResultSetRepositoryDeleteAllMethod() { - // when - customerRepository.deleteAll(); - - // then - verify(customerResultSetRepository, times(1)).deleteAll(); - } -} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/JdbcCustomerVoucherWalletRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/JdbcCustomerVoucherWalletRepositoryTest.java deleted file mode 100644 index 1d94301977..0000000000 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/customerVoucherWallet/JdbcCustomerVoucherWalletRepositoryTest.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.wonu606.vouchermanager.repository.customerVoucherWallet; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.wonu606.vouchermanager.domain.CustomerVoucherWallet.CustomerVoucherWallet; -import com.wonu606.vouchermanager.domain.customer.Customer; -import com.wonu606.vouchermanager.domain.customer.emailAddress.EmailAddress; -import com.wonu606.vouchermanager.domain.voucher.FixedAmountVoucher; -import com.wonu606.vouchermanager.domain.voucher.PercentageVoucher; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.FixedAmountValue; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.PercentageDiscountValue; -import com.wonu606.vouchermanager.repository.customer.JdbcCustomerResultSetRepository; -import com.wonu606.vouchermanager.repository.voucher.JdbcVoucherResultSetRepository; -import java.util.List; -import java.util.UUID; -import java.util.stream.Stream; -import javax.sql.DataSource; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; - -@JdbcTest -@DisplayName("JdbcCustomerVoucherWalletRepository 테스트") -class JdbcCustomerVoucherWalletRepositoryTest { - - private JdbcCustomerVoucherWalletRepository customerVoucherWalletRepository; - - @Autowired - private DataSource dataSource; - - @BeforeEach - void setUp() { - customerVoucherWalletRepository = new JdbcCustomerVoucherWalletRepository(dataSource); - - JdbcVoucherResultSetRepository voucherResultSetRepository = - new JdbcVoucherResultSetRepository(dataSource); - JdbcCustomerResultSetRepository customerResultSetRepository = - new JdbcCustomerResultSetRepository(dataSource); - - givenCustomerVouchers().forEach( - arguments -> { - Customer customer = (Customer) arguments.get()[0]; - Voucher voucher = (Voucher) arguments.get()[1]; - customerResultSetRepository.save(customer); - voucherResultSetRepository.save(voucher); - - customerVoucherWalletRepository.save( - new CustomerVoucherWallet( - voucher.getUuid(), - new EmailAddress(customer.getEmailAddress()))); - } - ); - } - - @ParameterizedTest - @MethodSource("givenCustomerVouchers") - @DisplayName("findVoucherIdByCustomerEmailAddress_저장된 이메일 주소라면_매핑된 Voucher id를 반환한다.") - void findVoucherIdByCustomerEmailAddress_SavedEmailAddress_ReturnsVoucherIds( - Customer customer, Voucher voucher) { - // given - EmailAddress emailAddress = new EmailAddress(customer.getEmailAddress()); - - // when - List actualUuids = - customerVoucherWalletRepository.findIdByCustomerEmailAddress(emailAddress); - - // then - assertThat(actualUuids).hasSize(1); - assertThat(actualUuids.get(0)).isEqualTo(voucher.getUuid()); - } - - @ParameterizedTest - @MethodSource("givenCustomerVouchers") - @DisplayName("deleteByCustomerVoucherWallet_저장된 월렛이면_해당 월렛을 제거한다.") - void deleteByCustomerVoucherWallet_SavedWallet_Deleted(Customer customer, Voucher voucher) { - // given - CustomerVoucherWallet customerVoucherWallet = - new CustomerVoucherWallet( - voucher.getUuid(), - new EmailAddress(customer.getEmailAddress())); - - // when - customerVoucherWalletRepository.deleteByWallet(customerVoucherWallet); - - // then - List emailAddresses = - customerVoucherWalletRepository.findEmailAddressesByVoucherId(voucher.getUuid()); - assertThat(emailAddresses).hasSize(0); - } - - @ParameterizedTest - @MethodSource("givenCustomerVouchers") - @DisplayName("findEmailAddressesByVoucherId_할당된 VoucherId라면_가지고 있는 Customer의 EmailAddress 반환한다.") - void findEmailAddressesByVoucherId_SavedVoucherId_ReturnsEmailAddresses( - Customer customer, Voucher voucher) { - // given - UUID voucherId = voucher.getUuid(); - - // when - List emailAddresses = - customerVoucherWalletRepository.findEmailAddressesByVoucherId(voucherId); - - // then - assertThat(emailAddresses).hasSize(1); - assertThat(emailAddresses.get(0)).isEqualTo(customer.getEmailAddress()); - } - - static Stream givenCustomerVouchers() { - Customer customer1 = new Customer( - new EmailAddress("Linlin@onepiece.org"), "Big Mom"); - Customer customer2 = new Customer( - new EmailAddress("loopy@onepiece.org"), "Pirate King"); - - UUID uuid1 = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - UUID uuid2 = UUID.fromString("123e4567-e89b-12d3-a456-426614174001"); - Voucher voucher1 = new PercentageVoucher( - uuid1, new PercentageDiscountValue(50.0)); - Voucher voucher2 = new FixedAmountVoucher( - uuid2, new FixedAmountValue(50.0)); - - return Stream.of( - Arguments.of(customer1, voucher1), - Arguments.of(customer2, voucher2)); - } -} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/JdbcVoucherResultSetVoucherRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/JdbcVoucherResultSetVoucherRepositoryTest.java deleted file mode 100644 index 3e4d63d8cb..0000000000 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/JdbcVoucherResultSetVoucherRepositoryTest.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.wonu606.vouchermanager.repository.voucher; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.wonu606.vouchermanager.domain.voucher.FixedAmountVoucher; -import com.wonu606.vouchermanager.domain.voucher.PercentageVoucher; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherResultSet; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.FixedAmountValue; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.PercentageDiscountValue; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import javax.sql.DataSource; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; - -@JdbcTest -@DisplayName("JdbcVoucherResultSetRepository 테스트") -class JdbcVoucherResultSetVoucherRepositoryTest { - - private JdbcVoucherResultSetRepository jdbcVoucherResultSetRepository; - - @Autowired - private DataSource dataSource; - - @BeforeEach - void setUp() { - jdbcVoucherResultSetRepository = new JdbcVoucherResultSetRepository(dataSource); - } - - @Test - @DisplayName("저장 시_저장되어 있지 않은 Voucher라면_Voucher가 저장된다.") - void save_UnsavedVoucher_VoucherSaved() { - // given - Voucher voucher = new FixedAmountVoucher( - UUID.randomUUID(), new FixedAmountValue(50.0)); - - // when - jdbcVoucherResultSetRepository.save(voucher); - var result = jdbcVoucherResultSetRepository.findById(voucher.getUuid()); - - // then - assertThat(result).isPresent(); - assertThat(result.get().getUuid()).isEqualTo(voucher.getUuid()); - assertThat(result.get().getSimpleName()).isEqualTo(voucher.getClass().getSimpleName()); - assertThat(result.get().getDiscountValue()).isEqualTo(voucher.getDiscountValue()); - } - - @Test - @DisplayName("findById_저장된 Voucher라면_해당 Voucher를 반환한다.") - void findById_savedVoucher_ReturnsVoucher() { - // given - Voucher voucher = new FixedAmountVoucher( - UUID.randomUUID(), new FixedAmountValue(50.0)); - jdbcVoucherResultSetRepository.save(voucher); - - // when - Optional foundVoucher = jdbcVoucherResultSetRepository.findById( - voucher.getUuid()); - - // then - assertThat(foundVoucher).isPresent(); - assertThat(foundVoucher.get().getUuid()).isEqualTo(voucher.getUuid()); - } - - @Test - @DisplayName("findById_저장되지 않은 Voucher_Empty를 반환한다.") - void findById_UnsavedVoucher_ReturnsEmpty() { - // given - UUID nonExistentId = UUID.randomUUID(); - - // when - Optional foundVoucher = jdbcVoucherResultSetRepository.findById( - nonExistentId); - - // then - assertThat(foundVoucher).isNotPresent(); - } - - @Test - @DisplayName("findAll_저장된 Vouchers_저장된 모든 Vouchers들을 반환한다.") - void findAll_SavedVouchers_ReturnsAllVouchers() { - // given - Voucher voucher1 = new FixedAmountVoucher( - UUID.randomUUID(), new FixedAmountValue(50.0)); - Voucher voucher2 = new PercentageVoucher( - UUID.randomUUID(), new PercentageDiscountValue(30.0)); - jdbcVoucherResultSetRepository.save(voucher1); - jdbcVoucherResultSetRepository.save(voucher2); - - // when - List allVouchers = jdbcVoucherResultSetRepository.findAll(); - - // then - assertThat(allVouchers).hasSize(2); - assertThat(allVouchers).extracting("uuid").contains(voucher1.getUuid(), voucher2.getUuid()); - } - - @Test - @DisplayName("deleteById_저장된 Vocuher_Voucher를 제거한다.") - void deleteById_SavedVoucher_VoucherDeleted() { - // given - Voucher voucher = new FixedAmountVoucher( - UUID.randomUUID(), new FixedAmountValue(50.0)); - jdbcVoucherResultSetRepository.save(voucher); - - // then - jdbcVoucherResultSetRepository.deleteById(voucher.getUuid()); - Optional foundVoucher = jdbcVoucherResultSetRepository.findById( - voucher.getUuid()); - - // when - assertThat(foundVoucher).isNotPresent(); - } - - @Test - @DisplayName("deleteAll_저장된 모든 Voucher_모든 Voucher를 제거한다.") - void deleteAll_SavedMultipleVouchers_AllVouchersDeleted() { - // given - Voucher voucher1 = new FixedAmountVoucher( - UUID.randomUUID(), new FixedAmountValue(50.0)); - Voucher voucher2 = new PercentageVoucher( - UUID.randomUUID(), new PercentageDiscountValue(30.0)); - jdbcVoucherResultSetRepository.save(voucher1); - jdbcVoucherResultSetRepository.save(voucher2); - - // then - jdbcVoucherResultSetRepository.deleteAll(); - List allVouchers = jdbcVoucherResultSetRepository.findAll(); - - // when - assertThat(allVouchers).isEmpty(); - } -} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/LocalMemoryVoucherVoucherRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/LocalMemoryVoucherVoucherRepositoryTest.java deleted file mode 100644 index ee1beb4a56..0000000000 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/LocalMemoryVoucherVoucherRepositoryTest.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.wonu606.vouchermanager.repository.voucher; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import com.wonu606.vouchermanager.domain.voucher.PercentageVoucher; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.PercentageDiscountValue; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.springframework.dao.DuplicateKeyException; - -@DisplayName("LocalMemoryVoucherRepository 테스트") -public class LocalMemoryVoucherVoucherRepositoryTest { - - private LocalMemoryVoucherVoucherRepository repository; - private Voucher voucher; - - @BeforeEach - public void setUp() { - repository = new LocalMemoryVoucherVoucherRepository(); - - UUID uuid = UUID.randomUUID(); - PercentageDiscountValue discountValue = new PercentageDiscountValue(20); - voucher = new PercentageVoucher(uuid, discountValue); - } - - @AfterEach - public void tearDown() { - repository.deleteAll(); - } - - @DisplayName("Save 메소드") - @Nested - public class SaveMethod { - - @DisplayName("유효한 바우처이면_저장된바우처가 반환된다.") - @Test - public void ValidVoucher_ReturnsSavedVoucher() { - // When - Voucher savedVoucher = repository.save(voucher); - - // Then - assertThat(savedVoucher).isEqualTo(voucher); - } - - @DisplayName("중복된 UUID 바우처라면_예외가발생한다.") - @Test - public void DuplicateUUIDVoucher_ThrowsException() { - // Given - repository.save(voucher); - - // When & Then - assertThatThrownBy(() -> repository.save(voucher)) - .isInstanceOf(DuplicateKeyException.class) - .hasMessage("이미 존재하는 바우처의 uuid입니다. [uuid]: " + voucher.getUuid()); - } - } - - @DisplayName("findById 메소드") - @Nested - public class FindByIdMethod { - - @DisplayName("유효한 UUID이면_바우처가 반환된다.") - @Test - public void ValidUUID_ReturnsVoucher() { - // Given - repository.save(voucher); - - // When - Optional foundVoucher = repository.findById(voucher.getUuid()); - - // Then - assertThat(foundVoucher).isPresent(); - assertThat(foundVoucher.get()).isEqualTo(voucher); - } - - @DisplayName("존재하지않는 UUID라면_빈 Optional이 반환된다.") - @Test - public void NonexistentUUID_ReturnsEmptyOptional() { - // Given - UUID uuid = UUID.randomUUID(); - - // When - Optional foundVoucher = repository.findById(uuid); - - // Then - assertThat(foundVoucher).isEmpty(); - } - } - - @DisplayName("findAll 메소드") - @Nested - public class FindAllMethod { - - @DisplayName("모든 바우처를 가져온다.") - @Test - public void ReturnsAllVouchers() { - // Given - UUID uuid2 = UUID.randomUUID(); - PercentageDiscountValue discountValue2 = new PercentageDiscountValue(30); - Voucher voucher2 = new PercentageVoucher(uuid2, discountValue2); - - repository.save(voucher); - repository.save(voucher2); - - // When - List allVouchers = repository.findAll(); - - // Then - assertThat(allVouchers.size()).isEqualTo(2); - assertThat(allVouchers).containsExactlyInAnyOrder(voucher, voucher2); - } - } - - @DisplayName("deleteById 메소드") - @Nested - public class DeleteByIdMethod { - - @DisplayName("UUID가 존재한다면_UUID에 해당하는 바우처를 제거한다.") - @Test - public void ExistingUUID_RemovesVoucher() { - // Given - repository.save(voucher); - - // When - repository.deleteById(voucher.getUuid()); - - // Then - assertThat(repository.findById(voucher.getUuid())).isEmpty(); - } - - @DisplayName("UUID가 존재하지 않는다면_아무 일도 일어나지 않는다.") - @Test - public void NonExistingUUID_ThrowsException() { - // Given - UUID nonExistingUUID = UUID.randomUUID(); - - // When & Then - repository.deleteById(nonExistingUUID); - } - } -} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/MappingVoucherRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/MappingVoucherRepositoryTest.java deleted file mode 100644 index 39ca399d0c..0000000000 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/MappingVoucherRepositoryTest.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.wonu606.vouchermanager.repository.voucher; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import com.wonu606.vouchermanager.domain.voucher.FixedAmountVoucher; -import com.wonu606.vouchermanager.domain.voucher.PercentageVoucher; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherResultSet; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.FixedAmountValue; -import com.wonu606.vouchermanager.domain.voucher.discountvalue.PercentageDiscountValue; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -@DisplayName("MappingVoucherRepository 테스트") -class MappingVoucherRepositoryTest { - - private VoucherResultSetRepository voucherResultSetRepository; - - private MappingVoucherRepository voucherRepository; - - static Stream givenVouchers() { - Voucher expectedVoucher1 = new PercentageVoucher( - UUID.randomUUID(), new PercentageDiscountValue(50.0)); - Voucher expectedVoucher2 = new FixedAmountVoucher( - UUID.randomUUID(), new FixedAmountValue(50.0)); - - return Stream.of(Arguments.of(expectedVoucher1, expectedVoucher2)); - } - - @BeforeEach - void setUp() { - voucherResultSetRepository = mock(VoucherResultSetRepository.class); - voucherRepository = new MappingVoucherRepository(voucherResultSetRepository); - } - - @Test - @DisplayName("save_바우처_바우처를 저장하고 반환한다.") - void save_VoucherProvided_ReturnVoucher() { - // given - Voucher voucher = mock(Voucher.class); - - // then - voucherRepository.save(voucher); - - // when - verify(voucherResultSetRepository, times(1)).save(voucher); - } - - @Test - @DisplayName("findById_존재하는 uuid_바우처로 변환하여 반환한다.") - void findById_ExistingUuid_ReturnsExpectedVoucher() { - // given - FixedAmountVoucher expectedVoucher = new FixedAmountVoucher( - UUID.randomUUID(), new FixedAmountValue(50.0) - ); - VoucherResultSet enteredResultSet = new VoucherResultSet( - expectedVoucher.getUuid(), - expectedVoucher.getClass().getSimpleName(), - expectedVoucher.getDiscountValue() - ); - - given(voucherResultSetRepository.findById(expectedVoucher.getUuid())) - .willReturn(Optional.of(enteredResultSet)); - - // when - Optional actualVoucher = voucherRepository.findById(expectedVoucher.getUuid()); - - // then - assertThat(actualVoucher.isPresent()).isTrue(); - assertThat(actualVoucher.get()).isEqualTo(expectedVoucher); - } - - @ParameterizedTest - @MethodSource("givenVouchers") - @DisplayName("findAll_빈 인수_저장된 모든 바우처를 반환한다.") - void findAll_EmptyArgument_SavedAllVoucher(Voucher expectedVoucher1, Voucher expectedVoucher2) { - // given - List expectedVoucherList = new ArrayList<>(); - expectedVoucherList.add(expectedVoucher1); - expectedVoucherList.add(expectedVoucher2); - - VoucherResultSet expectedResultSet1 = new VoucherResultSet( - expectedVoucher1.getUuid(), expectedVoucher1.getClass().getSimpleName(), - expectedVoucher1.getDiscountValue()); - VoucherResultSet expectedResultSet2 = new VoucherResultSet( - expectedVoucher2.getUuid(), expectedVoucher2.getClass().getSimpleName(), - expectedVoucher2.getDiscountValue()); - - List expectedVoucherResultSetList = new ArrayList<>(); - expectedVoucherResultSetList.add(expectedResultSet1); - expectedVoucherResultSetList.add(expectedResultSet2); - - given(voucherResultSetRepository.findAll()).willReturn(expectedVoucherResultSetList); - - // then - List actualVoucherList = voucherRepository.findAll(); - - // when - assertThat(actualVoucherList).hasSameSizeAs(expectedVoucherList); - assertThat(actualVoucherList).containsExactlyInAnyOrderElementsOf(expectedVoucherList); - } - - @Test - @DisplayName("deleteById_UUID_같은 UUID를 반환한다.") - void deleteById_Uuid_ReturnsSameUuid() { - // given - UUID expectedUuid = UUID.randomUUID(); - - // when - voucherRepository.deleteById(expectedUuid); - - // then - verify(voucherResultSetRepository, times(1)).deleteById(expectedUuid); - } - - @Test - @DisplayName("deleteAll_빈 인자_ResultSetRepository의 deleeteAll을 실행한다.") - void deleteAll_EmptyArgument_ExecuteVoucherResultSetRepositoryDeleteAllMethod() { - // when - voucherRepository.deleteAll(); - - // then - verify(voucherResultSetRepository, times(1)).deleteAll(); - } -} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/VoucherJdbcRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/VoucherJdbcRepositoryTest.java new file mode 100644 index 0000000000..21b7b7d5d8 --- /dev/null +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucher/VoucherJdbcRepositoryTest.java @@ -0,0 +1,117 @@ +package com.wonu606.vouchermanager.repository.voucher; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.wonu606.vouchermanager.repository.voucher.query.VoucherDeleteQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherFindQuery; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.reader.VoucherJdbcReader; +import com.wonu606.vouchermanager.repository.voucher.reader.VoucherReader; +import com.wonu606.vouchermanager.repository.voucher.reader.rowmapper.VoucherReaderRowMapperManager; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import com.wonu606.vouchermanager.repository.voucher.store.VoucherJdbcStore; +import com.wonu606.vouchermanager.repository.voucher.store.VoucherStore; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; + +@JdbcTest +@DisplayName("VoucherResultSetJdbcRepository 테스트") +class VoucherJdbcRepositoryTest { + + private VoucherRepository voucherJdbcRepository; + + @Autowired + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + @BeforeEach + void setUp() { + VoucherReader reader = new VoucherJdbcReader(namedParameterJdbcTemplate, + new VoucherReaderRowMapperManager()); + VoucherStore store = new VoucherJdbcStore(namedParameterJdbcTemplate); + voucherJdbcRepository = new VoucherJdbcRepository(reader, store); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("insert_새 Voucher라면_저장된다.") + public void insert_NewVoucher_ReturnInsertedVoucher(VoucherInsertQuery query) { + // Given + VoucherInsertResultSet expected = new VoucherInsertResultSet(1); + + // When + VoucherInsertResultSet actual = voucherJdbcRepository.insert(query); + + // Then + Assertions.assertThat(actual.getAffectedRowsCount()) + .isEqualTo(expected.getAffectedRowsCount()); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("findById_존재하는 Voucher라면_해당 Voucher를 반환한다.") + public void findById_ExistingVoucher_ReturnVoucher(VoucherInsertQuery query) { + // Given + voucherJdbcRepository.insert(query); + VoucherFindQuery voucherFindQuery = new VoucherFindQuery(query.getVoucherId()); + VoucherResultSet expected = new VoucherResultSet(query.getVoucherClassSimpleName(), + query.getVoucherId(), query.getDiscountValue()); + + // When + Optional actual = voucherJdbcRepository.findById(voucherFindQuery); + + // Then + assertThat(actual.isPresent()).isTrue(); + assertThat(actual.get()).usingRecursiveComparison().isEqualTo(expected); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("findAll_조건 없음_모든 바우처를 반환한다.") + public void findAll_NoConditions_ReturnAllVouchers(VoucherInsertQuery query) { + // Given + voucherJdbcRepository.insert(query); + VoucherResultSet expectedResultSet = new VoucherResultSet( + query.getVoucherClassSimpleName(), query.getVoucherId(), + query.getDiscountValue()); + + // When + List actual = voucherJdbcRepository.findAll(); + + // Then + assertThat(actual).hasSize(1); + assertThat(actual).usingRecursiveComparison().isEqualTo(List.of(expectedResultSet)); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + public void deleteById_ExistingVoucher_VoucherIsDeleted(VoucherInsertQuery query) { + // Given + voucherJdbcRepository.insert(query); + VoucherDeleteQuery voucherDeleteQuery = new VoucherDeleteQuery(query.getVoucherId()); + + // When + voucherJdbcRepository.deleteById(voucherDeleteQuery); + List actual = voucherJdbcRepository.findAll(); + + // Then + assertThat(actual).hasSize(0); + } + + static Stream givenVoucherInsertQuery() { + VoucherInsertQuery voucherInsertQuery = new VoucherInsertQuery("fixed", + "eaea93d1-08e4-4311-ad44-7b6a626c1a71", 10.0); + + return Stream.of(Arguments.of(voucherInsertQuery)); + } +} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletJdbcRepositoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletJdbcRepositoryTest.java new file mode 100644 index 0000000000..5e71b41fac --- /dev/null +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/repository/voucherwallet/VoucherWalletJdbcRepositoryTest.java @@ -0,0 +1,183 @@ +package com.wonu606.vouchermanager.repository.voucherwallet; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.wonu606.vouchermanager.repository.voucher.VoucherJdbcRepository; +import com.wonu606.vouchermanager.repository.voucher.VoucherRepository; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.reader.VoucherJdbcReader; +import com.wonu606.vouchermanager.repository.voucher.reader.VoucherReader; +import com.wonu606.vouchermanager.repository.voucher.reader.rowmapper.VoucherReaderRowMapperManager; +import com.wonu606.vouchermanager.repository.voucher.store.VoucherJdbcStore; +import com.wonu606.vouchermanager.repository.voucher.store.VoucherStore; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedCustomersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.OwnedVouchersQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.reader.VoucherWalletJdbcReader; +import com.wonu606.vouchermanager.repository.voucherwallet.reader.VoucherWalletReader; +import com.wonu606.vouchermanager.repository.voucherwallet.reader.rowmapper.VoucherWalletReaderRowMapperManager; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.store.VoucherWalletJdbcStore; +import com.wonu606.vouchermanager.repository.voucherwallet.store.VoucherWalletStore; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; + +@JdbcTest +@DisplayName("CustomerVoucherWalletJdbcRepository 테스트") +class VoucherWalletJdbcRepositoryTest { + + private VoucherWalletRepository voucherWalletRepository; + private VoucherRepository voucherRepository; + + @Autowired + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + @BeforeEach + void setUp() { + VoucherWalletReader voucherWalletReader = new VoucherWalletJdbcReader( + namedParameterJdbcTemplate, + new VoucherWalletReaderRowMapperManager()); + VoucherWalletStore voucherWalletJdbcStore = new VoucherWalletJdbcStore( + namedParameterJdbcTemplate); + voucherWalletRepository = new VoucherWalletJdbcRepository(voucherWalletReader, + voucherWalletJdbcStore); + + VoucherReader voucherReader = new VoucherJdbcReader(namedParameterJdbcTemplate, + new VoucherReaderRowMapperManager()); + VoucherStore voucherStore = new VoucherJdbcStore(namedParameterJdbcTemplate); + voucherRepository = new VoucherJdbcRepository(voucherReader, voucherStore); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("findOwnedVouchersByCustomer_존재하는 Customer라면_소유한 VoucherList 반환한다.") + void findOwnedVouchersByCustomer_ExistingCustomer_ReturnOwnedVouchers( + VoucherInsertQuery voucherInsertQuery) { + // Given + voucherRepository.insert(voucherInsertQuery); + voucherWalletRepository.insert(new WalletInsertQuery(voucherInsertQuery.getVoucherId())); + + OwnedVouchersQuery ownedVouchersQuery = new OwnedVouchersQuery("test@test.org"); + voucherWalletRepository.register( + new WalletRegisterQuery(ownedVouchersQuery.getCustomerId(), + voucherInsertQuery.getVoucherId())); + + OwnedVoucherResultSet ownedVoucherResultSet = new OwnedVoucherResultSet( + voucherInsertQuery.getVoucherId()); + + // When + List actual = voucherWalletRepository.findOwnedVouchersByCustomer( + ownedVouchersQuery); + + // Assert + assertThat(actual).usingRecursiveComparison().isEqualTo(List.of(ownedVoucherResultSet)); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("findOwnedCustomersByVoucher_존재하는 Voucher라면_소유한 CustomerList 반환한다.") + void findOwnedCustomersByVoucher_ExistingVoucher_ReturnOwnedCustomers( + VoucherInsertQuery voucherInsertQuery) { + // Given + voucherRepository.insert(voucherInsertQuery); + voucherWalletRepository.insert(new WalletInsertQuery(voucherInsertQuery.getVoucherId())); + OwnedCustomersQuery ownedCustomersQuery = new OwnedCustomersQuery( + voucherInsertQuery.getVoucherId()); + String customerId = "test@test.org"; + voucherWalletRepository.register( + new WalletRegisterQuery(customerId, voucherInsertQuery.getVoucherId())); + + // When + List actual = voucherWalletRepository.findOwnedCustomersByVoucher( + ownedCustomersQuery); + + // Then + assertThat(actual).hasSize(1); + assertThat(actual).usingRecursiveComparison() + .isEqualTo(List.of(new OwnedCustomerResultSet(customerId))); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("delete_존재하는 Wallet이라면_삭제한다.") + void delete_ExistingWallet_WalletIsDeleted(VoucherInsertQuery voucherInsertQuery) { + // Given + voucherRepository.insert(voucherInsertQuery); + voucherWalletRepository.insert(new WalletInsertQuery(voucherInsertQuery.getVoucherId())); + OwnedCustomersQuery ownedCustomersQuery = new OwnedCustomersQuery( + voucherInsertQuery.getVoucherId()); + String customerId = "test@test.org"; + voucherWalletRepository.register( + new WalletRegisterQuery(customerId, voucherInsertQuery.getVoucherId())); + WalletDeleteQuery walletDeleteQuery = new WalletDeleteQuery(customerId, + voucherInsertQuery.getVoucherId()); + + // When + voucherWalletRepository.delete(walletDeleteQuery); + List actual = voucherWalletRepository.findOwnedCustomersByVoucher( + ownedCustomersQuery); + + // Then + assertThat(actual).hasSize(0); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("insert_새로 기입_기입한 정보가 저장된다.") + void insert_NewEntry_EntryIsInserted(VoucherInsertQuery voucherInsertQuery) { + // Given + voucherRepository.insert(voucherInsertQuery); + voucherWalletRepository.insert(new WalletInsertQuery(voucherInsertQuery.getVoucherId())); + WalletInsertResultSet expected = new WalletInsertResultSet(1); + WalletInsertQuery walletInsertQuery = new WalletInsertQuery( + voucherInsertQuery.getVoucherId()); + + // When + WalletInsertResultSet actual = voucherWalletRepository.insert(walletInsertQuery); + + // Then + assertThat(actual).usingRecursiveComparison().isEqualTo(expected); + } + + @ParameterizedTest + @MethodSource("givenVoucherInsertQuery") + @DisplayName("register_존재하는 빈 Wallet이라면_기입한 정보가 업데이트된다.") + public void register_ExistingEmptyWallet_EntryIsRegistered( + VoucherInsertQuery voucherInsertQuery) { + // Given + voucherRepository.insert(voucherInsertQuery); + voucherWalletRepository.insert(new WalletInsertQuery(voucherInsertQuery.getVoucherId())); + String customerId = "test@test.org"; + WalletRegisterQuery walletRegisterQuery = new WalletRegisterQuery(customerId, + voucherInsertQuery.getVoucherId()); + OwnedCustomerResultSet expectedResultSet = new OwnedCustomerResultSet(customerId); + + // When + voucherWalletRepository.register(walletRegisterQuery); + List actualList = voucherWalletRepository.findOwnedCustomersByVoucher( + new OwnedCustomersQuery(voucherInsertQuery.getVoucherId())); + + // Then + assertThat(actualList).usingRecursiveFieldByFieldElementComparator() + .contains(expectedResultSet); + } + + static Stream givenVoucherInsertQuery() { + VoucherInsertQuery voucherInsertQuery = new VoucherInsertQuery("fixed", + "eaea93d1-08e4-4311-ad44-7b6a626c1a71", 10.0); + + return Stream.of(Arguments.of(voucherInsertQuery)); + } +} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/customer/CustomerServiceTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/customer/CustomerServiceTest.java new file mode 100644 index 0000000000..31d917b8d9 --- /dev/null +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/customer/CustomerServiceTest.java @@ -0,0 +1,139 @@ +package com.wonu606.vouchermanager.service.customer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; + +import com.wonu606.vouchermanager.repository.customer.CustomerRepository; +import com.wonu606.vouchermanager.repository.customer.query.CustomerCreateQuery; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerCreateResultSet; +import com.wonu606.vouchermanager.repository.customer.resultset.CustomerResultSet; +import com.wonu606.vouchermanager.service.customer.converter.CustomerServiceConverterManager; +import com.wonu606.vouchermanager.service.customer.factory.CustomerFactory; +import com.wonu606.vouchermanager.service.customer.param.CustomerCreateParam; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.service.customer.result.CustomerResult; +import com.wonu606.vouchermanager.service.voucherwallet.VoucherWalletService; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; +import java.util.List; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +@DisplayName("CustomerService 테스트") +class CustomerServiceTest { + + private VoucherWalletService voucherWalletService; + private CustomerRepository repository; + + private CustomerService customerService; + + @BeforeEach + void setUp() { + repository = mock(CustomerRepository.class); + voucherWalletService = mock(VoucherWalletService.class); + customerService = new CustomerService(voucherWalletService, repository, + new CustomerFactory(), new CustomerServiceConverterManager()); + } + + @Test + @DisplayName("createCustomer_유효한 파라미터이면_Customer를 생성한다.") + void createCustomer_WithValidParameters_CustomerIsCreated() { + // Given + String customerEmail = "test@test.org"; + String customerNickname = "test"; + CustomerCreateParam param = new CustomerCreateParam(customerEmail, customerNickname); + given(repository.insert(any())).willReturn(new CustomerCreateResultSet(1)); + CustomerCreateQuery excepted = new CustomerCreateQuery(customerEmail, customerNickname); + + // When + customerService.createCustomer(param); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + CustomerCreateQuery.class); + then(repository).should().insert(argument.capture()); + CustomerCreateQuery actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(excepted); + } + + @Test + @DisplayName("getCustomerList_존재하는 Customer라면_CustomerList를 반환한다.") + void getCustomerList_WhenCustomersExist_ReturnsListOfCustomers() { + // Given + List resultSets = List.of( + new CustomerResultSet("test@test.com", "nickname")); + given(repository.findAll()).willReturn(resultSets); + + List expectedResult = List.of( + new CustomerResult("test@test.com", "nickname")); + + // When + List actualResult = customerService.getCustomerList(); + + // Then + assertThat(actualResult).hasSize(1); + assertThat(actualResult).usingRecursiveComparison().isEqualTo(expectedResult); + } + + @Test + @DisplayName("findOwnedVouchersByCustomer_유효한 CustomerId라면_Voucher List를 반환한다.") + void findOwnedVouchersByCustomer_WithValidCustomerId_ReturnsListOfVouchers() { + // Given + List expectedResults = List.of(new OwnedVoucherResult("voucherId")); + given(voucherWalletService.findOwnedVouchersByCustomer(any())).willReturn(expectedResults); + OwnedVouchersParam expected = new OwnedVouchersParam("customerId"); + + // When + customerService.findOwnedVouchersByCustomer(expected); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + OwnedVouchersParam.class); + then(voucherWalletService).should().findOwnedVouchersByCustomer(argument.capture()); + OwnedVouchersParam actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(expected); + } + + @Test + @DisplayName("deleteWallet_유효한 파라미터라면_월렛을 제거한다.") + void deleteWallet_WithValidParam_WalletIsDeleted() { + // Given + UUID voucherUuid = UUID.randomUUID(); + WalletDeleteParam expected = new WalletDeleteParam(voucherUuid, "customerId"); + + // When + customerService.deleteWallet(expected); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + WalletDeleteParam.class); + then(voucherWalletService).should().deleteWallet(argument.capture()); + WalletDeleteParam actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(expected); + } + + @Test + @DisplayName("registerToWallet_유효한 파라미터라면_Wallet에 등록한다.") + void registerToWallet_WithValidParam_WalletIsRegistered() { + // Given + UUID voucherUuid = UUID.randomUUID(); + WalletRegisterParam expected = new WalletRegisterParam(voucherUuid, "customerId"); + + // When + customerService.registerToWallet(expected); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + WalletRegisterParam.class); + then(voucherWalletService).should().registerToWallet(argument.capture()); + WalletRegisterParam actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(expected); + } +} diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherFactoryTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherFactoryTest.java index f24e9a8f2b..343ceb1ac1 100644 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherFactoryTest.java +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherFactoryTest.java @@ -7,8 +7,9 @@ import com.wonu606.vouchermanager.domain.voucher.FixedAmountVoucher; import com.wonu606.vouchermanager.domain.voucher.PercentageVoucher; import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherDto; -import com.wonu606.vouchermanager.util.UUIDGenerator; +import com.wonu606.vouchermanager.service.voucher.factory.VoucherFactory; +import com.wonu606.vouchermanager.service.voucher.factory.util.UUIDGenerator; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -29,15 +30,15 @@ void setup() { @DisplayName("create_type이 FIXED이면_FixedAmountVoucher를 생성한다.") @Test - public void Create_TypeIsFIXED_CreatesFixedAmountVoucher() { + void Create_TypeIsFIXED_CreatesFixedAmountVoucher() { // Given UUID testUuid = UUID.randomUUID(); given(uuidGenerator.generateUUID()).willReturn(testUuid); double discountValue = 10.0; - VoucherDto voucherDto = new VoucherDto("FIXED", discountValue); + VoucherCreateParam voucherCreateParam = new VoucherCreateParam("FIXED", discountValue); // When - Voucher voucher = factory.create(voucherDto); + Voucher voucher = factory.create(voucherCreateParam); // Then assertThat(voucher).isInstanceOf(FixedAmountVoucher.class); @@ -48,15 +49,15 @@ public void Create_TypeIsFIXED_CreatesFixedAmountVoucher() { @DisplayName("create_type이 PERCENT이면_PercentageVoucher를 생성한다.") @Test - public void Create_TypeIsPERCENT_CreatesPercentageVoucher() { + void Create_TypeIsPERCENT_CreatesPercentageVoucher() { // Given UUID testUuid = UUID.randomUUID(); given(uuidGenerator.generateUUID()).willReturn(testUuid); double discountValue = 10.0; - VoucherDto voucherDto = new VoucherDto("PERCENT", discountValue); + VoucherCreateParam voucherCreateParam = new VoucherCreateParam("PERCENT", discountValue); // When - Voucher voucher = factory.create(voucherDto); + Voucher voucher = factory.create(voucherCreateParam); // Then assertThat(voucher).isInstanceOf(PercentageVoucher.class); @@ -67,14 +68,14 @@ public void Create_TypeIsPERCENT_CreatesPercentageVoucher() { @DisplayName("create_type이 존재하지 않는다면_예외가 발생한다.") @Test - public void Create_TypeDoesNotExist_ThrowsException() { + void Create_TypeDoesNotExist_ThrowsException() { // Given UUID uuid = UUID.randomUUID(); double discountValue = 10.0; - VoucherDto voucherDto = new VoucherDto("INVALID", discountValue); + VoucherCreateParam voucherCreateParam = new VoucherCreateParam("INVALID", discountValue); // When & Then - assertThatThrownBy(() -> factory.create(voucherDto)) + assertThatThrownBy(() -> factory.create(voucherCreateParam)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("존재하지 않는 바우처 타입입니다."); } diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherServiceTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherServiceTest.java index 03692cae7d..0db8c0fa55 100644 --- a/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherServiceTest.java +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucher/VoucherServiceTest.java @@ -1,68 +1,94 @@ package com.wonu606.vouchermanager.service.voucher; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import com.wonu606.vouchermanager.domain.voucher.Voucher; -import com.wonu606.vouchermanager.domain.voucher.VoucherDto; -import com.wonu606.vouchermanager.repository.voucher.LocalMemoryVoucherVoucherRepository; +import com.wonu606.vouchermanager.domain.voucher.FixedAmountVoucher; import com.wonu606.vouchermanager.repository.voucher.VoucherRepository; -import java.util.Arrays; +import com.wonu606.vouchermanager.repository.voucher.query.VoucherInsertQuery; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherInsertResultSet; +import com.wonu606.vouchermanager.repository.voucher.resultset.VoucherResultSet; +import com.wonu606.vouchermanager.service.voucher.converter.VoucherServiceConverterManager; +import com.wonu606.vouchermanager.service.voucher.factory.VoucherFactory; +import com.wonu606.vouchermanager.service.voucher.factory.util.UUIDGenerator; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; import java.util.List; +import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; @DisplayName("VoucherService 테스트") public class VoucherServiceTest { - private VoucherFactory factory; - - private VoucherRepository voucherRepository; - + private VoucherRepository repository; private VoucherService voucherService; @BeforeEach - public void setup() { - this.factory = mock(VoucherFactory.class); - this.voucherRepository = mock(LocalMemoryVoucherVoucherRepository.class); - this.voucherService = new VoucherService(factory, voucherRepository); + void setUp() { + repository = mock(VoucherRepository.class); + VoucherFactory factory = new VoucherFactory(new FixedUUIDGenerator()); + VoucherServiceConverterManager converterManager = new VoucherServiceConverterManager(); + voucherService = new VoucherService(repository, factory, converterManager); } @Test - @DisplayName("VoucherDto가 주어지고_createVoucher하면_바우처를 생성한다.") - public void GivenVoucherDto_WhenCreateVoucher_ThenReturnExpectedVoucher() { + @DisplayName("createVoucher_유효한 파리미터_바우처를 생성한다.") + void createVoucher_WithValidParameters_VoucherIsCreated() { // Given - VoucherDto dto = mock(VoucherDto.class); - Voucher expectedVoucher = mock(Voucher.class); + double expectedDiscountValue = 50.0; + VoucherCreateParam param = new VoucherCreateParam("fixed", expectedDiscountValue); + VoucherInsertResultSet successResultSet = new VoucherInsertResultSet(1); + given(repository.insert(any())).willReturn(successResultSet); - given(factory.create(dto)).willReturn(expectedVoucher); - given(voucherRepository.save(expectedVoucher)).willReturn(expectedVoucher); + VoucherInsertQuery expectedQuery = new VoucherInsertQuery( + FixedAmountVoucher.class.getSimpleName(), + FixedUUIDGenerator.FIXED_UUID.toString(), expectedDiscountValue); // When - Voucher actualVoucher = voucherService.createVoucher(dto); + voucherService.createVoucher(param); // Then - then(factory).should(times(1)).create(dto); - then(voucherRepository).should(times(1)).save(expectedVoucher); - assertEquals(expectedVoucher, actualVoucher); + ArgumentCaptor argument = ArgumentCaptor.forClass( + VoucherInsertQuery.class); + then(repository).should().insert(argument.capture()); + VoucherInsertQuery actualQuery = argument.getValue(); + assertThat(actualQuery).usingRecursiveComparison().isEqualTo(expectedQuery); } @Test - @DisplayName("Voucher들을 저장한 뒤_getVoucherList하면_바우처들을 반환한다.") - public void GivenSavedVouchers_WhenGetVoucherList_ThenReturnsExpectedVouchers() { + @DisplayName("getVoucherList_바우처가 존재한다면_바우처 리스트를 반환한다.") + void getVoucherList_WhenVouchersExist_ReturnsListOfVouchers() { // Given - List expectedVouchers = Arrays.asList(mock(Voucher.class), mock(Voucher.class)); - given(voucherRepository.findAll()).willReturn(expectedVouchers); + VoucherResultSet voucherResultSet = new VoucherResultSet( + FixedAmountVoucher.class.getSimpleName(), + FixedUUIDGenerator.FIXED_UUID.toString(), 50.0); + given(repository.findAll()).willReturn(List.of(voucherResultSet)); + + VoucherResult expectedResult = new VoucherResult(voucherResultSet.getUuid(), + voucherResultSet.getVoucherClassType(), voucherResultSet.getDiscountValue()); // When - List actualVouchers = voucherService.getVoucherList(); + List actualVoucherResults = voucherService.getVoucherList(); // Then - then(voucherRepository).should(times(1)).findAll(); - assertEquals(expectedVouchers, actualVouchers); + assertThat(actualVoucherResults).hasSize(1); + assertThat(actualVoucherResults).usingRecursiveFieldByFieldElementComparator() + .contains(expectedResult); + } + + private static class FixedUUIDGenerator extends UUIDGenerator { + + public static UUID FIXED_UUID = UUID.fromString("42424242-4242-4242-4242-424242424242"); + + @Override + public UUID generateUUID() { + return FIXED_UUID; + } } } diff --git a/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucherwallet/VoucherWalletServiceTest.java b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucherwallet/VoucherWalletServiceTest.java new file mode 100644 index 0000000000..f1d395ea9b --- /dev/null +++ b/voucher-manager/src/test/java/com/wonu606/vouchermanager/service/voucherwallet/VoucherWalletServiceTest.java @@ -0,0 +1,186 @@ +package com.wonu606.vouchermanager.service.voucherwallet; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; + +import com.wonu606.vouchermanager.repository.voucherwallet.VoucherWalletRepository; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletDeleteQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletInsertQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.query.WalletRegisterQuery; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedCustomerResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.OwnedVoucherResultSet; +import com.wonu606.vouchermanager.repository.voucherwallet.resultset.WalletInsertResultSet; +import com.wonu606.vouchermanager.service.customer.param.WalletRegisterParam; +import com.wonu606.vouchermanager.service.voucher.VoucherService; +import com.wonu606.vouchermanager.service.voucher.param.VoucherCreateParam; +import com.wonu606.vouchermanager.service.voucher.result.VoucherResult; +import com.wonu606.vouchermanager.service.voucherwallet.converter.VoucherWalletServiceConverterManager; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedCustomersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.OwnedVouchersParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletAssignParam; +import com.wonu606.vouchermanager.service.voucherwallet.param.WalletDeleteParam; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedCustomerResult; +import com.wonu606.vouchermanager.service.voucherwallet.result.OwnedVoucherResult; +import java.util.List; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +@DisplayName("VoucherWalletService 테스트") +class VoucherWalletServiceTest { + + @Mock + private VoucherService voucherService; + @Mock + private VoucherWalletRepository repository; + + private VoucherWalletService voucherWalletService; + + @BeforeEach + void setUp() { + VoucherWalletServiceConverterManager converterManager = new VoucherWalletServiceConverterManager(); + voucherWalletService = new VoucherWalletService(voucherService, repository, + converterManager); + } + + @Test + @DisplayName("findOwnedVouchersByCustomer_유효한 CustomerId라면_Voucher List를 반환한다.") + void findOwnedVouchersByCustomer_WithValidCustomerId_ReturnsListOfVouchers() { + // Given + String voucherUUID = "42424242-4242-4242-4242-424242424242"; + ; + given(repository.findOwnedVouchersByCustomer(any())).willReturn( + List.of(new OwnedVoucherResultSet(voucherUUID))); + + OwnedVoucherResult expectedResult = new OwnedVoucherResult(voucherUUID); + + OwnedVouchersParam param = new OwnedVouchersParam("test@test.org"); + + // When + List actualResult = voucherWalletService.findOwnedVouchersByCustomer( + param); + + // Then + assertThat(actualResult).hasSize(1); + assertThat(actualResult).usingRecursiveFieldByFieldElementComparator() + .contains(expectedResult); + } + + @Test + @DisplayName("deleteWallet_유효한 파라미터라면_Wallet을 제거한다.") + void deleteWallet_WithValidParam_WalletIsDeleted() { + // Given + WalletDeleteParam param = new WalletDeleteParam(UUID.randomUUID(), "test@test.org"); + WalletDeleteQuery expectedQuery = new WalletDeleteQuery(param.getCustomerEmail(), + param.getVoucherUuid().toString()); + + // When + voucherWalletService.deleteWallet(param); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + WalletDeleteQuery.class); + then(repository).should().delete(argument.capture()); + WalletDeleteQuery actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(expectedQuery); + } + + @Test + @DisplayName("assignWallet_유효한 파라미터라면_Wallet을 할당한다") + void assignWallet_WithValidParam_WalletIsAssigned() { + // Given + WalletAssignParam param = new WalletAssignParam(UUID.randomUUID()); + WalletInsertQuery expectedQuery = new WalletInsertQuery(param.getVoucherId().toString()); + given(repository.insert(any())).willReturn(new WalletInsertResultSet(1)); + + // When + voucherWalletService.assignWallet(param); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + WalletInsertQuery.class); + then(repository).should().insert(argument.capture()); + WalletInsertQuery actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(expectedQuery); + } + + @Test + @DisplayName("findOwnedCustomersByVoucher_유효한 파라미터라면_Customer List를 반환한다.") + void findOwnedCustomersByVoucher_WithValidParam_ReturnsListOfCustomers() { + // Given + OwnedCustomersParam param = new OwnedCustomersParam(UUID.randomUUID()); + + OwnedCustomerResultSet resultSet = new OwnedCustomerResultSet("test@test.org"); + given(repository.findOwnedCustomersByVoucher(any())).willReturn(List.of(resultSet)); + OwnedCustomerResult expectedResult = new OwnedCustomerResult(resultSet.getCustomerId()); + + // When + List actualResult = voucherWalletService.findOwnedCustomersByVoucher( + param); + + // Then + assertThat(actualResult).hasSize(1); + assertThat(actualResult).usingRecursiveFieldByFieldElementComparator() + .contains(expectedResult); + } + + + @Test + @DisplayName("createVoucher_유효한 파라미터라면_Voucher를 생성한다.") + void createVoucher_WithValidParam_CreatedVoucher() { + // Given + VoucherCreateParam param = new VoucherCreateParam("voucherType", 50.0); + + // When + voucherWalletService.createVoucher(param); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + VoucherCreateParam.class); + then(voucherService).should().createVoucher(argument.capture()); + VoucherCreateParam actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(param); + } + + @Test + @DisplayName("getVoucherList_데이터가 있을 경우_Voucher List를 반환한다.") + void getVoucherList_WhenDataExists_ReturnsListOfVouchers() { + // Given + List expectedResult = List.of( + new VoucherResult("voucherId", "voucherType", 50.0)); + given(voucherService.getVoucherList()).willReturn(expectedResult); + + // When + List actualResult = voucherWalletService.getVoucherList(); + + // Then + assertThat(actualResult).usingRecursiveComparison().isEqualTo(expectedResult); + } + + @Test + @DisplayName("registerToWallet_유효한 파라미터라면_Wallet을 할당한다.") + void registerToWallet_WithValidParam_WalletIsRegistered() { + // Given + WalletRegisterParam param = new WalletRegisterParam(UUID.randomUUID(), "customerId"); + WalletRegisterQuery expectedQuery = new WalletRegisterQuery(param.getCustomerId(), + param.getVoucherId().toString()); + + // When + voucherWalletService.registerToWallet(param); + + // Then + ArgumentCaptor argument = ArgumentCaptor.forClass( + WalletRegisterQuery.class); + then(repository).should().register(argument.capture()); + WalletRegisterQuery actual = argument.getValue(); + assertThat(actual).usingRecursiveComparison().isEqualTo(expectedQuery); + } +} diff --git a/voucher-manager/src/test/resources/schema.sql b/voucher-manager/src/test/resources/schema.sql new file mode 100644 index 0000000000..2850db5cce --- /dev/null +++ b/voucher-manager/src/test/resources/schema.sql @@ -0,0 +1,22 @@ +CREATE TABLE voucher +( + voucher_id VARCHAR(36) PRIMARY KEY, + voucher_type VARCHAR(50) NOT NULL, + discount_value DECIMAL(10, 2) NOT NULL +); + +CREATE TABLE customer +( + email VARCHAR(100) PRIMARY KEY, + nickname VARCHAR(100), + last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE voucher_wallet +( + voucher_id VARCHAR(36) NOT NULL, + customer_id VARCHAR(100), + + FOREIGN KEY (voucher_id) REFERENCES voucher (voucher_id) ON DELETE CASCADE +);