From 6d4d4c53920a9cad364f37237c01b6480521fb1c Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 15:27:14 +0900 Subject: [PATCH 01/62] =?UTF-8?q?Chore:=20=EB=8F=99=EC=82=AC=ED=98=95=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9D=B4=EB=A6=84=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/file/VoucherCsvFileManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index fd57545ca7..12fbfa5093 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -29,14 +29,14 @@ public VoucherCsvFileManager(CsvFileTemplate csvFileTemplate) { } public List read(){ - return csvFileTemplate.read(FILE_PATH, this::lineToVoucher); + return csvFileTemplate.read(FILE_PATH, this::covertToVoucher); } public void write(List voucherPolicies){ - csvFileTemplate.write(FILE_PATH, voucherPolicies, this::voucherToString, CSV_FIRST_LINE); + csvFileTemplate.write(FILE_PATH, voucherPolicies, this::convertToString, CSV_FIRST_LINE); } - private VoucherPolicy lineToVoucher(String line){ + private VoucherPolicy covertToVoucher(String line){ log.debug("line = {}", line); List splitLine = Arrays.stream(line.split(CSV_PATTERN)) @@ -62,7 +62,7 @@ private VoucherPolicy lineToVoucher(String line){ throw new IllegalArgumentException("Invalid voucher type."); } - private String voucherToString(VoucherPolicy voucherPolicy){ + private String convertToString(VoucherPolicy voucherPolicy){ StringBuilder sb = new StringBuilder(); sb.append(voucherPolicy.getVoucherId()); From 50704c4aa44da60bd3b1e63cec1b2b8a23ce0da4 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 15:44:56 +0900 Subject: [PATCH 02/62] =?UTF-8?q?Refactor:=20stream=20=ED=98=95=EC=8B=9D?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20for=EB=AC=B8=EC=9D=84=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/file/VoucherCsvFileManager.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index 12fbfa5093..7c2805c31c 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -44,22 +44,16 @@ private VoucherPolicy covertToVoucher(String line){ .toList(); VoucherType[] voucherTypes = VoucherType.values(); - for (VoucherType type : voucherTypes) { - String voucherType = type.getDisplayName(); - String curStringType = splitLine.get(TYPE_IDX); - - if (curStringType.equals(voucherType)) { - log.info("This voucher type is {}", voucherType); - - return type.create( - UUID.fromString(splitLine.get(UUID_IDX)), - Long.parseLong(splitLine.get(DISCOUNT_VALUE_IDX)) - ); - } - } - - log.error("Invalid voucher type."); - throw new IllegalArgumentException("Invalid voucher type."); + VoucherType thisVoucherType = + Arrays.stream(voucherTypes) + .filter(type -> type.getDisplayName().equals(splitLine.get(TYPE_IDX))) + .findAny() + .orElseThrow(() -> { + log.error("Invalid voucher type."); + return new IllegalArgumentException("Invalid voucher type"); + }); + return thisVoucherType.create(UUID.fromString(splitLine.get(UUID_IDX)), + Long.parseLong(splitLine.get(DISCOUNT_VALUE_IDX))); } private String convertToString(VoucherPolicy voucherPolicy){ From f96f455c8efc33dc1ba36a9055bf5cf2e61800bb Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 15:45:44 +0900 Subject: [PATCH 03/62] =?UTF-8?q?Chore:=20=EC=9E=98=EB=AA=BB=EB=90=9C=20?= =?UTF-8?q?=EA=B0=92=EC=97=90=20=EB=8C=80=ED=95=9C=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=98=20=EB=A0=88=EB=B2=A8=EC=9D=84=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootbasic/domain/voucher/PercentDiscountVoucher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java index 43ea04aa3f..cc7446ba3e 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java @@ -12,7 +12,7 @@ public class PercentDiscountVoucher implements VoucherPolicy { public PercentDiscountVoucher(UUID voucherId, long percent) { if (percent > 100 || percent <= 0) { - log.warn("percent value is out of range."); + log.error("percent value is out of range."); throw new OutOfRangeException("percent value is out of range."); } From 279b0369d3f97fd4d69b0939863602273467064b Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 16:06:28 +0900 Subject: [PATCH 04/62] =?UTF-8?q?Chore:=20=EC=83=81=EC=88=98=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B0=8D=EC=9D=84=20=EB=B3=80=EC=88=98=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B0=8D=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/prgms/springbootbasic/common/Console.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/Console.java b/src/main/java/org/prgms/springbootbasic/common/Console.java index 8ba68b94fd..079d1cf845 100644 --- a/src/main/java/org/prgms/springbootbasic/common/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/Console.java @@ -9,7 +9,7 @@ @Component @Slf4j public class Console { // view - domain 분리 필. Controller를 따로 만들고, 거기서 View, Service 호출. - private static final Scanner CONSOLE_INPUT = new Scanner(System.in); + private static final Scanner consoleInput = new Scanner(System.in); public static String readCommand() { System.out.println("=== Voucher Program ==="); @@ -18,7 +18,7 @@ public static String readCommand() { System.out.println("Type 'list' to list all vouchers."); System.out.println("Type 'black' to list customers blacked."); - return CONSOLE_INPUT.next(); + return consoleInput.next(); } public static int selectCreateType() { @@ -27,7 +27,7 @@ public static int selectCreateType() { System.out.println("1. FixedAmountVoucher"); System.out.println("2. PercentDiscountVoucher"); - return CONSOLE_INPUT.nextInt(); + return consoleInput.nextInt(); } public static int putDiscountDegree(VoucherType type) { @@ -35,12 +35,12 @@ public static int putDiscountDegree(VoucherType type) { case FIXED_AMOUNT -> System.out.println("Enter the fixed discount amount."); case PERCENT_DISCOUNT -> System.out.println("Enter the discount percentage."); } - return CONSOLE_INPUT.nextInt(); + return consoleInput.nextInt(); } public static String ignoreLine() { - return CONSOLE_INPUT.nextLine(); + return consoleInput.nextLine(); } public static void printList(Collection collection) { From 38c3600827b4b05f0018734c9d3ee1c25415d62d Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 16:14:55 +0900 Subject: [PATCH 05/62] =?UTF-8?q?Refactor:=20VoucherType=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EC=88=9C=EC=84=9C=EB=A5=BC=20CommonConstant?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EA=B0=80=EC=A7=80=EA=B3=A0=20=EC=9E=88?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/common/CommonConstant.java | 2 ++ .../java/org/prgms/springbootbasic/common/Console.java | 8 ++++++-- .../org/prgms/springbootbasic/domain/VoucherType.java | 7 +++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/CommonConstant.java b/src/main/java/org/prgms/springbootbasic/common/CommonConstant.java index 4b2592fc94..b1bf3e45fa 100644 --- a/src/main/java/org/prgms/springbootbasic/common/CommonConstant.java +++ b/src/main/java/org/prgms/springbootbasic/common/CommonConstant.java @@ -2,4 +2,6 @@ public class CommonConstant { public static final String CSV_PATTERN = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; + public static final int INPUT_FIXED_AMOUNT_VOUCHER = 1; + public static final int INPUT_PERCENT_DISCOUNT_VOUCHER = 2; } diff --git a/src/main/java/org/prgms/springbootbasic/common/Console.java b/src/main/java/org/prgms/springbootbasic/common/Console.java index 079d1cf845..80e65f34a2 100644 --- a/src/main/java/org/prgms/springbootbasic/common/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/Console.java @@ -4,8 +4,12 @@ import org.prgms.springbootbasic.domain.VoucherType; import org.springframework.stereotype.Component; +import java.text.MessageFormat; import java.util.*; +import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; +import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; + @Component @Slf4j public class Console { // view - domain 분리 필. Controller를 따로 만들고, 거기서 View, Service 호출. @@ -24,8 +28,8 @@ public static String readCommand() { public static int selectCreateType() { System.out.println(); System.out.println("Which voucher would you like to create? Just type number."); - System.out.println("1. FixedAmountVoucher"); - System.out.println("2. PercentDiscountVoucher"); + System.out.println(MessageFormat.format("{0}. FixedAmountVoucher", INPUT_FIXED_AMOUNT_VOUCHER)); + System.out.println(MessageFormat.format("{0}. PercentDiscountVoucher", INPUT_PERCENT_DISCOUNT_VOUCHER)); return consoleInput.nextInt(); } diff --git a/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java b/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java index 5e951eb9e1..f175ffb2cd 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java +++ b/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java @@ -9,10 +9,13 @@ import java.util.UUID; import java.util.function.BiFunction; +import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; +import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; + @Slf4j public enum VoucherType { - FIXED_AMOUNT(1, FixedAmountVoucher::new, "FixedAmountVoucher"), - PERCENT_DISCOUNT(2, PercentDiscountVoucher::new, "PercentDiscountVoucher"); + FIXED_AMOUNT(INPUT_FIXED_AMOUNT_VOUCHER, FixedAmountVoucher::new, "FixedAmountVoucher"), + PERCENT_DISCOUNT(INPUT_PERCENT_DISCOUNT_VOUCHER, PercentDiscountVoucher::new, "PercentDiscountVoucher"); private final int seq; private final BiFunction biFunction; From 5e648dc8a2a0d193fadee7d50726004832b23a4f Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 19:38:57 +0900 Subject: [PATCH 06/62] =?UTF-8?q?Chore:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=A3=BC=EC=84=9D=EB=93=A4=EC=9D=84=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/prgms/springbootbasic/common/Console.java | 2 +- .../springbootbasic/common/file/VoucherCsvFileManager.java | 2 +- .../prgms/springbootbasic/domain/voucher/VoucherPolicy.java | 4 ++-- .../springbootbasic/repository/VoucherFileRepository.java | 6 ------ .../org/prgms/springbootbasic/service/VoucherService.java | 2 +- 5 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/Console.java b/src/main/java/org/prgms/springbootbasic/common/Console.java index 80e65f34a2..e3cbb3b4c9 100644 --- a/src/main/java/org/prgms/springbootbasic/common/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/Console.java @@ -12,7 +12,7 @@ @Component @Slf4j -public class Console { // view - domain 분리 필. Controller를 따로 만들고, 거기서 View, Service 호출. +public class Console { private static final Scanner consoleInput = new Scanner(System.in); public static String readCommand() { diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index 7c2805c31c..1dcfd7d7a0 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -15,7 +15,7 @@ @Component @Slf4j @Profile({"dev", "prod"}) -public class VoucherCsvFileManager { // CsvFileManager 하나로 합쳐서. domain은 최대한 순수하게 유지. 외부 의존성이 들어간다? 이게 도메인에 들어가면 변경이 취약. -> 분리 +public class VoucherCsvFileManager { private static final String FILE_PATH = "./src/main/resources/voucher.csv"; private static final String CSV_FIRST_LINE = "UUID,Type,DiscountValue"; private static final int UUID_IDX = 0; diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java index 51dd756124..c789066c6e 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java @@ -3,7 +3,7 @@ import java.util.UUID; public interface VoucherPolicy { - UUID getVoucherId(); // VoucherPolicy를 file로 쓸 때 id와 discount 정도를 알아야 함. + UUID getVoucherId(); long discount(long beforeDiscount); - long getDiscountAmount(); // VoucherPolicy를 file로 쓸 때 id와 discount 정도를 알아야 함. + long getDiscountAmount(); } diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java index 6bcbd999d6..dc9d22a481 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java @@ -54,9 +54,3 @@ private void fileWrite(){ voucherCsvFileManager.write(this.findAll()); } } - -// 데코레이터 패턴: 기능의 확장. 이것이 기능의 확장이라 보기는 어렵다. -// 빈은 싱글톤이다. 만약 미래에 VoucherFileRepository와 VoucherMemoryRepository를 둘 다 쓴다면 -// VoucherFileRepository에서 VoucherMemoryRepository 내 HashMap을 쓸 건데 싱글톤이라 결국 둘을 공존해 쓰게 되고 이로 인한 이슈가 있을 수 있다. -// 둘을 분리하려면 new로 따로 인스턴스 생성해서 넣어줘도 해결을 할 수 있지만 이를 인지하면서 개발하는 것부터 스트레스임. -// 그냥 분리하자. diff --git a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java index 96d5246de9..c0ef6ce4d8 100644 --- a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java +++ b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java @@ -22,7 +22,7 @@ public VoucherType seqToType(int voucherSeq) { } public void create(VoucherType voucherType, int discountDegree) { - VoucherPolicy voucherPolicy = voucherType.create(discountDegree); // 생성 비즈니스 로직이 있다. + VoucherPolicy voucherPolicy = voucherType.create(discountDegree); voucherRepository.create(voucherPolicy); } From aa8eb705f6374b2b3d3c43ab005cebf4a8436292 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 19:43:04 +0900 Subject: [PATCH 07/62] =?UTF-8?q?Refactor:=20=EB=8F=99=EC=82=AC=ED=98=95?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=AA=85=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootbasic/common/file/CustomerCsvFileManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java index 7af4e6de13..24b8084b1f 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java @@ -31,10 +31,10 @@ public CustomerCsvFileManager(CsvFileTemplate csvFileTemplate) { public List readBlack(){ - return csvFileTemplate.read(BLACK_PATH, this::lineToBlack); + return csvFileTemplate.read(BLACK_PATH, this::convertToBlack); } - private Customer lineToBlack(String line){ + private Customer convertToBlack(String line){ log.debug("line = {}", line); List csvFields = From 662d31dadca85dd59d349179149b74d28d3acbb2 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 19:49:10 +0900 Subject: [PATCH 08/62] =?UTF-8?q?Fix:=20VoucherMemoryRepository=20?= =?UTF-8?q?=EB=82=B4=20=ED=95=84=EB=93=9C=EB=A5=BC=20private=20final?= =?UTF-8?q?=EB=A1=9C=20=EC=84=A4=EC=A0=95=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootbasic/repository/VoucherMemoryRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java index 458f3ce52f..76f60a385b 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java @@ -12,7 +12,7 @@ @Profile({"dev", "prod", "local", "test"}) @Slf4j public class VoucherMemoryRepository implements VoucherRepository{ - ConcurrentHashMap mem = new ConcurrentHashMap<>(); + private final ConcurrentHashMap mem = new ConcurrentHashMap<>(); @Override public VoucherPolicy findById(UUID voucherId) { From f3542b52b5cafdc95c8c557bbcadde6e7c49d56a Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 20:19:01 +0900 Subject: [PATCH 09/62] =?UTF-8?q?Refactor:=20VoucherRepository=EC=9D=98=20?= =?UTF-8?q?findById=20=EB=A9=94=EC=84=9C=EB=93=9C=EA=B0=80=20Optional?= =?UTF-8?q?=EC=9D=84=20=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootbasic/repository/VoucherFileRepository.java | 5 ++--- .../springbootbasic/repository/VoucherMemoryRepository.java | 5 ++--- .../prgms/springbootbasic/repository/VoucherRepository.java | 3 ++- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java index dc9d22a481..393bed4c55 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java @@ -27,9 +27,8 @@ public VoucherFileRepository(VoucherCsvFileManager voucherCsvFileManager) { } @Override - public VoucherPolicy findById(UUID voucherId) { - return Optional.ofNullable(vouchers.get(voucherId)) - .orElseThrow(NoSuchElementException::new); + public Optional findById(UUID voucherId) { + return Optional.ofNullable(vouchers.get(voucherId)); } @Override diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java index 76f60a385b..1e2b259b41 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java @@ -15,9 +15,8 @@ public class VoucherMemoryRepository implements VoucherRepository{ private final ConcurrentHashMap mem = new ConcurrentHashMap<>(); @Override - public VoucherPolicy findById(UUID voucherId) { - return Optional.ofNullable(mem.get(voucherId)) - .orElseThrow(NoSuchElementException::new); + public Optional findById(UUID voucherId) { + return Optional.ofNullable(mem.get(voucherId)); } @Override diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java index 2c77f22364..023cc14efa 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java @@ -3,10 +3,11 @@ import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import java.util.List; +import java.util.Optional; import java.util.UUID; public interface VoucherRepository { - VoucherPolicy findById(UUID voucherId); + Optional findById(UUID voucherId); List findAll(); VoucherPolicy create(VoucherPolicy voucherPolicy); } From 0fca27c93a2fd59b17aa73d4e27d4006d753073c Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 21:13:36 +0900 Subject: [PATCH 10/62] =?UTF-8?q?Test:=20VoucherMemoryRepository=EB=A5=BC?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=ED=95=98=EB=8A=94=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VoucherMemoryRepositoryTest.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java new file mode 100644 index 0000000000..455973f85b --- /dev/null +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java @@ -0,0 +1,72 @@ +package org.prgms.springbootbasic.repository; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.prgms.springbootbasic.BasicApplication; +import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import java.util.Optional; +import java.util.UUID; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@SpringJUnitConfig(BasicApplication.class) +@ActiveProfiles("test") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class VoucherMemoryRepositoryTest { + + @Autowired + private VoucherMemoryRepository voucherMemoryRepository; + + @Test + void findVoucherByIdFromMemory() { + UUID voucherId = UUID.randomUUID(); + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(voucherId, 1000); + + voucherMemoryRepository.create(fixedAmountVoucher); + + Optional retrievedVoucher = voucherMemoryRepository.findById(voucherId); + Optional randomRetrievedVoucher = voucherMemoryRepository.findById(UUID.randomUUID()); + + assertThat(randomRetrievedVoucher.isEmpty(), is(true)); + assertThat(retrievedVoucher.isEmpty(), is(false)); + assertThat(retrievedVoucher.get(), samePropertyValuesAs(fixedAmountVoucher)); + } + + @Test + @Order(1) + void findAllVoucherFromMemory() { + PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 6000); + + voucherMemoryRepository.create(percentDiscountVoucher); + voucherMemoryRepository.create(fixedAmountVoucher); + + assertThat(voucherMemoryRepository.findAll(), hasSize(2)); + assertThat(voucherMemoryRepository.findAll(), hasItem(percentDiscountVoucher)); + assertThat(voucherMemoryRepository.findAll(), hasItem(fixedAmountVoucher)); + } + + @Test + void createNewVouchersToMemory() { + int beforeSize = voucherMemoryRepository.findAll().size(); + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 30); + + this.voucherMemoryRepository.create(fixedAmountVoucher); + this.voucherMemoryRepository.create(percentDiscountVoucher); + + assertThat(voucherMemoryRepository.findAll(), hasSize(2 + beforeSize)); + assertThrows(IllegalArgumentException.class, + () -> voucherMemoryRepository.create(new PercentDiscountVoucher(UUID.randomUUID(), 0))); + } +} From f7d94d0e1926ecd9edc69664c969478302b93536 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 21:18:36 +0900 Subject: [PATCH 11/62] =?UTF-8?q?Chore:=20CSV=20=EC=A2=85=EC=86=8D?= =?UTF-8?q?=EC=A0=81=EC=9D=B4=EB=AF=80=EB=A1=9C=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=EC=9D=84=20=EA=B7=B8=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ucherFileRepository.java => VoucherCsvFileRepository.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/main/java/org/prgms/springbootbasic/repository/{VoucherFileRepository.java => VoucherCsvFileRepository.java} (87%) diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java similarity index 87% rename from src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java index 393bed4c55..8b72f3187a 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java @@ -16,11 +16,11 @@ @Profile({"dev", "prod"}) @Primary @Slf4j -public class VoucherFileRepository implements VoucherRepository { // csv를 다루니 이름은 Csv를 붙여 더 명확히 해야 하지 않나 싶다. +public class VoucherCsvFileRepository implements VoucherRepository { private final ConcurrentHashMap vouchers = new ConcurrentHashMap<>(); private final VoucherCsvFileManager voucherCsvFileManager; - public VoucherFileRepository(VoucherCsvFileManager voucherCsvFileManager) { + public VoucherCsvFileRepository(VoucherCsvFileManager voucherCsvFileManager) { log.debug("FileVoucherRepository started."); this.voucherCsvFileManager = voucherCsvFileManager; From 9c32e33b96d96d8b0c1da8f6d155e8527a63ae66 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 23:16:00 +0900 Subject: [PATCH 12/62] =?UTF-8?q?Feat:=20VoucherRepository=EC=97=90=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C,=20=EC=A0=84=EC=B2=B4=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/file/VoucherCsvFileManager.java | 4 ++-- .../repository/VoucherCsvFileRepository.java | 10 ++++++++++ .../repository/VoucherMemoryRepository.java | 12 +++++++++++- .../repository/VoucherRepository.java | 2 ++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index 1dcfd7d7a0..c19f4bc09a 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -29,14 +29,14 @@ public VoucherCsvFileManager(CsvFileTemplate csvFileTemplate) { } public List read(){ - return csvFileTemplate.read(FILE_PATH, this::covertToVoucher); + return csvFileTemplate.read(FILE_PATH, this::convertToVoucher); } public void write(List voucherPolicies){ csvFileTemplate.write(FILE_PATH, voucherPolicies, this::convertToString, CSV_FIRST_LINE); } - private VoucherPolicy covertToVoucher(String line){ + private VoucherPolicy convertToVoucher(String line){ log.debug("line = {}", line); List splitLine = Arrays.stream(line.split(CSV_PATTERN)) diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java index 8b72f3187a..7ba26b438b 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java @@ -42,6 +42,16 @@ public VoucherPolicy create(VoucherPolicy voucherPolicy) { return voucherPolicy; } + @Override + public Optional deleteById(UUID voucherId) { + return Optional.ofNullable(vouchers.remove(voucherId)); + } + + @Override + public void deleteAll() { + vouchers.clear(); + } + @PostConstruct private void fileRead(){ List voucherPolicies = voucherCsvFileManager.read(); diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java index 1e2b259b41..58523523dd 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java @@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap; @Repository -@Profile({"dev", "prod", "local", "test"}) +@Profile({"local", "test"}) @Slf4j public class VoucherMemoryRepository implements VoucherRepository{ private final ConcurrentHashMap mem = new ConcurrentHashMap<>(); @@ -29,4 +29,14 @@ public VoucherPolicy create(VoucherPolicy voucherPolicy) { mem.putIfAbsent(voucherPolicy.getVoucherId(), voucherPolicy); return voucherPolicy; } + + @Override + public Optional deleteById(UUID voucherId) { + return Optional.ofNullable(mem.remove(voucherId)); + } + + @Override + public void deleteAll() { + mem.clear(); + } } diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java b/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java index 023cc14efa..c723d58ba5 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java @@ -10,4 +10,6 @@ public interface VoucherRepository { Optional findById(UUID voucherId); List findAll(); VoucherPolicy create(VoucherPolicy voucherPolicy); + Optional deleteById(UUID voucherId); + void deleteAll(); } From f1e65198fd41fc27939cf634d6dcee0612c1692e Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sat, 28 Oct 2023 23:16:20 +0900 Subject: [PATCH 13/62] =?UTF-8?q?Test:=20=EC=82=AD=EC=A0=9C,=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VoucherMemoryRepositoryTest.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java index 455973f85b..4fcd74c0dc 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java @@ -1,9 +1,6 @@ package org.prgms.springbootbasic.repository; -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.*; import org.prgms.springbootbasic.BasicApplication; import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; @@ -21,12 +18,16 @@ @SpringJUnitConfig(BasicApplication.class) @ActiveProfiles("test") -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class VoucherMemoryRepositoryTest { @Autowired private VoucherMemoryRepository voucherMemoryRepository; + @AfterEach + void cleanUp(){ + voucherMemoryRepository.deleteAll(); + } + @Test void findVoucherByIdFromMemory() { UUID voucherId = UUID.randomUUID(); @@ -43,7 +44,6 @@ void findVoucherByIdFromMemory() { } @Test - @Order(1) void findAllVoucherFromMemory() { PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 6000); @@ -58,15 +58,25 @@ void findAllVoucherFromMemory() { @Test void createNewVouchersToMemory() { - int beforeSize = voucherMemoryRepository.findAll().size(); FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 30); this.voucherMemoryRepository.create(fixedAmountVoucher); this.voucherMemoryRepository.create(percentDiscountVoucher); - assertThat(voucherMemoryRepository.findAll(), hasSize(2 + beforeSize)); + assertThat(voucherMemoryRepository.findAll(), hasSize(2)); assertThrows(IllegalArgumentException.class, () -> voucherMemoryRepository.create(new PercentDiscountVoucher(UUID.randomUUID(), 0))); } + + @Test + void deleteAllVouchersFromMemory() { + voucherMemoryRepository.create(new PercentDiscountVoucher(UUID.randomUUID(), 10)); + voucherMemoryRepository.create(new PercentDiscountVoucher(UUID.randomUUID(), 20)); + voucherMemoryRepository.create(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + + voucherMemoryRepository.deleteAll(); + + assertThat(voucherMemoryRepository.findAll(), hasSize(0)); + } } From 3c6f00be6aadcb7883b68f2f8011d463a00786f1 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 00:39:59 +0900 Subject: [PATCH 14/62] =?UTF-8?q?Feat:=20@Value=EB=A5=BC=20=EC=9D=B4?= =?UTF-8?q?=EC=9A=A9=ED=95=B4=20=ED=8C=8C=EC=9D=BC=20=EA=B2=BD=EB=A1=9C?= =?UTF-8?q?=EB=A5=BC=20=ED=94=84=EB=A1=9C=ED=8C=8C=EC=9D=BC=EC=97=90=20?= =?UTF-8?q?=EB=A7=9E=EA=B2=8C=20=EC=84=A4=EC=A0=95=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootbasic/common/file/VoucherCsvFileManager.java | 4 +++- src/main/resources/application-dev.yml | 3 +++ src/main/resources/application-prod.yml | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/application-dev.yml create mode 100644 src/main/resources/application-prod.yml diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index c19f4bc09a..bd2c8ce6ed 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -3,6 +3,7 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @@ -16,7 +17,8 @@ @Slf4j @Profile({"dev", "prod"}) public class VoucherCsvFileManager { - private static final String FILE_PATH = "./src/main/resources/voucher.csv"; + @Value("${basic.file.path}") + private String FILE_PATH; private static final String CSV_FIRST_LINE = "UUID,Type,DiscountValue"; private static final int UUID_IDX = 0; private static final int TYPE_IDX = 1; diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000000..00d124a133 --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,3 @@ +basic: + file: + path: ./src/test/resources/voucher.csv diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml new file mode 100644 index 0000000000..afd1b972b9 --- /dev/null +++ b/src/main/resources/application-prod.yml @@ -0,0 +1,3 @@ +basic: + file: + path: ./src/main/resources/voucher.csv From 83c8ad2e928382da0e54d9c92efb3186fa1a91f2 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 00:40:39 +0900 Subject: [PATCH 15/62] =?UTF-8?q?Test:=20CSV=20=ED=8C=8C=EC=9D=BC=EB=A1=9C?= =?UTF-8?q?=20=EA=B4=80=EB=A6=AC=ED=95=98=EB=8A=94=20VoucherRepository=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=9E=91=EC=84=B1?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VoucherCsvFileRepositoryTest.java | 93 +++++++++++++++++++ .../VoucherMemoryRepositoryTest.java | 4 +- 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java new file mode 100644 index 0000000000..150650a5b8 --- /dev/null +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java @@ -0,0 +1,93 @@ +package org.prgms.springbootbasic.repository; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import java.util.Optional; +import java.util.UUID; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +@ActiveProfiles("dev") +@SpringBootTest +class VoucherCsvFileRepositoryTest { + + @Autowired + private VoucherCsvFileRepository voucherCsvFileRepository; + + @BeforeEach + void cleanUp(){ + voucherCsvFileRepository.deleteAll(); + } + + @Test + void findVoucherByIdFromFile() { + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + + voucherCsvFileRepository.create(fixedAmountVoucher); + + Optional retrievedVoucher = voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()); + + assertThat(retrievedVoucher.isPresent(), is(true)); + assertThat(retrievedVoucher.get(), samePropertyValuesAs(fixedAmountVoucher)); + } + + @Test + void findAllVoucherFromFile() { + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 10); + + voucherCsvFileRepository.create(fixedAmountVoucher); + voucherCsvFileRepository.create(percentDiscountVoucher); + + assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); + assertThat(voucherCsvFileRepository.findAll(), hasItem(fixedAmountVoucher)); + assertThat(voucherCsvFileRepository.findAll(), hasItem(percentDiscountVoucher)); + } + + @Test + void createVoucherToFile() { + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 2000); + PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); + + voucherCsvFileRepository.create(fixedAmountVoucher); + voucherCsvFileRepository.create(percentDiscountVoucher); + + assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); + assertThat(voucherCsvFileRepository.findAll(), hasItem(fixedAmountVoucher)); + assertThat(voucherCsvFileRepository.findAll(), hasItem(percentDiscountVoucher)); + assertThat(voucherCsvFileRepository.findAll(), + not(hasItem(new FixedAmountVoucher(UUID.randomUUID(), 2000)))); + } + + @Test + void deleteVoucherById() { + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + + voucherCsvFileRepository.create(fixedAmountVoucher); + + assertThat(voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()).isPresent(), is(true)); + + voucherCsvFileRepository.deleteById(UUID.randomUUID()); + assertThat(voucherCsvFileRepository.findAll(), hasSize(1)); + + voucherCsvFileRepository.deleteById(fixedAmountVoucher.getVoucherId()); + assertThat(voucherCsvFileRepository.findAll(), hasSize(0)); + } + + @Test + void deleteAllVoucher() { + voucherCsvFileRepository.create(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + + voucherCsvFileRepository.deleteAll(); + + assertThat(voucherCsvFileRepository.findAll(), hasSize(0)); + } +} diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java index 4fcd74c0dc..97c944b3ae 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java @@ -17,13 +17,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; @SpringJUnitConfig(BasicApplication.class) -@ActiveProfiles("test") +@ActiveProfiles("local") class VoucherMemoryRepositoryTest { @Autowired private VoucherMemoryRepository voucherMemoryRepository; - @AfterEach + @BeforeEach void cleanUp(){ voucherMemoryRepository.deleteAll(); } From a8216650ad016073cd0f7e81e6aa821a7221f7c1 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 00:51:18 +0900 Subject: [PATCH 16/62] =?UTF-8?q?Feat:=20customers=20=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=B8=94=20=EC=83=9D=EC=84=B1=EC=9D=84=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=EC=8A=A4=ED=82=A4=EB=A7=88=20=ED=8C=8C=EC=9D=BC=EC=9D=84=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/schema.sql | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/resources/schema.sql diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 0000000000..5a93592c33 --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS customers( + customer_id BINARY(16) PRIMARY KEY, + name VARCHAR(26) NOT NULL, + email VARCHAR(56) NOT NULL , + last_login_at DATETIME(6) DEFAULT NULL, + created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + is_blacked BOOLEAN NOT NULL DEFAULT FALSE, + CONSTRAINT unq_user_email UNIQUE (email) +); From 04b244436a78efdb98fb106914e4f8e07cfe8b1a Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 00:55:25 +0900 Subject: [PATCH 17/62] =?UTF-8?q?Rename:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=EB=A5=BC=20=EC=A0=81=EC=A0=88=ED=9E=88=20=EC=9D=B4=EB=8F=99?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/{ => customer}/CustomerFileRepository.java | 2 +- .../repository/{ => customer}/CustomerMemoryRepository.java | 2 +- .../repository/{ => customer}/CustomerRepository.java | 2 +- .../repository/{ => voucher}/VoucherCsvFileRepository.java | 2 +- .../repository/{ => voucher}/VoucherMemoryRepository.java | 2 +- .../repository/{ => voucher}/VoucherRepository.java | 2 +- .../java/org/prgms/springbootbasic/service/CustomerService.java | 2 +- .../java/org/prgms/springbootbasic/service/VoucherService.java | 2 +- .../repository/VoucherCsvFileRepositoryTest.java | 1 + .../springbootbasic/repository/VoucherMemoryRepositoryTest.java | 1 + 10 files changed, 10 insertions(+), 8 deletions(-) rename src/main/java/org/prgms/springbootbasic/repository/{ => customer}/CustomerFileRepository.java (92%) rename src/main/java/org/prgms/springbootbasic/repository/{ => customer}/CustomerMemoryRepository.java (88%) rename src/main/java/org/prgms/springbootbasic/repository/{ => customer}/CustomerRepository.java (74%) rename src/main/java/org/prgms/springbootbasic/repository/{ => voucher}/VoucherCsvFileRepository.java (97%) rename src/main/java/org/prgms/springbootbasic/repository/{ => voucher}/VoucherMemoryRepository.java (95%) rename src/main/java/org/prgms/springbootbasic/repository/{ => voucher}/VoucherRepository.java (88%) diff --git a/src/main/java/org/prgms/springbootbasic/repository/CustomerFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java similarity index 92% rename from src/main/java/org/prgms/springbootbasic/repository/CustomerFileRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java index 7db1b08835..668ef14f57 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/CustomerFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.customer; import org.prgms.springbootbasic.common.file.CustomerCsvFileManager; import org.prgms.springbootbasic.domain.customer.Customer; diff --git a/src/main/java/org/prgms/springbootbasic/repository/CustomerMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java similarity index 88% rename from src/main/java/org/prgms/springbootbasic/repository/CustomerMemoryRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java index 7c6caebd51..6429840be9 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/CustomerMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.customer; import org.prgms.springbootbasic.domain.customer.Customer; import org.springframework.context.annotation.Profile; diff --git a/src/main/java/org/prgms/springbootbasic/repository/CustomerRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java similarity index 74% rename from src/main/java/org/prgms/springbootbasic/repository/CustomerRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java index 09a3731d46..863178c0de 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/CustomerRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.customer; import org.prgms.springbootbasic.domain.customer.Customer; diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java similarity index 97% rename from src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java index 7ba26b438b..52fc4b4a62 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.voucher; import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.common.file.VoucherCsvFileManager; diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java similarity index 95% rename from src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java index 58523523dd..47cf82ad4b 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.voucher; import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; diff --git a/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java similarity index 88% rename from src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java index c723d58ba5..27717eceaa 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/VoucherRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; diff --git a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java index 35bd35fc46..c246059acd 100644 --- a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java +++ b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java @@ -2,7 +2,7 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.customer.Customer; -import org.prgms.springbootbasic.repository.CustomerRepository; +import org.prgms.springbootbasic.repository.customer.CustomerRepository; import org.springframework.stereotype.Service; import java.util.List; diff --git a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java index c0ef6ce4d8..d1615441f7 100644 --- a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java +++ b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java @@ -3,7 +3,7 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; -import org.prgms.springbootbasic.repository.VoucherRepository; +import org.prgms.springbootbasic.repository.voucher.VoucherRepository; import org.springframework.stereotype.Service; import java.util.List; diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java index 150650a5b8..885d98b264 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java @@ -5,6 +5,7 @@ import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.prgms.springbootbasic.repository.voucher.VoucherCsvFileRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java index 97c944b3ae..e51ea36480 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java @@ -5,6 +5,7 @@ import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.prgms.springbootbasic.repository.voucher.VoucherMemoryRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; From 124ab3cbe4a1c470173541c40ae35846747da919 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 01:07:02 +0900 Subject: [PATCH 18/62] =?UTF-8?q?Feat:=20CustomerRepository=EC=97=90=20CRU?= =?UTF-8?q?D=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EC=A0=84=EB=B6=80=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/customer/CustomerRepository.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java index 863178c0de..6d7d83ba2e 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java @@ -3,7 +3,14 @@ import org.prgms.springbootbasic.domain.customer.Customer; import java.util.List; +import java.util.Optional; +import java.util.UUID; public interface CustomerRepository { + Customer save(Customer customer); + Optional findById(UUID customerId); + Optional findByEmail(String email); List findBlackAll(); + Optional deleteById(UUID customerId); + void deleteAll(); } From 5ee64a4633514ce90324b0155c128d8250e18f9e Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 15:01:38 +0900 Subject: [PATCH 19/62] =?UTF-8?q?Feat:=20DB=EB=A5=BC=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20CustomerDatabaseRepository=EB=A5=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../domain/customer/Customer.java | 24 +++-- .../customer/CustomerDatabaseRepository.java | 99 +++++++++++++++++++ .../customer/CustomerFileRepository.java | 31 +++++- .../customer/CustomerMemoryRepository.java | 31 +++++- .../customer/CustomerRepository.java | 2 +- 6 files changed, 177 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java diff --git a/build.gradle b/build.gradle index f3158f0b0f..6f0574cf91 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter' + implementation 'org.springframework.boot:spring-boot-starter-jdbc' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java b/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java index cf42b9750e..acfa885791 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java +++ b/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java @@ -4,15 +4,15 @@ import java.util.UUID; public class Customer { - private UUID id; + private final UUID customerId; private String name; - private String email; - private LocalDateTime createdAt; + private final String email; + private final LocalDateTime createdAt; private LocalDateTime lastLoginAt; private boolean isBlacked; - public Customer(UUID id, String name, String email, LocalDateTime createdAt, LocalDateTime lastLoginAt, boolean isBlacked) { - this.id = id; + public Customer(UUID customerId, String name, String email, LocalDateTime createdAt, LocalDateTime lastLoginAt, boolean isBlacked) { + this.customerId = customerId; this.name = name; this.email = email; this.createdAt = createdAt; @@ -20,10 +20,22 @@ public Customer(UUID id, String name, String email, LocalDateTime createdAt, Loc this.isBlacked = isBlacked; } + public UUID getCustomerId() { + return customerId; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + @Override public String toString() { return "Customer{" + - "id=" + id + + "id=" + customerId + ", name='" + name + '\'' + ", email='" + email + '\'' + ", createdAt=" + createdAt + diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java new file mode 100644 index 0000000000..d5ff2711ee --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java @@ -0,0 +1,99 @@ +package org.prgms.springbootbasic.repository.customer; + +import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.domain.customer.Customer; +import org.springframework.context.annotation.Profile; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Repository; + +import java.nio.ByteBuffer; +import java.time.LocalDateTime; +import java.util.*; + +@Repository +@Slf4j +@Profile({"dev", "prod"}) +public class CustomerDatabaseRepository implements CustomerRepository{ + private final NamedParameterJdbcTemplate jdbcTemplate; + + public CustomerDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + public Customer save(Customer customer) { + Optional foundCustomer = findById(customer.getCustomerId()); + + if (foundCustomer.isPresent()){ + jdbcTemplate.update("UPDATE customers SET name = :name WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", + toParamMap(customer)); + } else { + jdbcTemplate.update("INSERT INTO customers(customer_id, name, email) VALUES (UNHEX(REPLACE(:customerId, '-', '')), :name, :email)", + toParamMap(customer)); + } + + return customer; + } + + @Override + public Optional findById(UUID customerId) { + return Optional.ofNullable( + jdbcTemplate.queryForObject("SELECT * FROM customers WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", + Collections.singletonMap("customerId", customerId.toString().getBytes()), + Customer.class)); + } + + @Override + public Optional findByEmail(String email) { + return Optional.ofNullable( + jdbcTemplate.queryForObject("SELECT * FROM customers WHERE email = :email", + Collections.singletonMap("email", email), + Customer.class)); + } + + @Override + public List findBlackAll() { + return jdbcTemplate.query("SELECT * FROM customers WHERE is_blacked = true", mapToCustomer); + } + + @Override + public Optional deleteById(UUID customerId) { + Optional deleteCustomer = findById(customerId); + + jdbcTemplate.update("DELETE FROM customers WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", + Collections.singletonMap("customerId", customerId.toString().getBytes())); + return deleteCustomer; + } + + @Override + public int deleteAll() { + return jdbcTemplate.update("DELETE FROM customers", Collections.emptyMap()); + } + + private static Map toParamMap(Customer customer) { + return new HashMap<>(){{ + put("customerId", customer.getCustomerId().toString().getBytes()); + put("name", customer.getName()); + put("email", customer.getEmail()); + }}; + } + + private static RowMapper mapToCustomer = (rs, rowNum) -> { + String customerName = rs.getString("name"); + UUID customerId = toUUID(rs.getBytes("customer_id")); + String email = rs.getString("email"); + LocalDateTime createdAt = rs.getTimestamp("created_at").toLocalDateTime(); + LocalDateTime lastLoginAt = rs.getTimestamp("last_login_at") != null + ? rs.getTimestamp("last_login_at").toLocalDateTime() : null; + boolean isBlacked = rs.getBoolean("is_blacked"); + + return new Customer(customerId, customerName, email, lastLoginAt, createdAt, isBlacked); + }; + + private static UUID toUUID(byte[] customerId) { + ByteBuffer wrappedByte = ByteBuffer.wrap(customerId); + + return new UUID(wrappedByte.getLong(), wrappedByte.getLong()); + } +} diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java index 668ef14f57..064cfea7a4 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java @@ -6,18 +6,45 @@ import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; +import java.util.UUID; @Repository -@Profile({"dev", "prod"}) -public class CustomerFileRepository implements CustomerRepository { +@Profile({"test"}) +public class CustomerFileRepository implements CustomerRepository { // 추후 구현 private final CustomerCsvFileManager customerCsvFileManager; public CustomerFileRepository(CustomerCsvFileManager customerCsvFileManager) { this.customerCsvFileManager = customerCsvFileManager; } + @Override + public Customer save(Customer customer) { + return null; + } + + @Override + public Optional findById(UUID customerId) { + return Optional.empty(); + } + + @Override + public Optional findByEmail(String email) { + return Optional.empty(); + } + @Override public List findBlackAll() { return customerCsvFileManager.readBlack(); } + + @Override + public Optional deleteById(UUID customerId) { + return Optional.empty(); + } + + @Override + public int deleteAll() { + return 0; + } } diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java index 6429840be9..bc46a5f0d5 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java @@ -6,12 +6,39 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; +import java.util.UUID; @Repository -@Profile({"local", "test"}) -public class CustomerMemoryRepository implements CustomerRepository { +@Profile({"local"}) +public class CustomerMemoryRepository implements CustomerRepository { // 추후 구현 + @Override + public Customer save(Customer customer) { + return null; + } + + @Override + public Optional findById(UUID customerId) { + return Optional.empty(); + } + + @Override + public Optional findByEmail(String email) { + return Optional.empty(); + } + @Override public List findBlackAll() { return new ArrayList<>(); } + + @Override + public Optional deleteById(UUID customerId) { + return Optional.empty(); + } + + @Override + public int deleteAll() { + return 0; + } } diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java index 6d7d83ba2e..e67bd4ae68 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java @@ -12,5 +12,5 @@ public interface CustomerRepository { Optional findByEmail(String email); List findBlackAll(); Optional deleteById(UUID customerId); - void deleteAll(); + int deleteAll(); } From 64fb4c176960f4f3551eb216404b948c1df6a620 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 15:58:07 +0900 Subject: [PATCH 20/62] =?UTF-8?q?Feat:=20final=EC=9D=B4=20=EC=95=84?= =?UTF-8?q?=EB=8B=8C=20=ED=95=84=EB=93=9C=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90,=20=EC=A0=95=EB=B3=B4=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EA=B7=B8=EB=A6=AC=EA=B3=A0=20isBlack=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20getter=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/customer/Customer.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java b/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java index acfa885791..b57aa21d89 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java +++ b/src/main/java/org/prgms/springbootbasic/domain/customer/Customer.java @@ -11,6 +11,14 @@ public class Customer { private LocalDateTime lastLoginAt; private boolean isBlacked; + public Customer(UUID customerId, String name, String email, LocalDateTime createdAt) { + this.customerId = customerId; + this.name = name; + this.email = email; + this.createdAt = createdAt; + this.isBlacked = false; + } + public Customer(UUID customerId, String name, String email, LocalDateTime createdAt, LocalDateTime lastLoginAt, boolean isBlacked) { this.customerId = customerId; this.name = name; @@ -32,6 +40,17 @@ public String getEmail() { return email; } + public boolean isBlacked() { + return isBlacked; + } + + public Customer changeInfo(String name, boolean isBlacked){ + this.name = name; + this.isBlacked = isBlacked; + + return this; + } + @Override public String toString() { return "Customer{" + From d3e34a1289cc4ba1dc7473b4bab06627aaa1207b Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 17:21:39 +0900 Subject: [PATCH 21/62] =?UTF-8?q?Feat:=20MySQL=20=EC=97=B0=EA=B2=B0?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EC=84=A4=EC=A0=95=EC=9D=84=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + src/main/resources/application-dev.yml | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/build.gradle b/build.gradle index 6f0574cf91..1b750e01f2 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-jdbc' + runtimeOnly 'com.mysql:mysql-connector-j' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 00d124a133..c1a5857bce 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -1,3 +1,10 @@ +spring: + datasource: + url: jdbc:mysql://localhost:3306/test + username: test + password: 1234 + driver-class-name: com.mysql.cj.jdbc.Driver + basic: file: path: ./src/test/resources/voucher.csv From 83b64692ed6ce3cc4322be7a22332afea3a40306 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 17:21:57 +0900 Subject: [PATCH 22/62] =?UTF-8?q?Feat:=20=EA=B3=A0=EA=B0=9D=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/customer/CustomerFileRepository.java | 5 +++++ .../repository/customer/CustomerMemoryRepository.java | 5 +++++ .../repository/customer/CustomerRepository.java | 1 + 3 files changed, 11 insertions(+) diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java index 064cfea7a4..37f289d504 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java @@ -33,6 +33,11 @@ public Optional findByEmail(String email) { return Optional.empty(); } + @Override + public List findAll() { + return null; + } + @Override public List findBlackAll() { return customerCsvFileManager.readBlack(); diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java index bc46a5f0d5..2fd62af8fc 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java @@ -27,6 +27,11 @@ public Optional findByEmail(String email) { return Optional.empty(); } + @Override + public List findAll() { + return null; + } + @Override public List findBlackAll() { return new ArrayList<>(); diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java index e67bd4ae68..fcb475c5de 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java @@ -10,6 +10,7 @@ public interface CustomerRepository { Customer save(Customer customer); Optional findById(UUID customerId); Optional findByEmail(String email); + List findAll(); List findBlackAll(); Optional deleteById(UUID customerId); int deleteAll(); From 81b816ecc2bbbf95e1f89d46e485dff68cf57e93 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 18:06:17 +0900 Subject: [PATCH 23/62] =?UTF-8?q?Feat:=20DB=EC=97=90=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EA=B0=80=20?= =?UTF-8?q?=EC=97=86=EC=9D=84=20=EB=95=8C=20=EB=82=98=ED=83=80=EB=82=98?= =?UTF-8?q?=EB=8A=94=20=EC=98=88=EC=99=B8=EB=A5=BC=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootbasic/exception/EntityNotFoundException.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/java/org/prgms/springbootbasic/exception/EntityNotFoundException.java diff --git a/src/main/java/org/prgms/springbootbasic/exception/EntityNotFoundException.java b/src/main/java/org/prgms/springbootbasic/exception/EntityNotFoundException.java new file mode 100644 index 0000000000..046b3083f4 --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/exception/EntityNotFoundException.java @@ -0,0 +1,6 @@ +package org.prgms.springbootbasic.exception; + +public class EntityNotFoundException extends RuntimeException{ + public EntityNotFoundException() { + } +} From 3e7207c1c2646138f9363428ab0bb0231a112989 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 18:07:11 +0900 Subject: [PATCH 24/62] =?UTF-8?q?Feat:=20queryForObject=20=EC=8B=9C,=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EA=B0=80=200=EA=B0=9C=EC=9D=B8=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=EB=A5=BC=20=ED=95=98=EA=B3=A0,=20toParamMap=EC=97=90=20isBlack?= =?UTF-8?q?ed=20=EC=97=AC=EB=B6=80=EB=8F=84=20=EC=B6=94=EA=B0=80=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../customer/CustomerDatabaseRepository.java | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java index d5ff2711ee..1a0b18dc9d 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java @@ -3,6 +3,7 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.customer.Customer; import org.springframework.context.annotation.Profile; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Repository; @@ -29,7 +30,7 @@ public Customer save(Customer customer) { jdbcTemplate.update("UPDATE customers SET name = :name WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", toParamMap(customer)); } else { - jdbcTemplate.update("INSERT INTO customers(customer_id, name, email) VALUES (UNHEX(REPLACE(:customerId, '-', '')), :name, :email)", + jdbcTemplate.update("INSERT INTO customers(customer_id, name, email, is_blacked) VALUES (UNHEX(REPLACE(:customerId, '-', '')), :name, :email, :isBlacked)", toParamMap(customer)); } @@ -38,18 +39,33 @@ public Customer save(Customer customer) { @Override public Optional findById(UUID customerId) { - return Optional.ofNullable( - jdbcTemplate.queryForObject("SELECT * FROM customers WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", - Collections.singletonMap("customerId", customerId.toString().getBytes()), - Customer.class)); + try { + return Optional.ofNullable( + jdbcTemplate.queryForObject("SELECT * FROM customers WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", + Collections.singletonMap("customerId", customerId.toString().getBytes()), + mapToCustomer)); + } catch (EmptyResultDataAccessException e) { + log.info("customerId에 해당하는 customer가 DB에 없음."); + return Optional.empty(); + } } @Override public Optional findByEmail(String email) { - return Optional.ofNullable( - jdbcTemplate.queryForObject("SELECT * FROM customers WHERE email = :email", - Collections.singletonMap("email", email), - Customer.class)); + try { + return Optional.ofNullable( + jdbcTemplate.queryForObject("SELECT * FROM customers WHERE email = :email", + Collections.singletonMap("email", email), + mapToCustomer)); + } catch (EmptyResultDataAccessException e) { + log.info("email에 해당하는 customer가 DB에 없음."); + return Optional.empty(); + } + } + + @Override + public List findAll() { + return jdbcTemplate.query("SELECT * FROM customers", mapToCustomer); } @Override @@ -76,6 +92,7 @@ private static Map toParamMap(Customer customer) { put("customerId", customer.getCustomerId().toString().getBytes()); put("name", customer.getName()); put("email", customer.getEmail()); + put("isBlacked", customer.isBlacked()); }}; } From 1b7c39a051a30b9fb26355759519f09fa73656b6 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 18:10:19 +0900 Subject: [PATCH 25/62] =?UTF-8?q?Test:=20=EA=B3=A0=EA=B0=9D=20DB=20Reposit?= =?UTF-8?q?ory=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=9E=91=EC=84=B1?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomerDatabaseRepositoryTest.java | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java diff --git a/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java new file mode 100644 index 0000000000..9f7f0cf3ee --- /dev/null +++ b/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java @@ -0,0 +1,135 @@ +package org.prgms.springbootbasic.repository.customer; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.exception.EntityNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +@SpringBootTest +@ActiveProfiles("dev") +class CustomerDatabaseRepositoryTest { + @Autowired + private CustomerRepository customerRepository; + + private Customer setUpCustomer; + + @BeforeEach + void setUp() { + setUpCustomer = new Customer(UUID.randomUUID(), + "test", + "test@gmail.com", + LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS)); + + customerRepository.save(setUpCustomer); + } + + @AfterEach + void clean() { + customerRepository.deleteAll(); + } + + @Test + void insertNewCustomerInDB() { + Customer customer = new Customer(UUID.randomUUID(), + "test2", + "test2@gmail.com", + LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS)); + + customerRepository.save(customer); + Optional retrievedCustomer = customerRepository.findById(customer.getCustomerId()); + + assertThat(retrievedCustomer.isPresent(), is(true)); + assertThat(retrievedCustomer.get(), is(samePropertyValuesAs(customer))); + } + + @Test + void updateExistingCustomerInDB() { + Customer customer = customerRepository.findById(setUpCustomer.getCustomerId()).orElseThrow(EntityNotFoundException::new); + + Customer changedCustomer = customer.changeInfo("updated", customer.isBlacked()); + + customerRepository.save(changedCustomer); + + Optional retrievedCustomer = customerRepository.findById(setUpCustomer.getCustomerId()); + + assertThat(retrievedCustomer.isPresent(), is(true)); + assertThat(retrievedCustomer.get(), samePropertyValuesAs(changedCustomer)); + } + + @Test + void findCustomerByCustomerIdInDB() { + Optional retrievedCustomer = customerRepository.findById(setUpCustomer.getCustomerId()); + Optional notExistingCustomer = customerRepository.findById(UUID.randomUUID()); + + assertThat(notExistingCustomer.isPresent(), is(false)); + assertThat(retrievedCustomer.isPresent(), is(true)); + assertThat(retrievedCustomer.get(), samePropertyValuesAs(setUpCustomer)); + } + + @Test + void findCustomerByEmailInDB() { + Optional retrievedCustomer = customerRepository.findByEmail(setUpCustomer.getEmail()); + Optional notExistingCustomer = customerRepository.findByEmail("blahblah@naver.com"); + + assertThat(notExistingCustomer.isPresent(), is(false)); + assertThat(retrievedCustomer.isPresent(), is(true)); + assertThat(retrievedCustomer.get(), samePropertyValuesAs(setUpCustomer)); + } + + @Test + void findAllCustomersInDB(){ + customerRepository.save(new Customer(UUID.randomUUID(), "test2", "test2@gmail.com", LocalDateTime.now())); + + List customers = customerRepository.findAll(); + + assertThat(customers, hasSize(2)); + } + + @Test + void findAllBlackedCustomersInDB() { + Customer blackCustomer = new Customer(UUID.randomUUID(), + "black", + "black@gmail.com", + LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS)); + + blackCustomer.changeInfo(blackCustomer.getName(), true); + customerRepository.save(blackCustomer); + + List blackList = customerRepository.findBlackAll(); + + assertThat(blackList, hasSize(1)); + } + + @Test + void deleteCustomerByCustomerIdInDB() { + customerRepository.deleteById(setUpCustomer.getCustomerId()); + + var deletedCustomer = customerRepository.findById(setUpCustomer.getCustomerId()); + + assertThat(deletedCustomer.isPresent(), is(false)); + } + + @Test + void deleteAllCustomersInDB() { + customerRepository.save(new Customer(UUID.randomUUID(), "test2", "test2@gmail.com", LocalDateTime.now())); + + customerRepository.deleteAll(); + + List customers = customerRepository.findAll(); + + assertThat(customers, hasSize(0)); + } +} From 43b5892b572a729163e6c1e6ecdc488a196d581f Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 21:48:36 +0900 Subject: [PATCH 26/62] =?UTF-8?q?Feat:=20=EC=BD=94=EB=93=9C=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=EC=97=90=EC=84=9C=20=EC=82=AC=EC=9A=A9=ED=95=A0?= =?UTF-8?q?=EB=A7=8C=ED=95=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EB=AA=A8=EC=95=84=EB=91=94=20UtilMethod=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EB=A5=BC=20=EC=A0=95=EC=9D=98=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/common/UtilMethod.java | 12 ++++++++++++ .../customer/CustomerDatabaseRepository.java | 9 ++------- 2 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/prgms/springbootbasic/common/UtilMethod.java diff --git a/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java b/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java new file mode 100644 index 0000000000..882359e8b2 --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java @@ -0,0 +1,12 @@ +package org.prgms.springbootbasic.common; + +import java.nio.ByteBuffer; +import java.util.UUID; + +public class UtilMethod { + public static UUID bytesToUUID(byte[] bytes) { + ByteBuffer wrappedByte = ByteBuffer.wrap(bytes); + + return new UUID(wrappedByte.getLong(), wrappedByte.getLong()); + } +} diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java index 1a0b18dc9d..f96b71069b 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java @@ -1,6 +1,7 @@ package org.prgms.springbootbasic.repository.customer; import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.common.UtilMethod; import org.prgms.springbootbasic.domain.customer.Customer; import org.springframework.context.annotation.Profile; import org.springframework.dao.EmptyResultDataAccessException; @@ -98,7 +99,7 @@ private static Map toParamMap(Customer customer) { private static RowMapper mapToCustomer = (rs, rowNum) -> { String customerName = rs.getString("name"); - UUID customerId = toUUID(rs.getBytes("customer_id")); + UUID customerId = UtilMethod.bytesToUUID(rs.getBytes("customer_id")); String email = rs.getString("email"); LocalDateTime createdAt = rs.getTimestamp("created_at").toLocalDateTime(); LocalDateTime lastLoginAt = rs.getTimestamp("last_login_at") != null @@ -107,10 +108,4 @@ private static Map toParamMap(Customer customer) { return new Customer(customerId, customerName, email, lastLoginAt, createdAt, isBlacked); }; - - private static UUID toUUID(byte[] customerId) { - ByteBuffer wrappedByte = ByteBuffer.wrap(customerId); - - return new UUID(wrappedByte.getLong(), wrappedByte.getLong()); - } } From 2d1c408adcf943b1f0af92bb6429bee94c961e58 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 21:49:37 +0900 Subject: [PATCH 27/62] =?UTF-8?q?Chore:=20VoucherRepository=EC=9D=98=20cre?= =?UTF-8?q?ate=EB=A5=BC=20save=EB=9D=BC=EB=8A=94=20=EB=84=A4=EC=9D=B4?= =?UTF-8?q?=EB=B0=8D=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/file/VoucherCsvFileManager.java | 4 ++-- .../voucher/VoucherCsvFileRepository.java | 4 ++-- .../voucher/VoucherMemoryRepository.java | 2 +- .../repository/voucher/VoucherRepository.java | 2 +- .../service/VoucherService.java | 2 +- .../VoucherCsvFileRepositoryTest.java | 14 +++++++------- .../VoucherMemoryRepositoryTest.java | 18 +++++++++--------- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index bd2c8ce6ed..521afd053c 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -22,7 +22,7 @@ public class VoucherCsvFileManager { private static final String CSV_FIRST_LINE = "UUID,Type,DiscountValue"; private static final int UUID_IDX = 0; private static final int TYPE_IDX = 1; - private static final int DISCOUNT_VALUE_IDX = 2; + private static final int DISCOUNT_DEGREE_IDX = 2; private final CsvFileTemplate csvFileTemplate; @@ -55,7 +55,7 @@ private VoucherPolicy convertToVoucher(String line){ return new IllegalArgumentException("Invalid voucher type"); }); return thisVoucherType.create(UUID.fromString(splitLine.get(UUID_IDX)), - Long.parseLong(splitLine.get(DISCOUNT_VALUE_IDX))); + Long.parseLong(splitLine.get(DISCOUNT_DEGREE_IDX))); } private String convertToString(VoucherPolicy voucherPolicy){ diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java index 52fc4b4a62..ce007e901d 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java @@ -37,7 +37,7 @@ public List findAll() { } @Override - public VoucherPolicy create(VoucherPolicy voucherPolicy) { + public VoucherPolicy save(VoucherPolicy voucherPolicy) { vouchers.putIfAbsent(voucherPolicy.getVoucherId(), voucherPolicy); return voucherPolicy; } @@ -55,7 +55,7 @@ public void deleteAll() { @PostConstruct private void fileRead(){ List voucherPolicies = voucherCsvFileManager.read(); - voucherPolicies.forEach(this::create); + voucherPolicies.forEach(this::save); } @PreDestroy diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java index 47cf82ad4b..93fc37a079 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java @@ -25,7 +25,7 @@ public List findAll() { } @Override - public VoucherPolicy create(VoucherPolicy voucherPolicy) { + public VoucherPolicy save(VoucherPolicy voucherPolicy) { mem.putIfAbsent(voucherPolicy.getVoucherId(), voucherPolicy); return voucherPolicy; } diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java index 27717eceaa..be1c441a96 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java @@ -9,7 +9,7 @@ public interface VoucherRepository { Optional findById(UUID voucherId); List findAll(); - VoucherPolicy create(VoucherPolicy voucherPolicy); + VoucherPolicy save(VoucherPolicy voucherPolicy); Optional deleteById(UUID voucherId); void deleteAll(); } diff --git a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java index d1615441f7..ea7df34c45 100644 --- a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java +++ b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java @@ -24,7 +24,7 @@ public VoucherType seqToType(int voucherSeq) { public void create(VoucherType voucherType, int discountDegree) { VoucherPolicy voucherPolicy = voucherType.create(discountDegree); - voucherRepository.create(voucherPolicy); + voucherRepository.save(voucherPolicy); } public List findAll(){ diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java index 885d98b264..d8517c2239 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java @@ -32,7 +32,7 @@ void cleanUp(){ void findVoucherByIdFromFile() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); - voucherCsvFileRepository.create(fixedAmountVoucher); + voucherCsvFileRepository.save(fixedAmountVoucher); Optional retrievedVoucher = voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()); @@ -45,8 +45,8 @@ void findAllVoucherFromFile() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 10); - voucherCsvFileRepository.create(fixedAmountVoucher); - voucherCsvFileRepository.create(percentDiscountVoucher); + voucherCsvFileRepository.save(fixedAmountVoucher); + voucherCsvFileRepository.save(percentDiscountVoucher); assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); assertThat(voucherCsvFileRepository.findAll(), hasItem(fixedAmountVoucher)); @@ -58,8 +58,8 @@ void createVoucherToFile() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 2000); PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); - voucherCsvFileRepository.create(fixedAmountVoucher); - voucherCsvFileRepository.create(percentDiscountVoucher); + voucherCsvFileRepository.save(fixedAmountVoucher); + voucherCsvFileRepository.save(percentDiscountVoucher); assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); assertThat(voucherCsvFileRepository.findAll(), hasItem(fixedAmountVoucher)); @@ -72,7 +72,7 @@ void createVoucherToFile() { void deleteVoucherById() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); - voucherCsvFileRepository.create(fixedAmountVoucher); + voucherCsvFileRepository.save(fixedAmountVoucher); assertThat(voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()).isPresent(), is(true)); @@ -85,7 +85,7 @@ void deleteVoucherById() { @Test void deleteAllVoucher() { - voucherCsvFileRepository.create(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + voucherCsvFileRepository.save(new FixedAmountVoucher(UUID.randomUUID(), 1000)); voucherCsvFileRepository.deleteAll(); diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java index e51ea36480..90a54c4dd2 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java @@ -34,7 +34,7 @@ void findVoucherByIdFromMemory() { UUID voucherId = UUID.randomUUID(); FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(voucherId, 1000); - voucherMemoryRepository.create(fixedAmountVoucher); + voucherMemoryRepository.save(fixedAmountVoucher); Optional retrievedVoucher = voucherMemoryRepository.findById(voucherId); Optional randomRetrievedVoucher = voucherMemoryRepository.findById(UUID.randomUUID()); @@ -49,8 +49,8 @@ void findAllVoucherFromMemory() { PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 6000); - voucherMemoryRepository.create(percentDiscountVoucher); - voucherMemoryRepository.create(fixedAmountVoucher); + voucherMemoryRepository.save(percentDiscountVoucher); + voucherMemoryRepository.save(fixedAmountVoucher); assertThat(voucherMemoryRepository.findAll(), hasSize(2)); assertThat(voucherMemoryRepository.findAll(), hasItem(percentDiscountVoucher)); @@ -62,19 +62,19 @@ void createNewVouchersToMemory() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 30); - this.voucherMemoryRepository.create(fixedAmountVoucher); - this.voucherMemoryRepository.create(percentDiscountVoucher); + this.voucherMemoryRepository.save(fixedAmountVoucher); + this.voucherMemoryRepository.save(percentDiscountVoucher); assertThat(voucherMemoryRepository.findAll(), hasSize(2)); assertThrows(IllegalArgumentException.class, - () -> voucherMemoryRepository.create(new PercentDiscountVoucher(UUID.randomUUID(), 0))); + () -> voucherMemoryRepository.save(new PercentDiscountVoucher(UUID.randomUUID(), 0))); } @Test void deleteAllVouchersFromMemory() { - voucherMemoryRepository.create(new PercentDiscountVoucher(UUID.randomUUID(), 10)); - voucherMemoryRepository.create(new PercentDiscountVoucher(UUID.randomUUID(), 20)); - voucherMemoryRepository.create(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + voucherMemoryRepository.save(new PercentDiscountVoucher(UUID.randomUUID(), 10)); + voucherMemoryRepository.save(new PercentDiscountVoucher(UUID.randomUUID(), 20)); + voucherMemoryRepository.save(new FixedAmountVoucher(UUID.randomUUID(), 1000)); voucherMemoryRepository.deleteAll(); From 6d527b686ca478fb7f6619ae447f1976bb9bfb47 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 21:49:56 +0900 Subject: [PATCH 28/62] =?UTF-8?q?Feat:=20vouchers=20=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=B8=94=20=EC=A0=95=EC=9D=98=20SQL=EB=AC=B8=EC=9D=84=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/schema.sql | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 5a93592c33..641f9e41a6 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS customers( +CREATE TABLE IF NOT EXISTS customers ( customer_id BINARY(16) PRIMARY KEY, name VARCHAR(26) NOT NULL, email VARCHAR(56) NOT NULL , @@ -7,3 +7,8 @@ CREATE TABLE IF NOT EXISTS customers( is_blacked BOOLEAN NOT NULL DEFAULT FALSE, CONSTRAINT unq_user_email UNIQUE (email) ); + +CREATE TABLE IF NOT EXISTS vouchers ( + voucher_id BINARY(16) PRIMARY KEY, + discount_degree bigint NOT NULL +); From a03c585696e223ffb005d61ec9ab37ff9179514a Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Sun, 29 Oct 2023 21:55:06 +0900 Subject: [PATCH 29/62] =?UTF-8?q?Chore:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=EC=9D=84=20=EB=B0=94=EA=BE=BC=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootbasic/common/file/VoucherCsvFileManager.java | 2 +- .../springbootbasic/domain/voucher/FixedAmountVoucher.java | 2 +- .../springbootbasic/domain/voucher/PercentDiscountVoucher.java | 2 +- .../org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index 521afd053c..9728e45a0a 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -65,7 +65,7 @@ private String convertToString(VoucherPolicy voucherPolicy){ sb.append(","); sb.append(voucherPolicy.getClass().getSimpleName()); sb.append(","); - sb.append(voucherPolicy.getDiscountAmount()); + sb.append(voucherPolicy.getDiscountDegree()); sb.append(System.lineSeparator()); return sb.toString(); diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java index 9eea6ea667..aa71ce3f89 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java @@ -23,7 +23,7 @@ public UUID getVoucherId() { } @Override - public long getDiscountAmount() { + public long getDiscountDegree() { return this.amount; } diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java index cc7446ba3e..d0916a32eb 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java @@ -26,7 +26,7 @@ public UUID getVoucherId() { } @Override - public long getDiscountAmount() { + public long getDiscountDegree() { return this.percent; } diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java index c789066c6e..a969b2b3ee 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java @@ -5,5 +5,5 @@ public interface VoucherPolicy { UUID getVoucherId(); long discount(long beforeDiscount); - long getDiscountAmount(); + long getDiscountDegree(); } From e74d7bbc6582032c8445b4a6c74f902b6c552afc Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 11:20:29 +0900 Subject: [PATCH 30/62] =?UTF-8?q?Feat:=20vouchers=20=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=B8=94=EC=97=90=20=ED=83=80=EC=9E=85=20=EC=BB=AC=EB=9F=BC?= =?UTF-8?q?=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/schema.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 641f9e41a6..4330242be4 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -10,5 +10,6 @@ CREATE TABLE IF NOT EXISTS customers ( CREATE TABLE IF NOT EXISTS vouchers ( voucher_id BINARY(16) PRIMARY KEY, - discount_degree bigint NOT NULL + discount_degree bigint NOT NULL, + voucher_type varchar(64) NOT NULL ); From b959918c001a527289fec0e392a7c894eb0168f5 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 11:23:51 +0900 Subject: [PATCH 31/62] =?UTF-8?q?Feat:=20DB=EB=A1=9C=20Voucher=EB=A5=BC=20?= =?UTF-8?q?=EA=B4=80=EB=A6=AC=ED=95=98=EB=8A=94=20Repository=EB=A5=BC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../voucher/VoucherDatabaseRepository.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java new file mode 100644 index 0000000000..e039b49623 --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java @@ -0,0 +1,95 @@ +package org.prgms.springbootbasic.repository.voucher; + +import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.domain.VoucherType; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.springframework.context.annotation.Profile; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Repository; + +import java.util.*; + +import static org.prgms.springbootbasic.common.UtilMethod.bytesToUUID; + +@Repository +@Slf4j +@Profile({"dev", "prod"}) +public class VoucherDatabaseRepository implements VoucherRepository { + private final NamedParameterJdbcTemplate jdbcTemplate; + + public VoucherDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + public VoucherPolicy save(VoucherPolicy voucher) { + Optional foundVoucher = findById(voucher.getVoucherId()); + + if (foundVoucher.isPresent()){ + jdbcTemplate.update("UPDATE vouchers SET discount_degree = :discountDegree, voucher_type = :voucherType WHERE voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", + toParamMap(voucher)); + } else { + jdbcTemplate.update("INSERT INTO vouchers(voucher_id, discount_degree, voucher_type) " + + "VALUES (UNHEX(REPLACE(:voucherId, '-', '')), :discountDegree, :voucherType)", + toParamMap(voucher)); + } + + return voucher; + } + + @Override + public Optional findById(UUID voucherId) { + try { + return Optional.ofNullable( + jdbcTemplate.queryForObject("SELECT * FROM vouchers WHERE voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", + Collections.singletonMap("voucherId", voucherId.toString().getBytes()), + mapToVoucher)); + } catch (EmptyResultDataAccessException e) { + log.info("voucherId에 해당하는 Voucher가 DB에 없음."); + return Optional.empty(); + } + } + + @Override + public List findAll() { + return jdbcTemplate.query("SELECT * FROM vouchers", mapToVoucher); + } + + @Override + public Optional deleteById(UUID voucherId) { + Optional deleteVoucher = findById(voucherId); + + jdbcTemplate.update("DELETE FROM vouchers WHERE voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", + Collections.singletonMap("voucherId", voucherId.toString().getBytes())); + return deleteVoucher; + } + + @Override + public void deleteAll() { + jdbcTemplate.update("DELETE FROM vouchers", Collections.emptyMap()); + } + + private static Map toParamMap(VoucherPolicy voucher) { + return new HashMap<>(){{ + put("voucherId", voucher.getVoucherId().toString().getBytes()); + put("discountDegree", voucher.getDiscountDegree()); + put("voucherType", voucher.getClass().getSimpleName()); + }}; + } + + private static RowMapper mapToVoucher = (rs, rowNum) -> { + UUID voucherId = bytesToUUID(rs.getBytes("voucher_id")); + long discountDegree = rs.getLong("discount_degree"); + String voucherTypeString = rs.getString("voucher_type"); + VoucherType voucherType = Arrays.stream(VoucherType.values()) + .filter(vt -> vt.getDisplayName().equals(voucherTypeString)) + .findAny() + .orElseThrow(() -> new NoSuchElementException("해당 VoucherType이 존재하지 않음.")); + + return voucherType.create(voucherId, discountDegree); + }; + + +} From 957dc408362a22cd031ed8983924cb616f186ce8 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 13:38:01 +0900 Subject: [PATCH 32/62] =?UTF-8?q?Refactor:=20=EA=B5=AC=EC=84=B1=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EC=9D=84=20=EC=88=98=EC=A0=95=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/common/Console.java | 3 ++- .../common/file/CsvFileTemplate.java | 2 ++ .../common/file/CustomerCsvFileManager.java | 2 +- .../common/file/VoucherCsvFileManager.java | 2 +- .../springbootbasic/controller/MainController.java | 4 ++-- .../customer/CustomerDatabaseRepository.java | 1 - .../voucher/VoucherCsvFileRepository.java | 7 +++++-- .../repository/voucher/VoucherMemoryRepository.java | 7 +++++-- src/main/resources/application-dev.yml | 9 --------- src/main/resources/application-local.yml | 3 +++ src/main/resources/application-prod.yml | 3 --- src/main/resources/application-test.yml | 3 +++ src/main/resources/application.yml | 5 +++++ .../{ => voucher}/VoucherCsvFileRepositoryTest.java | 13 ++++++------- .../{ => voucher}/VoucherMemoryRepositoryTest.java | 13 ++++++------- 15 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 src/main/resources/application-local.yml create mode 100644 src/main/resources/application-test.yml rename src/test/java/org/prgms/springbootbasic/repository/{ => voucher}/VoucherCsvFileRepositoryTest.java (86%) rename src/test/java/org/prgms/springbootbasic/repository/{ => voucher}/VoucherMemoryRepositoryTest.java (87%) diff --git a/src/main/java/org/prgms/springbootbasic/common/Console.java b/src/main/java/org/prgms/springbootbasic/common/Console.java index e3cbb3b4c9..297abbeef7 100644 --- a/src/main/java/org/prgms/springbootbasic/common/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/Console.java @@ -5,7 +5,8 @@ import org.springframework.stereotype.Component; import java.text.MessageFormat; -import java.util.*; +import java.util.Collection; +import java.util.Scanner; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; diff --git a/src/main/java/org/prgms/springbootbasic/common/file/CsvFileTemplate.java b/src/main/java/org/prgms/springbootbasic/common/file/CsvFileTemplate.java index fb2fe2b1ec..a715da97df 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/CsvFileTemplate.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/CsvFileTemplate.java @@ -2,6 +2,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import java.io.*; @@ -11,6 +12,7 @@ import java.util.function.Function; @Component +@Profile("test") public class CsvFileTemplate { private static final Logger log = LoggerFactory.getLogger(CsvFileTemplate.class); diff --git a/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java index 24b8084b1f..6d5ba4ab48 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/CustomerCsvFileManager.java @@ -13,7 +13,7 @@ import static org.prgms.springbootbasic.common.CommonConstant.CSV_PATTERN; @Component -@Profile({"dev", "prod"}) +@Profile({"test"}) @Slf4j public class CustomerCsvFileManager { private static final String BLACK_PATH = "./src/main/resources/customer_blacklist.csv"; diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index 9728e45a0a..f04d613981 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -15,7 +15,7 @@ @Component @Slf4j -@Profile({"dev", "prod"}) +@Profile({"test"}) public class VoucherCsvFileManager { @Value("${basic.file.path}") private String FILE_PATH; diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index 3db9514923..05dcdd2cef 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -66,13 +66,13 @@ public void create(){ } private void list(){ - List voucherPolicies = voucherService.findAll(); // 뷰가 다른 클래스에 의존 + List voucherPolicies = voucherService.findAll(); Console.printList(voucherPolicies); } private void black(){ - List blacklist = customerService.findBlackAll(); // customerRepository에 의존 + List blacklist = customerService.findBlackAll(); Console.printList(blacklist); } diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java index f96b71069b..715ba08a77 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java @@ -9,7 +9,6 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Repository; -import java.nio.ByteBuffer; import java.time.LocalDateTime; import java.util.*; diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java index ce007e901d..4b725ecf45 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java @@ -9,11 +9,14 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @Repository -@Profile({"dev", "prod"}) +@Profile({"test"}) @Primary @Slf4j public class VoucherCsvFileRepository implements VoucherRepository { diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java index 93fc37a079..18504a93f6 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java @@ -5,11 +5,14 @@ import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @Repository -@Profile({"local", "test"}) +@Profile({"local"}) @Slf4j public class VoucherMemoryRepository implements VoucherRepository{ private final ConcurrentHashMap mem = new ConcurrentHashMap<>(); diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index c1a5857bce..8b13789179 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -1,10 +1 @@ -spring: - datasource: - url: jdbc:mysql://localhost:3306/test - username: test - password: 1234 - driver-class-name: com.mysql.cj.jdbc.Driver -basic: - file: - path: ./src/test/resources/voucher.csv diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml new file mode 100644 index 0000000000..afd1b972b9 --- /dev/null +++ b/src/main/resources/application-local.yml @@ -0,0 +1,3 @@ +basic: + file: + path: ./src/main/resources/voucher.csv diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index afd1b972b9..e69de29bb2 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -1,3 +0,0 @@ -basic: - file: - path: ./src/main/resources/voucher.csv diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml new file mode 100644 index 0000000000..00d124a133 --- /dev/null +++ b/src/main/resources/application-test.yml @@ -0,0 +1,3 @@ +basic: + file: + path: ./src/test/resources/voucher.csv diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9f96606843..625e09e389 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,3 +1,8 @@ spring: profiles: active: prod + datasource: + url: jdbc:mysql://localhost:3306/test + username: test + password: 1234 + driver-class-name: com.mysql.cj.jdbc.Driver diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java similarity index 86% rename from src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java rename to src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java index d8517c2239..14b5f04a51 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherCsvFileRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java @@ -1,11 +1,10 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.voucher; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; -import org.prgms.springbootbasic.repository.voucher.VoucherCsvFileRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @@ -16,8 +15,8 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -@ActiveProfiles("dev") @SpringBootTest +@ActiveProfiles("test") class VoucherCsvFileRepositoryTest { @Autowired @@ -49,8 +48,8 @@ void findAllVoucherFromFile() { voucherCsvFileRepository.save(percentDiscountVoucher); assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); - assertThat(voucherCsvFileRepository.findAll(), hasItem(fixedAmountVoucher)); - assertThat(voucherCsvFileRepository.findAll(), hasItem(percentDiscountVoucher)); + assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); + assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(percentDiscountVoucher))); } @Test @@ -62,8 +61,8 @@ void createVoucherToFile() { voucherCsvFileRepository.save(percentDiscountVoucher); assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); - assertThat(voucherCsvFileRepository.findAll(), hasItem(fixedAmountVoucher)); - assertThat(voucherCsvFileRepository.findAll(), hasItem(percentDiscountVoucher)); + assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); + assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(percentDiscountVoucher))); assertThat(voucherCsvFileRepository.findAll(), not(hasItem(new FixedAmountVoucher(UUID.randomUUID(), 2000)))); } diff --git a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java similarity index 87% rename from src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java rename to src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java index 90a54c4dd2..06c8916214 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java @@ -1,11 +1,10 @@ -package org.prgms.springbootbasic.repository; +package org.prgms.springbootbasic.repository.voucher; -import org.junit.jupiter.api.*; -import org.prgms.springbootbasic.BasicApplication; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; -import org.prgms.springbootbasic.repository.voucher.VoucherMemoryRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -17,7 +16,7 @@ import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertThrows; -@SpringJUnitConfig(BasicApplication.class) +@SpringJUnitConfig(VoucherMemoryRepository.class) @ActiveProfiles("local") class VoucherMemoryRepositoryTest { @@ -53,8 +52,8 @@ void findAllVoucherFromMemory() { voucherMemoryRepository.save(fixedAmountVoucher); assertThat(voucherMemoryRepository.findAll(), hasSize(2)); - assertThat(voucherMemoryRepository.findAll(), hasItem(percentDiscountVoucher)); - assertThat(voucherMemoryRepository.findAll(), hasItem(fixedAmountVoucher)); + assertThat(voucherMemoryRepository.findAll(), hasItem(samePropertyValuesAs(percentDiscountVoucher))); + assertThat(voucherMemoryRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); } @Test From facf373be92b5503ec75381d808ac26f68bcb927 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 13:38:36 +0900 Subject: [PATCH 33/62] =?UTF-8?q?Test:=20DB=20=EA=B4=80=EB=A6=AC=20Voucher?= =?UTF-8?q?Repository=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VoucherDatabaseRepositoryTest.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java new file mode 100644 index 0000000000..049b981a6b --- /dev/null +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java @@ -0,0 +1,106 @@ +package org.prgms.springbootbasic.repository.voucher; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + + +@SpringBootTest +@ActiveProfiles("dev") +class VoucherDatabaseRepositoryTest { + @Autowired + private VoucherDatabaseRepository voucherDatabaseRepository; + + private PercentDiscountVoucher setUpVoucher; + @BeforeEach + void setUp() { + setUpVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 40); + voucherDatabaseRepository.save(setUpVoucher); + } + + @AfterEach + void clean() { + voucherDatabaseRepository.deleteAll(); + } + + @Test + void saveNewVoucherToDB() { + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + + voucherDatabaseRepository.save(fixedAmountVoucher); + + Optional retrievedVoucher = voucherDatabaseRepository.findById(fixedAmountVoucher.getVoucherId()); + + assertThat(retrievedVoucher.isPresent(), is(true)); + assertThat(retrievedVoucher.get(), samePropertyValuesAs(fixedAmountVoucher)); + } + + @Test + void updateVoucherInDB() { + FixedAmountVoucher updateVoucher = new FixedAmountVoucher(setUpVoucher.getVoucherId(), 2000); + + voucherDatabaseRepository.save(updateVoucher); + + Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); + + assertThat(retrievedVoucher.isPresent(), is(true)); + assertThat(retrievedVoucher.get(), samePropertyValuesAs(updateVoucher)); + } + + @Test + void findVoucherByIdInDB() { + Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); + + assertThat(retrievedVoucher.isPresent(), is(true)); + assertThat(retrievedVoucher.get(), samePropertyValuesAs(setUpVoucher)); + } + + @Test + void findAllVouchersInDB() { + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 3000); + + voucherDatabaseRepository.save(fixedAmountVoucher); + + List vouchers = voucherDatabaseRepository.findAll(); + + assertThat(vouchers, hasSize(2)); + assertThat(vouchers, hasItem(samePropertyValuesAs(fixedAmountVoucher))); + assertThat(vouchers, hasItem(samePropertyValuesAs(setUpVoucher))); + } + + @Test + void deleteVoucherByIdInDB() { + Optional deleteVoucher = voucherDatabaseRepository.deleteById(setUpVoucher.getVoucherId()); + + List vouchers = voucherDatabaseRepository.findAll(); + + assertThat(deleteVoucher.isPresent(), is(true)); + assertThat(deleteVoucher.get(), samePropertyValuesAs(setUpVoucher)); + assertThat(vouchers, hasSize(0)); + } + + @Test + void deleteAllVouchersInDB() { + FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 3000); + + voucherDatabaseRepository.save(fixedAmountVoucher); + voucherDatabaseRepository.deleteAll(); + + List vouchers = voucherDatabaseRepository.findAll(); + + assertThat(vouchers.size(), is(0)); + } +} From e2f9d158870cfec9189ecc53ce9a086c8806cdd2 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 17:23:42 +0900 Subject: [PATCH 34/62] =?UTF-8?q?Feat:=20=EB=B0=94=EC=9A=B0=EC=B2=98=20?= =?UTF-8?q?=EC=A7=80=EA=B0=91=20=EC=BD=98=EC=86=94=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EC=9D=84=20=EB=B0=9B=EB=8A=94=20WalletConsole=EC=9D=84=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=98=EA=B3=A0=20=EC=B5=9C=EC=B4=88=20?= =?UTF-8?q?=EC=BD=98=EC=86=94=20=EC=9E=85=EC=B6=9C=EB=A0=A5=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=A5=BC=20MainConsole=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/console/Console.java | 7 +++ .../MainConsole.java} | 8 +-- .../common/console/WalletConsole.java | 38 ++++++++++++++ .../controller/MainController.java | 52 +++++++++++-------- 4 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 src/main/java/org/prgms/springbootbasic/common/console/Console.java rename src/main/java/org/prgms/springbootbasic/common/{Console.java => console/MainConsole.java} (90%) create mode 100644 src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java diff --git a/src/main/java/org/prgms/springbootbasic/common/console/Console.java b/src/main/java/org/prgms/springbootbasic/common/console/Console.java new file mode 100644 index 0000000000..137a6752aa --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/common/console/Console.java @@ -0,0 +1,7 @@ +package org.prgms.springbootbasic.common.console; + +import java.util.Scanner; + +public class Console { + public static final Scanner consoleInput = new Scanner(System.in); +} diff --git a/src/main/java/org/prgms/springbootbasic/common/Console.java b/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java similarity index 90% rename from src/main/java/org/prgms/springbootbasic/common/Console.java rename to src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java index 297abbeef7..d85cc2de5f 100644 --- a/src/main/java/org/prgms/springbootbasic/common/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.common; +package org.prgms.springbootbasic.common.console; import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.VoucherType; @@ -6,15 +6,14 @@ import java.text.MessageFormat; import java.util.Collection; -import java.util.Scanner; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; +import static org.prgms.springbootbasic.common.console.Console.consoleInput; @Component @Slf4j -public class Console { - private static final Scanner consoleInput = new Scanner(System.in); +public class MainConsole { public static String readCommand() { System.out.println("=== Voucher Program ==="); @@ -22,6 +21,7 @@ public static String readCommand() { System.out.println("Type 'create' to create a new voucher."); System.out.println("Type 'list' to list all vouchers."); System.out.println("Type 'black' to list customers blacked."); + System.out.println("Type 'wallet' to enter wallet service."); return consoleInput.next(); } diff --git a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java new file mode 100644 index 0000000000..fff86e2e6d --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java @@ -0,0 +1,38 @@ +package org.prgms.springbootbasic.common.console; + +import java.text.MessageFormat; + +import static org.prgms.springbootbasic.common.console.Console.consoleInput; + +public class WalletConsole { + public static String readCommand(){ + System.out.println(); + System.out.println("Welcome to our wallet service."); + System.out.println("Type 'allocate' if you want to allocate voucher to a customer." ); + System.out.println("Type 'delete' if you want to delete voucher from a customer."); + System.out.println("Type 'showVoucher' to view customers with a voucher."); + System.out.println("Type 'showCustomer' to view vouchers that a customer has."); + + return consoleInput.next(); + } + + public static String allocateVoucher(){ + + } + + public static String typeCustomerId(){ + System.out.println("Type customer Id."); + + return consoleInput.next(); + } + + public static String typeVoucherId(){ + System.out.println("Type voucher Id."); + + return consoleInput.next(); + } + + public static void success(String command){ + System.out.println("'" + command + "' command successfully executed."); + } +} diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index 05dcdd2cef..e8e647a0fe 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -1,7 +1,7 @@ package org.prgms.springbootbasic.controller; import lombok.extern.slf4j.Slf4j; -import org.prgms.springbootbasic.common.Console; +import org.prgms.springbootbasic.common.console.MainConsole; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.customer.Customer; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; @@ -20,24 +20,27 @@ public class MainController { private static final String LIST = "list"; private static final String CREATE = "create"; private static final String BLACK = "black"; + private static final String WALLET = "wallet"; private final VoucherService voucherService; private final CustomerService customerService; + private final WalletController walletController; - public MainController(VoucherService voucherService, CustomerService customerService) { + public MainController(VoucherService voucherService, CustomerService customerService, WalletController walletController) { this.voucherService = voucherService; this.customerService = customerService; + this.walletController = walletController; } public void run() { String command = ""; while (!command.equals(EXIT)) { try { - command = Console.readCommand(); + command = MainConsole.readCommand(); executeCommand(command); } catch (InputMismatchException e) { - String invalidVal = Console.ignoreLine(); + String invalidVal = MainConsole.ignoreLine(); log.warn("User input = {}", invalidVal); throw new IllegalArgumentException("Not integer."); @@ -48,19 +51,33 @@ public void run() { log.error("Scanner is closed"); throw new RuntimeException("Scanner is closed."); } catch (IllegalArgumentException e) { - Console.printArgException(); + MainConsole.printArgException(); } catch (RuntimeException e) { - Console.printRuntimeException(); + MainConsole.printRuntimeException(); } } } - public void create(){ - int voucherSeq = Console.selectCreateType(); + private void executeCommand(String command) { + switch (command) { + case CREATE -> create(); + case LIST -> list(); + case BLACK -> black(); + case WALLET -> wallet(); + case EXIT -> {} + default -> { + log.warn("invalid command. now command = {}", command); + throw new IllegalArgumentException("Invalid command. Type command again."); + } + } + } + + private void create(){ + int voucherSeq = MainConsole.selectCreateType(); VoucherType voucherType = voucherService.seqToType(voucherSeq); - int discountDegree = Console.putDiscountDegree(voucherType); + int discountDegree = MainConsole.putDiscountDegree(voucherType); voucherService.create(voucherType, discountDegree); } @@ -68,25 +85,16 @@ public void create(){ private void list(){ List voucherPolicies = voucherService.findAll(); - Console.printList(voucherPolicies); + MainConsole.printList(voucherPolicies); } private void black(){ List blacklist = customerService.findBlackAll(); - Console.printList(blacklist); + MainConsole.printList(blacklist); } - private void executeCommand(String command) { - switch (command) { - case CREATE -> create(); - case LIST -> list(); - case BLACK -> black(); - case EXIT -> {} - default -> { - log.warn("invalid command. now command = {}", command); - throw new IllegalArgumentException("Invalid command. Type command again."); - } - } + private void wallet(){ + walletController.run(); } } From a0599429f106c98c55a3aa3d3fe1cad9cf2ea160 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 17:23:52 +0900 Subject: [PATCH 35/62] =?UTF-8?q?Feat:=20=EB=B0=94=EC=9A=B0=EC=B2=98=20?= =?UTF-8?q?=EC=A7=80=EA=B0=91=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WalletController.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/java/org/prgms/springbootbasic/controller/WalletController.java diff --git a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java new file mode 100644 index 0000000000..99f5649818 --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java @@ -0,0 +1,60 @@ +package org.prgms.springbootbasic.controller; + +import org.prgms.springbootbasic.common.console.WalletConsole; +import org.springframework.stereotype.Controller; + +@Controller +public class WalletController { + private final String ALLOCATE = "allocate"; + private final String DELETE = "delete"; + private final String SHOW_CUSTOMER = "showCustomer"; + private final String SHOW_VOUCHER = "showVoucher"; + + private final WalletService walletService; + + public WalletController(WalletService walletService) { + this.walletService = walletService; + } + + public void run(){ + String command = WalletConsole.readCommand(); + + executeCommand(command); + WalletConsole.success(command); + } + + private void executeCommand(String command){ + switch (command){ + case ALLOCATE -> allocate(); + case DELETE -> delete(); + case SHOW_CUSTOMER -> showCustomer(); + case SHOW_VOUCHER -> showVoucher(); + } + } + + private void allocate(){ + String customerId = WalletConsole.typeCustomerId(); + String voucherId = WalletConsole.typeVoucherId(); + + walletService.allocate(customerId, voucherId); + } + + private void delete(){ + String customerId = WalletConsole.typeCustomerId(); + String voucherId = WalletConsole.typeVoucherId(); + + walletService.delete(customerId, voucherId); + } + + private void showCustomer(){ + String customerId = WalletConsole.typeCustomerId(); + + walletService.showCustomer(customerId); + } + + private void showVoucher(){ + String voucherId = WalletConsole.typeVoucherId(); + + walletService.showVoucher(voucherId); + } +} From abc40630a8542e0edf986e415b1481c50beed1f9 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 17:24:42 +0900 Subject: [PATCH 36/62] =?UTF-8?q?Refactor:=20=ED=95=84=EC=9A=94=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../prgms/springbootbasic/common/console/WalletConsole.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java index fff86e2e6d..ba972db3c2 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java @@ -1,7 +1,5 @@ package org.prgms.springbootbasic.common.console; -import java.text.MessageFormat; - import static org.prgms.springbootbasic.common.console.Console.consoleInput; public class WalletConsole { @@ -16,10 +14,6 @@ public static String readCommand(){ return consoleInput.next(); } - public static String allocateVoucher(){ - - } - public static String typeCustomerId(){ System.out.println("Type customer Id."); From 326419f6723ec75e16b499104f258dd31c321413 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 17:36:58 +0900 Subject: [PATCH 37/62] =?UTF-8?q?Feat:=20String=EC=9D=84=20UUID=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD=ED=95=98=EB=8A=94=20=EA=B3=B5=ED=86=B5=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/prgms/springbootbasic/common/UtilMethod.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java b/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java index 882359e8b2..d3aa974353 100644 --- a/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java +++ b/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java @@ -9,4 +9,10 @@ public static UUID bytesToUUID(byte[] bytes) { return new UUID(wrappedByte.getLong(), wrappedByte.getLong()); } + + public static UUID stringToUUID(String str){ + byte[] bytes = str.getBytes(); + + return bytesToUUID(bytes); + } } From c9caf0d04769388ca7d2a9f04136a9bf7766f7e2 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 17:37:43 +0900 Subject: [PATCH 38/62] =?UTF-8?q?Refactor:=20Id=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EC=9D=84=20=EB=B0=9B=EC=9C=BC=EB=A9=B4=20UUID=EB=A1=9C=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/console/WalletConsole.java | 11 +++++++---- .../controller/WalletController.java | 13 ++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java index ba972db3c2..680a6571b1 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java @@ -1,5 +1,8 @@ package org.prgms.springbootbasic.common.console; +import java.util.UUID; + +import static org.prgms.springbootbasic.common.UtilMethod.stringToUUID; import static org.prgms.springbootbasic.common.console.Console.consoleInput; public class WalletConsole { @@ -14,16 +17,16 @@ public static String readCommand(){ return consoleInput.next(); } - public static String typeCustomerId(){ + public static UUID typeCustomerId(){ System.out.println("Type customer Id."); - return consoleInput.next(); + return stringToUUID(consoleInput.next()); } - public static String typeVoucherId(){ + public static UUID typeVoucherId(){ System.out.println("Type voucher Id."); - return consoleInput.next(); + return stringToUUID(consoleInput.next()); } public static void success(String command){ diff --git a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java index 99f5649818..614eb20c8a 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java @@ -1,8 +1,11 @@ package org.prgms.springbootbasic.controller; import org.prgms.springbootbasic.common.console.WalletConsole; +import org.prgms.springbootbasic.service.WalletService; import org.springframework.stereotype.Controller; +import java.util.UUID; + @Controller public class WalletController { private final String ALLOCATE = "allocate"; @@ -33,21 +36,21 @@ private void executeCommand(String command){ } private void allocate(){ - String customerId = WalletConsole.typeCustomerId(); - String voucherId = WalletConsole.typeVoucherId(); + UUID customerId = WalletConsole.typeCustomerId(); + UUID voucherId = WalletConsole.typeVoucherId(); walletService.allocate(customerId, voucherId); } private void delete(){ - String customerId = WalletConsole.typeCustomerId(); - String voucherId = WalletConsole.typeVoucherId(); + UUID customerId = WalletConsole.typeCustomerId(); + UUID voucherId = WalletConsole.typeVoucherId(); walletService.delete(customerId, voucherId); } private void showCustomer(){ - String customerId = WalletConsole.typeCustomerId(); + UUID customerId = WalletConsole.typeCustomerId(); walletService.showCustomer(customerId); } From e34bf01df36a3e5a9e0a6b58295021c2df427ddf Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 17:41:39 +0900 Subject: [PATCH 39/62] =?UTF-8?q?Refactor:=20=EC=A7=81=EA=B4=80=EC=A0=81?= =?UTF-8?q?=EC=9D=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=AA=85=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../prgms/springbootbasic/controller/WalletController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java index 614eb20c8a..575bdb9cac 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java @@ -52,12 +52,12 @@ private void delete(){ private void showCustomer(){ UUID customerId = WalletConsole.typeCustomerId(); - walletService.showCustomer(customerId); + walletService.showVouchersFromCustomer(customerId); } private void showVoucher(){ - String voucherId = WalletConsole.typeVoucherId(); + UUID voucherId = WalletConsole.typeVoucherId(); - walletService.showVoucher(voucherId); + walletService.showCustomerFromVoucher(voucherId); } } From 350a18147d774dfdd3ecbe99b9faf674d3acda1d Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 17:47:10 +0900 Subject: [PATCH 40/62] =?UTF-8?q?Feat:=20=EC=A7=80=EA=B0=91=20=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EB=B8=94=20wallet=20=EC=83=9D=EC=84=B1=20SQL=EB=AC=B8?= =?UTF-8?q?=EC=9D=84=20=EC=9E=91=EC=84=B1=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/schema.sql | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 4330242be4..dbf51628dd 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -13,3 +13,10 @@ CREATE TABLE IF NOT EXISTS vouchers ( discount_degree bigint NOT NULL, voucher_type varchar(64) NOT NULL ); + +CREATE TABLE IF NOT EXISTS wallet ( + customer_id BINARY(16) NOT NULL, + voucher_id BINARY(16) NOT NULL, + FOREIGN KEY (customer_id) references customers(customer_id), + FOREIGN KEY (voucher_id) references vouchers(voucher_id) +); From 5b98ac462c157b602bfa4ddfaf94a71f275349ae Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 18:09:51 +0900 Subject: [PATCH 41/62] =?UTF-8?q?Refactor:=20Colection=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=EC=9D=84=20=EA=B3=B5=ED=86=B5=20=EC=BD=98=EC=86=94=20?= =?UTF-8?q?=EB=B6=80=EB=B6=84=EC=9C=BC=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/common/console/Console.java | 7 +++++++ .../prgms/springbootbasic/common/console/MainConsole.java | 6 ------ .../prgms/springbootbasic/controller/MainController.java | 5 +++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/console/Console.java b/src/main/java/org/prgms/springbootbasic/common/console/Console.java index 137a6752aa..2caf1062b4 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/Console.java @@ -1,7 +1,14 @@ package org.prgms.springbootbasic.common.console; +import java.util.Collection; import java.util.Scanner; public class Console { public static final Scanner consoleInput = new Scanner(System.in); + + public static void printList(Collection collection) { + System.out.println(); + collection.forEach(System.out::println); + System.out.println(); + } } diff --git a/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java index d85cc2de5f..9d7fde23d6 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java @@ -48,12 +48,6 @@ public static String ignoreLine() { return consoleInput.nextLine(); } - public static void printList(Collection collection) { - System.out.println(); - collection.forEach(System.out::println); - System.out.println(); - } - public static void printArgException() { System.out.println("Invalid argument. Type command again."); } diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index e8e647a0fe..ed56459834 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -1,6 +1,7 @@ package org.prgms.springbootbasic.controller; import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.common.console.Console; import org.prgms.springbootbasic.common.console.MainConsole; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.customer.Customer; @@ -85,13 +86,13 @@ private void create(){ private void list(){ List voucherPolicies = voucherService.findAll(); - MainConsole.printList(voucherPolicies); + Console.printList(voucherPolicies); } private void black(){ List blacklist = customerService.findBlackAll(); - MainConsole.printList(blacklist); + Console.printList(blacklist); } private void wallet(){ From f773ecfd16a8de32db316bafa484ff3f9569f98d Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 18:11:58 +0900 Subject: [PATCH 42/62] =?UTF-8?q?Feat:=20=EB=B0=94=EC=9A=B0=EC=B2=98=20?= =?UTF-8?q?=EC=A7=80=EA=B0=91=20=EC=BD=98=EC=86=94=EA=B3=BC=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EB=B6=80=EB=B6=84=EC=9D=84=20=EC=99=84?= =?UTF-8?q?=EC=84=B1=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/console/MainConsole.java | 1 - .../common/console/WalletConsole.java | 1 + .../controller/WalletController.java | 10 ++++-- .../repository/wallet/WalletRepository.java | 14 ++++++++ .../service/WalletService.java | 34 +++++++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java create mode 100644 src/main/java/org/prgms/springbootbasic/service/WalletService.java diff --git a/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java index 9d7fde23d6..45d0a463c2 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java @@ -5,7 +5,6 @@ import org.springframework.stereotype.Component; import java.text.MessageFormat; -import java.util.Collection; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; diff --git a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java index 680a6571b1..eff48d4f42 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java @@ -31,5 +31,6 @@ public static UUID typeVoucherId(){ public static void success(String command){ System.out.println("'" + command + "' command successfully executed."); + System.out.println(); } } diff --git a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java index 575bdb9cac..dbc92ce746 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java @@ -1,9 +1,13 @@ package org.prgms.springbootbasic.controller; +import org.prgms.springbootbasic.common.console.Console; import org.prgms.springbootbasic.common.console.WalletConsole; +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.prgms.springbootbasic.service.WalletService; import org.springframework.stereotype.Controller; +import java.util.List; import java.util.UUID; @Controller @@ -51,13 +55,15 @@ private void delete(){ private void showCustomer(){ UUID customerId = WalletConsole.typeCustomerId(); + List vouchers = walletService.searchVouchersFromCustomer(customerId); - walletService.showVouchersFromCustomer(customerId); + Console.printList(vouchers); } private void showVoucher(){ UUID voucherId = WalletConsole.typeVoucherId(); + List customers = walletService.searchCustomerFromVoucher(voucherId); - walletService.showCustomerFromVoucher(voucherId); + Console.printList(customers); } } diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java new file mode 100644 index 0000000000..1d4f737a0a --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java @@ -0,0 +1,14 @@ +package org.prgms.springbootbasic.repository.wallet; + +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; + +import java.util.List; +import java.util.UUID; + +public interface WalletRepository { + void allocateVoucherById(UUID customerId, UUID voucherId); + void deleteVoucherById(UUID customerId, UUID voucherId); + List searchVouchersByCustomerId(UUID customerId); + List searchCustomersByVoucherId(UUID voucherId); +} diff --git a/src/main/java/org/prgms/springbootbasic/service/WalletService.java b/src/main/java/org/prgms/springbootbasic/service/WalletService.java new file mode 100644 index 0000000000..98f3b07e64 --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/service/WalletService.java @@ -0,0 +1,34 @@ +package org.prgms.springbootbasic.service; + +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.prgms.springbootbasic.repository.wallet.WalletRepository; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.UUID; + +@Service +public class WalletService { + private final WalletRepository walletRepository; + + public WalletService(WalletRepository walletRepository) { + this.walletRepository = walletRepository; + } + + public void allocate(UUID customerId, UUID voucherId){ + walletRepository.allocateVoucherById(customerId, voucherId); + } + + public void delete(UUID customerId, UUID voucherId){ + walletRepository.deleteVoucherById(customerId, voucherId); + } + + public List searchVouchersFromCustomer(UUID customerId){ + return walletRepository.searchVouchersByCustomerId(customerId); + } + + public List searchCustomerFromVoucher(UUID voucherId){ + return walletRepository.searchCustomersByVoucherId(voucherId); + } +} From aa4e08f7978531ee4e3ff3776b7f2b5c8c82e5cd Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 18:26:43 +0900 Subject: [PATCH 43/62] =?UTF-8?q?Feat:=20=EB=8D=B0=EC=9D=B4=ED=84=B0?= =?UTF-8?q?=EB=B2=A0=EC=9D=B4=EC=8A=A4=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=EB=A5=BC=20=EC=9E=A1=EB=8F=84=EB=A1=9D=20=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/controller/MainController.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index ed56459834..b2f35ce099 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -8,6 +8,7 @@ import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.prgms.springbootbasic.service.CustomerService; import org.prgms.springbootbasic.service.VoucherService; +import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Controller; import java.util.InputMismatchException; @@ -53,6 +54,9 @@ public void run() { throw new RuntimeException("Scanner is closed."); } catch (IllegalArgumentException e) { MainConsole.printArgException(); + } catch (DataAccessException e) { + log.error("Database error."); + MainConsole.printRuntimeException(); } catch (RuntimeException e) { MainConsole.printRuntimeException(); } From 7a81749764a1a34ede85b33192b8c0197b2777c2 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 21:41:51 +0900 Subject: [PATCH 44/62] =?UTF-8?q?Feat:=20DataAccessException=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=20=EC=8B=9C,=20=EB=A1=9C=EA=B7=B8=EC=97=90=20?= =?UTF-8?q?=EC=8A=A4=ED=83=9D=20=ED=8A=B8=EB=A0=88=EC=9D=B4=EC=8A=A4?= =?UTF-8?q?=EB=A5=BC=20=EB=82=A8=EA=B8=B0=EB=8F=84=EB=A1=9D=20=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/controller/MainController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index b2f35ce099..a6cc8ee696 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -55,7 +55,7 @@ public void run() { } catch (IllegalArgumentException e) { MainConsole.printArgException(); } catch (DataAccessException e) { - log.error("Database error."); + log.error("Database error.", e); MainConsole.printRuntimeException(); } catch (RuntimeException e) { MainConsole.printRuntimeException(); From 96d61216a1d99bb509781a039a3dcea5e0814e93 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 21:42:08 +0900 Subject: [PATCH 45/62] =?UTF-8?q?Refactor:=20=EC=9D=B4=EB=AF=B8=20?= =?UTF-8?q?=EC=A1=B4=EC=9E=AC=ED=95=98=EB=8A=94=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=9D=B4=EC=9A=A9=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/prgms/springbootbasic/common/UtilMethod.java | 6 ------ .../prgms/springbootbasic/common/console/WalletConsole.java | 5 ++--- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java b/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java index d3aa974353..882359e8b2 100644 --- a/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java +++ b/src/main/java/org/prgms/springbootbasic/common/UtilMethod.java @@ -9,10 +9,4 @@ public static UUID bytesToUUID(byte[] bytes) { return new UUID(wrappedByte.getLong(), wrappedByte.getLong()); } - - public static UUID stringToUUID(String str){ - byte[] bytes = str.getBytes(); - - return bytesToUUID(bytes); - } } diff --git a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java index eff48d4f42..83730e15cf 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java @@ -2,7 +2,6 @@ import java.util.UUID; -import static org.prgms.springbootbasic.common.UtilMethod.stringToUUID; import static org.prgms.springbootbasic.common.console.Console.consoleInput; public class WalletConsole { @@ -20,13 +19,13 @@ public static String readCommand(){ public static UUID typeCustomerId(){ System.out.println("Type customer Id."); - return stringToUUID(consoleInput.next()); + return UUID.fromString(consoleInput.next()); } public static UUID typeVoucherId(){ System.out.println("Type voucher Id."); - return stringToUUID(consoleInput.next()); + return UUID.fromString(consoleInput.next()); } public static void success(String command){ From 3b1136bb8adbc9a6c78115f19640be8c854fb8ea Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 21:42:41 +0900 Subject: [PATCH 46/62] =?UTF-8?q?Feat:=20DB=EC=97=90=EC=84=9C=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8A=94=20=EB=B0=94=EC=9A=B0=EC=B2=98=20?= =?UTF-8?q?=EC=A7=80=EA=B0=91=20Repository=EB=A5=BC=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wallet/WalletDatabaseRepository.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java new file mode 100644 index 0000000000..107b34dca0 --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java @@ -0,0 +1,84 @@ +package org.prgms.springbootbasic.repository.wallet; + +import org.prgms.springbootbasic.common.UtilMethod; +import org.prgms.springbootbasic.domain.VoucherType; +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Repository; + +import java.time.LocalDateTime; +import java.util.*; + +import static org.prgms.springbootbasic.common.UtilMethod.bytesToUUID; + +@Repository +public class WalletDatabaseRepository implements WalletRepository { + private final NamedParameterJdbcTemplate jdbcTemplate; + + public WalletDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + public void allocateVoucherById(UUID customerId, UUID voucherId) { + jdbcTemplate.update("INSERT INTO wallet VALUES (UNHEX(REPLACE(:customerId, '-', '')), UNHEX(REPLACE(:voucherId, '-', '')))", + toParamMap(customerId, voucherId)); + } + + @Override + public void deleteVoucherById(UUID customerId, UUID voucherId) { + jdbcTemplate.update("DELETE FROM wallet WHERE customer_id = UNHEX(REPLACE(:customerId, '-', '')) AND voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", + toParamMap(customerId, voucherId)); + } + + @Override + public List searchVouchersByCustomerId(UUID customerId) { + return jdbcTemplate.query("SELECT v.voucher_id, v.discount_degree, v.voucher_type " + + "FROM vouchers v JOIN wallet w ON v.voucher_id = w.voucher_id " + + "WHERE w.customer_id = UNHEX(REPLACE(:customerId, '-', ''))", + Collections.singletonMap("customerId", customerId.toString().getBytes()), + mapToVoucher); + } + + @Override + public List searchCustomersByVoucherId(UUID voucherId) { + return jdbcTemplate.query("SELECT c.customer_id, c.name, c.email, c.last_login_at, c.created_at, c.is_blacked " + + "FROM customers c " + + "JOIN wallet w ON c.customer_id = w.customer_id " + + "WHERE w.voucher_id = UUID_TO_BIN(:voucherId)", + Collections.singletonMap("voucherId", voucherId.toString().getBytes()), + mapToCustomer); + } + + private Map toParamMap(UUID customerId, UUID voucherId){ + return new HashMap<>(){{ + put("customerId", customerId.toString().getBytes()); + put("voucherId", voucherId.toString().getBytes()); + }}; + } + private static RowMapper mapToVoucher = (rs, rowNum) -> { + UUID voucherId = bytesToUUID(rs.getBytes("voucher_id")); + long discountDegree = rs.getLong("discount_degree"); + String voucherTypeString = rs.getString("voucher_type"); + VoucherType voucherType = Arrays.stream(VoucherType.values()) + .filter(vt -> vt.getDisplayName().equals(voucherTypeString)) + .findAny() + .orElseThrow(() -> new NoSuchElementException("해당 VoucherType이 존재하지 않음.")); + + return voucherType.create(voucherId, discountDegree); + }; + + private static RowMapper mapToCustomer = (rs, rowNum) -> { + String customerName = rs.getString("name"); + UUID customerId = UtilMethod.bytesToUUID(rs.getBytes("customer_id")); + String email = rs.getString("email"); + LocalDateTime createdAt = rs.getTimestamp("created_at").toLocalDateTime(); + LocalDateTime lastLoginAt = rs.getTimestamp("last_login_at") != null + ? rs.getTimestamp("last_login_at").toLocalDateTime() : null; + boolean isBlacked = rs.getBoolean("is_blacked"); + + return new Customer(customerId, customerName, email, lastLoginAt, createdAt, isBlacked); + }; +} From 193f73c8b9e6ba4c21285958fcd04fa33dc9f033 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 21:51:25 +0900 Subject: [PATCH 47/62] =?UTF-8?q?Fix:=20=EB=B6=80=ED=8A=B8=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=EC=9D=84=20=EB=B3=80=EA=B2=BD=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-dev.yml | 7 ++++++- src/main/resources/application-prod.yml | 6 ++++++ src/main/resources/application.yml | 5 ----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 8b13789179..057274fbff 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -1 +1,6 @@ - +spring: + datasource: + url: jdbc:mysql://localhost:3306/test + username: test + password: 1234 + driver-class-name: com.mysql.cj.jdbc.Driver diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index e69de29bb2..64c9e21ad4 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -0,0 +1,6 @@ +spring: + datasource: + url: jdbc:mysql://localhost:3306/prod + username: root + password: 1234 + driver-class-name: com.mysql.cj.jdbc.Driver diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 625e09e389..9f96606843 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,8 +1,3 @@ spring: profiles: active: prod - datasource: - url: jdbc:mysql://localhost:3306/test - username: test - password: 1234 - driver-class-name: com.mysql.cj.jdbc.Driver From ad0baa3f94ecbe976aff2220b73bfc3d946451fe Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Mon, 30 Oct 2023 21:59:32 +0900 Subject: [PATCH 48/62] =?UTF-8?q?Refactor:=20=EC=A7=81=EA=B4=80=EC=A0=81?= =?UTF-8?q?=EC=9D=B8=20=EC=9D=B4=EB=A6=84=EC=9C=BC=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/console/WalletConsole.java | 4 ++-- .../springbootbasic/controller/WalletController.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java index 83730e15cf..75838c6864 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java @@ -10,8 +10,8 @@ public static String readCommand(){ System.out.println("Welcome to our wallet service."); System.out.println("Type 'allocate' if you want to allocate voucher to a customer." ); System.out.println("Type 'delete' if you want to delete voucher from a customer."); - System.out.println("Type 'showVoucher' to view customers with a voucher."); - System.out.println("Type 'showCustomer' to view vouchers that a customer has."); + System.out.println("Type 'showVoucherByCustomer' to view customers with a voucher."); + System.out.println("Type 'showCustomerByVoucher' to view vouchers that a customer has."); return consoleInput.next(); } diff --git a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java index dbc92ce746..ee9385cd2f 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java @@ -14,8 +14,8 @@ public class WalletController { private final String ALLOCATE = "allocate"; private final String DELETE = "delete"; - private final String SHOW_CUSTOMER = "showCustomer"; - private final String SHOW_VOUCHER = "showVoucher"; + private final String SHOW_CUSTOMER_BY_VOUCHER = "showVoucherByCustomer"; + private final String SHOW_VOUCHER_BY_CUSTOMER = "showCustomerByVoucher"; private final WalletService walletService; @@ -34,8 +34,8 @@ private void executeCommand(String command){ switch (command){ case ALLOCATE -> allocate(); case DELETE -> delete(); - case SHOW_CUSTOMER -> showCustomer(); - case SHOW_VOUCHER -> showVoucher(); + case SHOW_CUSTOMER_BY_VOUCHER -> showVoucherByCustomer(); + case SHOW_VOUCHER_BY_CUSTOMER -> showCustomerByVoucher(); } } @@ -53,14 +53,14 @@ private void delete(){ walletService.delete(customerId, voucherId); } - private void showCustomer(){ + private void showVoucherByCustomer(){ UUID customerId = WalletConsole.typeCustomerId(); List vouchers = walletService.searchVouchersFromCustomer(customerId); Console.printList(vouchers); } - private void showVoucher(){ + private void showCustomerByVoucher(){ UUID voucherId = WalletConsole.typeVoucherId(); List customers = walletService.searchCustomerFromVoucher(voucherId); From 97b54cf57be9f12957b62d5d5e4fd20a86a83b27 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Tue, 31 Oct 2023 10:36:14 +0900 Subject: [PATCH 49/62] =?UTF-8?q?Refactor:=20deleteById=EA=B0=80=20void=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=EA=B0=92=EC=9D=84=20=EA=B0=80=EC=A7=80?= =?UTF-8?q?=EA=B2=8C=20=ED=95=98=EA=B3=A0=20save=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EB=AA=85=EC=9D=84=20=EC=A7=81=EA=B4=80=EC=A0=81?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B0=94=EA=BE=BC=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../customer/CustomerDatabaseRepository.java | 5 ++--- .../customer/CustomerFileRepository.java | 6 +++--- .../customer/CustomerMemoryRepository.java | 6 +++--- .../customer/CustomerRepository.java | 4 ++-- .../voucher/VoucherCsvFileRepository.java | 14 ++++++++----- .../voucher/VoucherDatabaseRepository.java | 4 ++-- .../voucher/VoucherMemoryRepository.java | 20 +++++++++++-------- .../repository/voucher/VoucherRepository.java | 4 ++-- .../service/VoucherService.java | 2 +- .../CustomerDatabaseRepositoryTest.java | 12 +++++------ .../voucher/VoucherCsvFileRepositoryTest.java | 14 ++++++------- .../VoucherDatabaseRepositoryTest.java | 14 ++++++------- .../voucher/VoucherMemoryRepositoryTest.java | 18 ++++++++--------- 13 files changed, 64 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java index 715ba08a77..fc5ed40479 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java @@ -23,7 +23,7 @@ public CustomerDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { } @Override - public Customer save(Customer customer) { + public Customer upsert(Customer customer) { Optional foundCustomer = findById(customer.getCustomerId()); if (foundCustomer.isPresent()){ @@ -74,12 +74,11 @@ public List findBlackAll() { } @Override - public Optional deleteById(UUID customerId) { + public void deleteById(UUID customerId) { Optional deleteCustomer = findById(customerId); jdbcTemplate.update("DELETE FROM customers WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", Collections.singletonMap("customerId", customerId.toString().getBytes())); - return deleteCustomer; } @Override diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java index 37f289d504..6595734bf2 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerFileRepository.java @@ -19,7 +19,7 @@ public CustomerFileRepository(CustomerCsvFileManager customerCsvFileManager) { } @Override - public Customer save(Customer customer) { + public Customer upsert(Customer customer) { return null; } @@ -44,8 +44,8 @@ public List findBlackAll() { } @Override - public Optional deleteById(UUID customerId) { - return Optional.empty(); + public void deleteById(UUID customerId) { + } @Override diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java index 2fd62af8fc..580d1bd51f 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerMemoryRepository.java @@ -13,7 +13,7 @@ @Profile({"local"}) public class CustomerMemoryRepository implements CustomerRepository { // 추후 구현 @Override - public Customer save(Customer customer) { + public Customer upsert(Customer customer) { return null; } @@ -38,8 +38,8 @@ public List findBlackAll() { } @Override - public Optional deleteById(UUID customerId) { - return Optional.empty(); + public void deleteById(UUID customerId) { + } @Override diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java index fcb475c5de..f01227e877 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerRepository.java @@ -7,11 +7,11 @@ import java.util.UUID; public interface CustomerRepository { - Customer save(Customer customer); + Customer upsert(Customer customer); Optional findById(UUID customerId); Optional findByEmail(String email); List findAll(); List findBlackAll(); - Optional deleteById(UUID customerId); + void deleteById(UUID customerId); int deleteAll(); } diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java index 4b725ecf45..5603895ba2 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java @@ -40,14 +40,18 @@ public List findAll() { } @Override - public VoucherPolicy save(VoucherPolicy voucherPolicy) { - vouchers.putIfAbsent(voucherPolicy.getVoucherId(), voucherPolicy); + public VoucherPolicy upsert(VoucherPolicy voucherPolicy) { + if (Optional.ofNullable(vouchers.get(voucherPolicy.getVoucherId())).isPresent()) { + vouchers.replace(voucherPolicy.getVoucherId(), voucherPolicy); + } else { + vouchers.put(voucherPolicy.getVoucherId(), voucherPolicy); + } return voucherPolicy; } @Override - public Optional deleteById(UUID voucherId) { - return Optional.ofNullable(vouchers.remove(voucherId)); + public void deleteById(UUID voucherId) { + vouchers.remove(voucherId); } @Override @@ -58,7 +62,7 @@ public void deleteAll() { @PostConstruct private void fileRead(){ List voucherPolicies = voucherCsvFileManager.read(); - voucherPolicies.forEach(this::save); + voucherPolicies.forEach(this::upsert); } @PreDestroy diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java index e039b49623..66e3f63004 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java @@ -24,7 +24,7 @@ public VoucherDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { } @Override - public VoucherPolicy save(VoucherPolicy voucher) { + public VoucherPolicy upsert(VoucherPolicy voucher) { Optional foundVoucher = findById(voucher.getVoucherId()); if (foundVoucher.isPresent()){ @@ -58,7 +58,7 @@ public List findAll() { } @Override - public Optional deleteById(UUID voucherId) { + public void deleteById(UUID voucherId) { Optional deleteVoucher = findById(voucherId); jdbcTemplate.update("DELETE FROM vouchers WHERE voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java index 18504a93f6..e5094612b3 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java @@ -15,31 +15,35 @@ @Profile({"local"}) @Slf4j public class VoucherMemoryRepository implements VoucherRepository{ - private final ConcurrentHashMap mem = new ConcurrentHashMap<>(); + private final ConcurrentHashMap vouchers = new ConcurrentHashMap<>(); @Override public Optional findById(UUID voucherId) { - return Optional.ofNullable(mem.get(voucherId)); + return Optional.ofNullable(vouchers.get(voucherId)); } @Override public List findAll() { - return new ArrayList<>(mem.values()); + return new ArrayList<>(vouchers.values()); } @Override - public VoucherPolicy save(VoucherPolicy voucherPolicy) { - mem.putIfAbsent(voucherPolicy.getVoucherId(), voucherPolicy); + public VoucherPolicy upsert(VoucherPolicy voucherPolicy) { + if (Optional.ofNullable(vouchers.get(voucherPolicy.getVoucherId())).isPresent()) { + vouchers.replace(voucherPolicy.getVoucherId(), voucherPolicy); + } else { + vouchers.put(voucherPolicy.getVoucherId(), voucherPolicy); + } return voucherPolicy; } @Override - public Optional deleteById(UUID voucherId) { - return Optional.ofNullable(mem.remove(voucherId)); + public void deleteById(UUID voucherId) { + vouchers.remove(voucherId); } @Override public void deleteAll() { - mem.clear(); + vouchers.clear(); } } diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java index be1c441a96..0b4bf8e3cf 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java @@ -9,7 +9,7 @@ public interface VoucherRepository { Optional findById(UUID voucherId); List findAll(); - VoucherPolicy save(VoucherPolicy voucherPolicy); - Optional deleteById(UUID voucherId); + VoucherPolicy upsert(VoucherPolicy voucherPolicy); + void deleteById(UUID voucherId); void deleteAll(); } diff --git a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java index ea7df34c45..336ccafdf7 100644 --- a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java +++ b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java @@ -24,7 +24,7 @@ public VoucherType seqToType(int voucherSeq) { public void create(VoucherType voucherType, int discountDegree) { VoucherPolicy voucherPolicy = voucherType.create(discountDegree); - voucherRepository.save(voucherPolicy); + voucherRepository.upsert(voucherPolicy); } public List findAll(){ diff --git a/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java index 9f7f0cf3ee..91dbf9d532 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java @@ -33,7 +33,7 @@ void setUp() { "test@gmail.com", LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS)); - customerRepository.save(setUpCustomer); + customerRepository.upsert(setUpCustomer); } @AfterEach @@ -48,7 +48,7 @@ void insertNewCustomerInDB() { "test2@gmail.com", LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS)); - customerRepository.save(customer); + customerRepository.upsert(customer); Optional retrievedCustomer = customerRepository.findById(customer.getCustomerId()); assertThat(retrievedCustomer.isPresent(), is(true)); @@ -61,7 +61,7 @@ void updateExistingCustomerInDB() { Customer changedCustomer = customer.changeInfo("updated", customer.isBlacked()); - customerRepository.save(changedCustomer); + customerRepository.upsert(changedCustomer); Optional retrievedCustomer = customerRepository.findById(setUpCustomer.getCustomerId()); @@ -91,7 +91,7 @@ void findCustomerByEmailInDB() { @Test void findAllCustomersInDB(){ - customerRepository.save(new Customer(UUID.randomUUID(), "test2", "test2@gmail.com", LocalDateTime.now())); + customerRepository.upsert(new Customer(UUID.randomUUID(), "test2", "test2@gmail.com", LocalDateTime.now())); List customers = customerRepository.findAll(); @@ -106,7 +106,7 @@ void findAllBlackedCustomersInDB() { LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS)); blackCustomer.changeInfo(blackCustomer.getName(), true); - customerRepository.save(blackCustomer); + customerRepository.upsert(blackCustomer); List blackList = customerRepository.findBlackAll(); @@ -124,7 +124,7 @@ void deleteCustomerByCustomerIdInDB() { @Test void deleteAllCustomersInDB() { - customerRepository.save(new Customer(UUID.randomUUID(), "test2", "test2@gmail.com", LocalDateTime.now())); + customerRepository.upsert(new Customer(UUID.randomUUID(), "test2", "test2@gmail.com", LocalDateTime.now())); customerRepository.deleteAll(); diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java index 14b5f04a51..7b785c547f 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java @@ -31,7 +31,7 @@ void cleanUp(){ void findVoucherByIdFromFile() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); - voucherCsvFileRepository.save(fixedAmountVoucher); + voucherCsvFileRepository.upsert(fixedAmountVoucher); Optional retrievedVoucher = voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()); @@ -44,8 +44,8 @@ void findAllVoucherFromFile() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 10); - voucherCsvFileRepository.save(fixedAmountVoucher); - voucherCsvFileRepository.save(percentDiscountVoucher); + voucherCsvFileRepository.upsert(fixedAmountVoucher); + voucherCsvFileRepository.upsert(percentDiscountVoucher); assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); @@ -57,8 +57,8 @@ void createVoucherToFile() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 2000); PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); - voucherCsvFileRepository.save(fixedAmountVoucher); - voucherCsvFileRepository.save(percentDiscountVoucher); + voucherCsvFileRepository.upsert(fixedAmountVoucher); + voucherCsvFileRepository.upsert(percentDiscountVoucher); assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); @@ -71,7 +71,7 @@ void createVoucherToFile() { void deleteVoucherById() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); - voucherCsvFileRepository.save(fixedAmountVoucher); + voucherCsvFileRepository.upsert(fixedAmountVoucher); assertThat(voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()).isPresent(), is(true)); @@ -84,7 +84,7 @@ void deleteVoucherById() { @Test void deleteAllVoucher() { - voucherCsvFileRepository.save(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + voucherCsvFileRepository.upsert(new FixedAmountVoucher(UUID.randomUUID(), 1000)); voucherCsvFileRepository.deleteAll(); diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java index 049b981a6b..fb232fc33d 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java @@ -28,7 +28,7 @@ class VoucherDatabaseRepositoryTest { @BeforeEach void setUp() { setUpVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 40); - voucherDatabaseRepository.save(setUpVoucher); + voucherDatabaseRepository.upsert(setUpVoucher); } @AfterEach @@ -40,7 +40,7 @@ void clean() { void saveNewVoucherToDB() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); - voucherDatabaseRepository.save(fixedAmountVoucher); + voucherDatabaseRepository.upsert(fixedAmountVoucher); Optional retrievedVoucher = voucherDatabaseRepository.findById(fixedAmountVoucher.getVoucherId()); @@ -52,7 +52,7 @@ void saveNewVoucherToDB() { void updateVoucherInDB() { FixedAmountVoucher updateVoucher = new FixedAmountVoucher(setUpVoucher.getVoucherId(), 2000); - voucherDatabaseRepository.save(updateVoucher); + voucherDatabaseRepository.upsert(updateVoucher); Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); @@ -72,7 +72,7 @@ void findVoucherByIdInDB() { void findAllVouchersInDB() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 3000); - voucherDatabaseRepository.save(fixedAmountVoucher); + voucherDatabaseRepository.upsert(fixedAmountVoucher); List vouchers = voucherDatabaseRepository.findAll(); @@ -83,12 +83,10 @@ void findAllVouchersInDB() { @Test void deleteVoucherByIdInDB() { - Optional deleteVoucher = voucherDatabaseRepository.deleteById(setUpVoucher.getVoucherId()); + voucherDatabaseRepository.deleteById(setUpVoucher.getVoucherId()); List vouchers = voucherDatabaseRepository.findAll(); - assertThat(deleteVoucher.isPresent(), is(true)); - assertThat(deleteVoucher.get(), samePropertyValuesAs(setUpVoucher)); assertThat(vouchers, hasSize(0)); } @@ -96,7 +94,7 @@ void deleteVoucherByIdInDB() { void deleteAllVouchersInDB() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 3000); - voucherDatabaseRepository.save(fixedAmountVoucher); + voucherDatabaseRepository.upsert(fixedAmountVoucher); voucherDatabaseRepository.deleteAll(); List vouchers = voucherDatabaseRepository.findAll(); diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java index 06c8916214..882e00a4de 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java @@ -33,7 +33,7 @@ void findVoucherByIdFromMemory() { UUID voucherId = UUID.randomUUID(); FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(voucherId, 1000); - voucherMemoryRepository.save(fixedAmountVoucher); + voucherMemoryRepository.upsert(fixedAmountVoucher); Optional retrievedVoucher = voucherMemoryRepository.findById(voucherId); Optional randomRetrievedVoucher = voucherMemoryRepository.findById(UUID.randomUUID()); @@ -48,8 +48,8 @@ void findAllVoucherFromMemory() { PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 6000); - voucherMemoryRepository.save(percentDiscountVoucher); - voucherMemoryRepository.save(fixedAmountVoucher); + voucherMemoryRepository.upsert(percentDiscountVoucher); + voucherMemoryRepository.upsert(fixedAmountVoucher); assertThat(voucherMemoryRepository.findAll(), hasSize(2)); assertThat(voucherMemoryRepository.findAll(), hasItem(samePropertyValuesAs(percentDiscountVoucher))); @@ -61,19 +61,19 @@ void createNewVouchersToMemory() { FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 30); - this.voucherMemoryRepository.save(fixedAmountVoucher); - this.voucherMemoryRepository.save(percentDiscountVoucher); + this.voucherMemoryRepository.upsert(fixedAmountVoucher); + this.voucherMemoryRepository.upsert(percentDiscountVoucher); assertThat(voucherMemoryRepository.findAll(), hasSize(2)); assertThrows(IllegalArgumentException.class, - () -> voucherMemoryRepository.save(new PercentDiscountVoucher(UUID.randomUUID(), 0))); + () -> voucherMemoryRepository.upsert(new PercentDiscountVoucher(UUID.randomUUID(), 0))); } @Test void deleteAllVouchersFromMemory() { - voucherMemoryRepository.save(new PercentDiscountVoucher(UUID.randomUUID(), 10)); - voucherMemoryRepository.save(new PercentDiscountVoucher(UUID.randomUUID(), 20)); - voucherMemoryRepository.save(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + voucherMemoryRepository.upsert(new PercentDiscountVoucher(UUID.randomUUID(), 10)); + voucherMemoryRepository.upsert(new PercentDiscountVoucher(UUID.randomUUID(), 20)); + voucherMemoryRepository.upsert(new FixedAmountVoucher(UUID.randomUUID(), 1000)); voucherMemoryRepository.deleteAll(); From f5fda67d8faed1d051a12dedd1f92c19be611e6f Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Tue, 31 Oct 2023 10:54:15 +0900 Subject: [PATCH 50/62] =?UTF-8?q?Refactor:=20=ED=95=98=EB=82=98=EC=9D=98?= =?UTF-8?q?=20Console,=20=ED=95=98=EB=82=98=EC=9D=98=20Controller=EB=A1=9C?= =?UTF-8?q?=20=ED=86=B5=ED=95=A9=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/console/Console.java | 83 +++++++++++++++++- .../common/console/MainConsole.java | 57 ------------- .../common/console/WalletConsole.java | 35 -------- .../controller/MainController.java | 85 +++++++++++++++---- .../controller/WalletController.java | 69 --------------- .../customer/CustomerDatabaseRepository.java | 2 - .../voucher/VoucherDatabaseRepository.java | 5 +- 7 files changed, 153 insertions(+), 183 deletions(-) delete mode 100644 src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java delete mode 100644 src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java delete mode 100644 src/main/java/org/prgms/springbootbasic/controller/WalletController.java diff --git a/src/main/java/org/prgms/springbootbasic/common/console/Console.java b/src/main/java/org/prgms/springbootbasic/common/console/Console.java index 2caf1062b4..91611c8f86 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/Console.java @@ -1,11 +1,92 @@ package org.prgms.springbootbasic.common.console; +import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.domain.VoucherType; +import org.springframework.stereotype.Component; + +import java.text.MessageFormat; import java.util.Collection; import java.util.Scanner; +import java.util.UUID; + +import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; +import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; -public class Console { +@Component +@Slf4j +public class Console { // Console이 common인가? MVC 생각하며 고민하자. View에 해당한다고 보이는데... MVC가 뭔지 대답 못한 것을 두번째 걸렸다. 공부하자... public static final Scanner consoleInput = new Scanner(System.in); + public static String readCommand() { + System.out.println("=== Voucher Program ==="); + System.out.println("Type 'exit' to exit the program."); + System.out.println("Type 'create' to create a new voucher."); + System.out.println("Type 'list' to list all vouchers."); + System.out.println("Type 'black' to list customers blacked."); + System.out.println("Type 'wallet' to enter wallet service."); + + return consoleInput.next(); + } + + public static int selectCreateType() { + System.out.println(); + System.out.println("Which voucher would you like to create? Just type number."); + System.out.println(MessageFormat.format("{0}. FixedAmountVoucher", INPUT_FIXED_AMOUNT_VOUCHER)); + System.out.println(MessageFormat.format("{0}. PercentDiscountVoucher", INPUT_PERCENT_DISCOUNT_VOUCHER)); + + return consoleInput.nextInt(); + } + + public static int putDiscountDegree(VoucherType type) { + switch (type){ + case FIXED_AMOUNT -> System.out.println("Enter the fixed discount amount."); + case PERCENT_DISCOUNT -> System.out.println("Enter the discount percentage."); + } + return consoleInput.nextInt(); + } + + + public static String ignoreLine() { + return consoleInput.nextLine(); + } + + public static void printArgException() { + System.out.println("Invalid argument. Type command again."); + } + + public static void printRuntimeException() { + System.out.println("Invalid input or system error. redirect to beginning."); + } + + public static String readWalletCommand(){ // MVC 제대로 알아옴!! Controller 중재. 뷰와 모델은 컨트롤러 모름. 콘솔이 도메인(Wallet)을 알고 있다. 수정. Console이 두개일 이유도 없다. + System.out.println(); + System.out.println("Welcome to our wallet service."); + System.out.println("Type 'allocate' if you want to allocate voucher to a customer." ); + System.out.println("Type 'delete' if you want to delete voucher from a customer."); + System.out.println("Type 'showVoucherByCustomer' to view customers with a voucher."); + System.out.println("Type 'showCustomerByVoucher' to view vouchers that a customer has."); + System.out.println("Type 'back' to go back."); + + return consoleInput.next(); + } + + public static UUID typeCustomerId(){ + System.out.println("Type customer Id."); + + return UUID.fromString(consoleInput.next()); + } + + public static UUID typeVoucherId(){ + System.out.println("Type voucher Id."); + + return UUID.fromString(consoleInput.next()); + } + + public static void success(String command){ + System.out.println("'" + command + "' command successfully executed."); + System.out.println(); + } + public static void printList(Collection collection) { System.out.println(); collection.forEach(System.out::println); diff --git a/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java deleted file mode 100644 index 45d0a463c2..0000000000 --- a/src/main/java/org/prgms/springbootbasic/common/console/MainConsole.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.prgms.springbootbasic.common.console; - -import lombok.extern.slf4j.Slf4j; -import org.prgms.springbootbasic.domain.VoucherType; -import org.springframework.stereotype.Component; - -import java.text.MessageFormat; - -import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; -import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; -import static org.prgms.springbootbasic.common.console.Console.consoleInput; - -@Component -@Slf4j -public class MainConsole { - - public static String readCommand() { - System.out.println("=== Voucher Program ==="); - System.out.println("Type 'exit' to exit the program."); - System.out.println("Type 'create' to create a new voucher."); - System.out.println("Type 'list' to list all vouchers."); - System.out.println("Type 'black' to list customers blacked."); - System.out.println("Type 'wallet' to enter wallet service."); - - return consoleInput.next(); - } - - public static int selectCreateType() { - System.out.println(); - System.out.println("Which voucher would you like to create? Just type number."); - System.out.println(MessageFormat.format("{0}. FixedAmountVoucher", INPUT_FIXED_AMOUNT_VOUCHER)); - System.out.println(MessageFormat.format("{0}. PercentDiscountVoucher", INPUT_PERCENT_DISCOUNT_VOUCHER)); - - return consoleInput.nextInt(); - } - - public static int putDiscountDegree(VoucherType type) { - switch (type){ - case FIXED_AMOUNT -> System.out.println("Enter the fixed discount amount."); - case PERCENT_DISCOUNT -> System.out.println("Enter the discount percentage."); - } - return consoleInput.nextInt(); - } - - - public static String ignoreLine() { - return consoleInput.nextLine(); - } - - public static void printArgException() { - System.out.println("Invalid argument. Type command again."); - } - - public static void printRuntimeException() { - System.out.println("Invalid input or system error. redirect to beginning."); - } -} diff --git a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java b/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java deleted file mode 100644 index 75838c6864..0000000000 --- a/src/main/java/org/prgms/springbootbasic/common/console/WalletConsole.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.prgms.springbootbasic.common.console; - -import java.util.UUID; - -import static org.prgms.springbootbasic.common.console.Console.consoleInput; - -public class WalletConsole { - public static String readCommand(){ - System.out.println(); - System.out.println("Welcome to our wallet service."); - System.out.println("Type 'allocate' if you want to allocate voucher to a customer." ); - System.out.println("Type 'delete' if you want to delete voucher from a customer."); - System.out.println("Type 'showVoucherByCustomer' to view customers with a voucher."); - System.out.println("Type 'showCustomerByVoucher' to view vouchers that a customer has."); - - return consoleInput.next(); - } - - public static UUID typeCustomerId(){ - System.out.println("Type customer Id."); - - return UUID.fromString(consoleInput.next()); - } - - public static UUID typeVoucherId(){ - System.out.println("Type voucher Id."); - - return UUID.fromString(consoleInput.next()); - } - - public static void success(String command){ - System.out.println("'" + command + "' command successfully executed."); - System.out.println(); - } -} diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index a6cc8ee696..1b9f5998bf 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -2,18 +2,22 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.common.console.Console; -import org.prgms.springbootbasic.common.console.MainConsole; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.customer.Customer; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.prgms.springbootbasic.service.CustomerService; import org.prgms.springbootbasic.service.VoucherService; +import org.prgms.springbootbasic.service.WalletService; import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Controller; import java.util.InputMismatchException; import java.util.List; import java.util.NoSuchElementException; +import java.util.UUID; + +import static org.prgms.springbootbasic.common.console.Console.*; +import static org.prgms.springbootbasic.common.console.Console.typeVoucherId; @Controller @Slf4j @@ -23,26 +27,31 @@ public class MainController { private static final String CREATE = "create"; private static final String BLACK = "black"; private static final String WALLET = "wallet"; + private static final String ALLOCATE = "allocate"; + private static final String DELETE = "delete"; + private static final String SHOW_CUSTOMER_BY_VOUCHER = "showVoucherByCustomer"; + private static final String SHOW_VOUCHER_BY_CUSTOMER = "showCustomerByVoucher"; + private static final String BACK = "back"; private final VoucherService voucherService; private final CustomerService customerService; - private final WalletController walletController; + private final WalletService walletService; - public MainController(VoucherService voucherService, CustomerService customerService, WalletController walletController) { + public MainController(VoucherService voucherService, CustomerService customerService, WalletService walletService) { this.voucherService = voucherService; this.customerService = customerService; - this.walletController = walletController; + this.walletService = walletService; } public void run() { String command = ""; while (!command.equals(EXIT)) { try { - command = MainConsole.readCommand(); + command = Console.readCommand(); executeCommand(command); } catch (InputMismatchException e) { - String invalidVal = MainConsole.ignoreLine(); + String invalidVal = Console.ignoreLine(); log.warn("User input = {}", invalidVal); throw new IllegalArgumentException("Not integer."); @@ -53,22 +62,22 @@ public void run() { log.error("Scanner is closed"); throw new RuntimeException("Scanner is closed."); } catch (IllegalArgumentException e) { - MainConsole.printArgException(); + Console.printArgException(); } catch (DataAccessException e) { log.error("Database error.", e); - MainConsole.printRuntimeException(); + Console.printRuntimeException(); } catch (RuntimeException e) { - MainConsole.printRuntimeException(); + Console.printRuntimeException(); } } - } + } // 프론트 컨트롤러 패턴에 대해. 공부해보자. 컨트롤러 일관성 없음. 왜 wallet만 따로 존재하는? 고민... -> 그냥 통일합시다. private void executeCommand(String command) { switch (command) { case CREATE -> create(); case LIST -> list(); case BLACK -> black(); - case WALLET -> wallet(); + case WALLET -> runWallet(); case EXIT -> {} default -> { log.warn("invalid command. now command = {}", command); @@ -78,11 +87,11 @@ private void executeCommand(String command) { } private void create(){ - int voucherSeq = MainConsole.selectCreateType(); + int voucherSeq = Console.selectCreateType(); VoucherType voucherType = voucherService.seqToType(voucherSeq); - int discountDegree = MainConsole.putDiscountDegree(voucherType); + int discountDegree = Console.putDiscountDegree(voucherType); voucherService.create(voucherType, discountDegree); } @@ -99,7 +108,53 @@ private void black(){ Console.printList(blacklist); } - private void wallet(){ - walletController.run(); + + private void runWallet(){ + String command = readWalletCommand(); + + executeWalletCommand(command); + success(command); + } + + private void executeWalletCommand(String command){ + switch (command){ + case ALLOCATE -> allocate(); + case DELETE -> delete(); + case SHOW_CUSTOMER_BY_VOUCHER -> showVoucherByCustomer(); + case SHOW_VOUCHER_BY_CUSTOMER -> showCustomerByVoucher(); + case BACK -> {} + default -> { + log.warn("invalid command. now command = {}", command); + throw new IllegalArgumentException("Invalid command. Type command again."); + } + } + } + + private void allocate(){ + UUID customerId = typeCustomerId(); + UUID voucherId = typeVoucherId(); + + walletService.allocate(customerId, voucherId); + } + + private void delete(){ + UUID customerId = typeCustomerId(); + UUID voucherId = typeVoucherId(); + + walletService.delete(customerId, voucherId); + } + + private void showVoucherByCustomer(){ + UUID customerId = typeCustomerId(); + List vouchers = walletService.searchVouchersFromCustomer(customerId); + + Console.printList(vouchers); + } + + private void showCustomerByVoucher(){ + UUID voucherId = typeVoucherId(); + List customers = walletService.searchCustomerFromVoucher(voucherId); + + Console.printList(customers); } } diff --git a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java b/src/main/java/org/prgms/springbootbasic/controller/WalletController.java deleted file mode 100644 index ee9385cd2f..0000000000 --- a/src/main/java/org/prgms/springbootbasic/controller/WalletController.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.prgms.springbootbasic.controller; - -import org.prgms.springbootbasic.common.console.Console; -import org.prgms.springbootbasic.common.console.WalletConsole; -import org.prgms.springbootbasic.domain.customer.Customer; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; -import org.prgms.springbootbasic.service.WalletService; -import org.springframework.stereotype.Controller; - -import java.util.List; -import java.util.UUID; - -@Controller -public class WalletController { - private final String ALLOCATE = "allocate"; - private final String DELETE = "delete"; - private final String SHOW_CUSTOMER_BY_VOUCHER = "showVoucherByCustomer"; - private final String SHOW_VOUCHER_BY_CUSTOMER = "showCustomerByVoucher"; - - private final WalletService walletService; - - public WalletController(WalletService walletService) { - this.walletService = walletService; - } - - public void run(){ - String command = WalletConsole.readCommand(); - - executeCommand(command); - WalletConsole.success(command); - } - - private void executeCommand(String command){ - switch (command){ - case ALLOCATE -> allocate(); - case DELETE -> delete(); - case SHOW_CUSTOMER_BY_VOUCHER -> showVoucherByCustomer(); - case SHOW_VOUCHER_BY_CUSTOMER -> showCustomerByVoucher(); - } - } - - private void allocate(){ - UUID customerId = WalletConsole.typeCustomerId(); - UUID voucherId = WalletConsole.typeVoucherId(); - - walletService.allocate(customerId, voucherId); - } - - private void delete(){ - UUID customerId = WalletConsole.typeCustomerId(); - UUID voucherId = WalletConsole.typeVoucherId(); - - walletService.delete(customerId, voucherId); - } - - private void showVoucherByCustomer(){ - UUID customerId = WalletConsole.typeCustomerId(); - List vouchers = walletService.searchVouchersFromCustomer(customerId); - - Console.printList(vouchers); - } - - private void showCustomerByVoucher(){ - UUID voucherId = WalletConsole.typeVoucherId(); - List customers = walletService.searchCustomerFromVoucher(voucherId); - - Console.printList(customers); - } -} diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java index fc5ed40479..9d40d2266a 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java @@ -75,8 +75,6 @@ public List findBlackAll() { @Override public void deleteById(UUID customerId) { - Optional deleteCustomer = findById(customerId); - jdbcTemplate.update("DELETE FROM customers WHERE customer_id = UNHEX(REPLACE(:customerId, '-', ''))", Collections.singletonMap("customerId", customerId.toString().getBytes())); } diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java index 66e3f63004..f39bce2eba 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java @@ -59,11 +59,8 @@ public List findAll() { @Override public void deleteById(UUID voucherId) { - Optional deleteVoucher = findById(voucherId); - jdbcTemplate.update("DELETE FROM vouchers WHERE voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", - Collections.singletonMap("voucherId", voucherId.toString().getBytes())); - return deleteVoucher; + Collections.singletonMap("voucherId", voucherId.toString().getBytes())); // 예외 명시적으로 던지기. update가 던져주는 예외는 이해하기 어려울 수도. } @Override From eb7da2e95de496df585062f9410567082d281428 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Tue, 31 Oct 2023 18:36:39 +0900 Subject: [PATCH 51/62] =?UTF-8?q?Feat:=20Voucher=EB=A5=BC=20=EC=83=88?= =?UTF-8?q?=EB=A1=9C=20=EC=A0=95=EC=9D=98=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/voucher/FixedAmountVoucher.java | 24 +----------- .../voucher/PercentDiscountVoucher.java | 33 +++------------- .../domain/voucher/Voucher.java | 38 +++++++++++++++++++ .../domain/voucher/VoucherPolicy.java | 6 +-- 4 files changed, 46 insertions(+), 55 deletions(-) create mode 100644 src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java index aa71ce3f89..07314a0f28 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java @@ -3,36 +3,16 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.exception.OutOfRangeException; -import java.util.UUID; - import static java.lang.Math.max; @Slf4j public class FixedAmountVoucher implements VoucherPolicy { - private final UUID voucherId; - private final long amount; - - public FixedAmountVoucher(UUID voucherId, long amount) { - this.voucherId = voucherId; - this.amount = amount; - } - - @Override - public UUID getVoucherId() { - return this.voucherId; - } - - @Override - public long getDiscountDegree() { - return this.amount; - } - @Override - public long discount(long beforeDiscount) { + public long discount(long beforeDiscount, long discountDegree) { if (beforeDiscount < 0) { throw new OutOfRangeException("beforeDiscount is less than 0."); } - return max(0, beforeDiscount - this.amount); + return max(0, beforeDiscount - discountDegree); } } diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java index d0916a32eb..c6ad594649 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java @@ -3,37 +3,14 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.exception.OutOfRangeException; -import java.util.UUID; - @Slf4j public class PercentDiscountVoucher implements VoucherPolicy { - private final UUID voucherId; - private final long percent; - - public PercentDiscountVoucher(UUID voucherId, long percent) { - if (percent > 100 || percent <= 0) { - log.error("percent value is out of range."); - throw new OutOfRangeException("percent value is out of range."); - } - - this.voucherId = voucherId; - this.percent = percent; - } - - @Override - public UUID getVoucherId() { - return this.voucherId; - } - @Override - public long getDiscountDegree() { - return this.percent; - } - - @Override - public long discount(long beforeDiscount) { // 음수 고려 - if (beforeDiscount < 0) + public long discount(long beforeDiscount, long discountDegree) { + if (beforeDiscount < 0) { throw new OutOfRangeException("beforeDiscount is less than 0."); - return beforeDiscount * percent / 100L; + } + + return beforeDiscount * discountDegree / 100L; } } diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java new file mode 100644 index 0000000000..dbf5c91c3c --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java @@ -0,0 +1,38 @@ +package org.prgms.springbootbasic.domain.voucher; + +import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.exception.OutOfRangeException; + +import java.util.UUID; + +@Slf4j +public class Voucher { + private final UUID voucherId; + private final long discountDegree; + private final VoucherPolicy voucherPolicy; + + public Voucher(UUID voucherId, long discountDegree, VoucherPolicy voucherPolicy) { + if (voucherPolicy instanceof PercentDiscountVoucher) { + if (discountDegree <= 0 || discountDegree > 100) { + log.error("percent value is out of range."); + throw new OutOfRangeException("percent value is out of range."); + } + } + + this.voucherId = voucherId; + this.discountDegree = discountDegree; + this.voucherPolicy = voucherPolicy; + } + + public UUID getVoucherId() { + return voucherId; + } + + public long getDiscountDegree() { + return discountDegree; + } + + public long discount(long beforeDiscount){ + return voucherPolicy.discount(beforeDiscount, this.discountDegree); + } +} diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java index a969b2b3ee..72e073810d 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/VoucherPolicy.java @@ -1,9 +1,5 @@ package org.prgms.springbootbasic.domain.voucher; -import java.util.UUID; - public interface VoucherPolicy { - UUID getVoucherId(); - long discount(long beforeDiscount); - long getDiscountDegree(); + long discount(long beforeDiscount, long discountDegree); } From 4b64bf1684b838f4dff13ce40507682d6d7a368d Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Wed, 1 Nov 2023 09:24:15 +0900 Subject: [PATCH 52/62] =?UTF-8?q?Refactor:=20=EC=A0=95=EC=9D=98=EB=90=9C?= =?UTF-8?q?=20Voucher=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EA=B8=B0=EC=A1=B4?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=EB=A5=BC=20VoucherPolicy=EC=97=90?= =?UTF-8?q?=EC=84=9C=20Voucher=EB=A1=9C=20=EC=B2=98=EB=A6=AC=ED=95=98?= =?UTF-8?q?=EA=B2=8C=20=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/console/Console.java | 2 +- .../common/file/VoucherCsvFileManager.java | 24 ++++--- .../controller/MainController.java | 7 +- .../springbootbasic/domain/VoucherType.java | 25 +++---- ...untVoucher.java => FixedAmountPolicy.java} | 2 +- ...oucher.java => PercentDiscountPolicy.java} | 2 +- .../voucher/VoucherCsvFileRepository.java | 19 +++--- .../voucher/VoucherDatabaseRepository.java | 20 +++--- .../voucher/VoucherMemoryRepository.java | 17 ++--- .../repository/voucher/VoucherRepository.java | 7 +- .../wallet/WalletDatabaseRepository.java | 11 ++- .../repository/wallet/WalletRepository.java | 3 +- .../service/VoucherService.java | 9 ++- .../service/WalletService.java | 2 +- .../voucher/VoucherCsvFileRepositoryTest.java | 68 +++++++++++-------- .../VoucherDatabaseRepositoryTest.java | 52 ++++++++------ .../voucher/VoucherMemoryRepositoryTest.java | 37 +++++----- 17 files changed, 173 insertions(+), 134 deletions(-) rename src/main/java/org/prgms/springbootbasic/domain/voucher/{FixedAmountVoucher.java => FixedAmountPolicy.java} (88%) rename src/main/java/org/prgms/springbootbasic/domain/voucher/{PercentDiscountVoucher.java => PercentDiscountPolicy.java} (87%) diff --git a/src/main/java/org/prgms/springbootbasic/common/console/Console.java b/src/main/java/org/prgms/springbootbasic/common/console/Console.java index 91611c8f86..81464fcb29 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/Console.java @@ -19,11 +19,11 @@ public class Console { // Console이 common인가? MVC 생각하며 고민하자 public static String readCommand() { System.out.println("=== Voucher Program ==="); - System.out.println("Type 'exit' to exit the program."); System.out.println("Type 'create' to create a new voucher."); System.out.println("Type 'list' to list all vouchers."); System.out.println("Type 'black' to list customers blacked."); System.out.println("Type 'wallet' to enter wallet service."); + System.out.println("Type 'exit' to exit the program."); return consoleInput.next(); } diff --git a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java index f04d613981..eff7509aa8 100644 --- a/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java +++ b/src/main/java/org/prgms/springbootbasic/common/file/VoucherCsvFileManager.java @@ -2,6 +2,7 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.VoucherType; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; @@ -30,15 +31,15 @@ public VoucherCsvFileManager(CsvFileTemplate csvFileTemplate) { this.csvFileTemplate = csvFileTemplate; } - public List read(){ + public List read(){ return csvFileTemplate.read(FILE_PATH, this::convertToVoucher); } - public void write(List voucherPolicies){ + public void write(List voucherPolicies){ csvFileTemplate.write(FILE_PATH, voucherPolicies, this::convertToString, CSV_FIRST_LINE); } - private VoucherPolicy convertToVoucher(String line){ + private Voucher convertToVoucher(String line){ log.debug("line = {}", line); List splitLine = Arrays.stream(line.split(CSV_PATTERN)) @@ -54,18 +55,23 @@ private VoucherPolicy convertToVoucher(String line){ log.error("Invalid voucher type."); return new IllegalArgumentException("Invalid voucher type"); }); - return thisVoucherType.create(UUID.fromString(splitLine.get(UUID_IDX)), - Long.parseLong(splitLine.get(DISCOUNT_DEGREE_IDX))); + + VoucherPolicy voucherPolicy = thisVoucherType.create(); + UUID voucherId = UUID.fromString(splitLine.get(UUID_IDX)); + long discountDegree = Long.parseLong(splitLine.get(DISCOUNT_DEGREE_IDX)); + + return new Voucher(voucherId, discountDegree, voucherPolicy); } - private String convertToString(VoucherPolicy voucherPolicy){ + private String convertToString(Voucher voucher){ // 외부에 도메인을 맞추면 안됨. -> DB 의존적 클래스랑 실제 내부 도메인 분리. + // 이거를 위해서 VoucherPolicy가 getter를 들고있는게 말이 안됨. 얘를 도메인에 맞춰야지 얘때문에 도메인이 망가지면 안된다. StringBuilder sb = new StringBuilder(); - sb.append(voucherPolicy.getVoucherId()); + sb.append(voucher.getVoucherId()); sb.append(","); - sb.append(voucherPolicy.getClass().getSimpleName()); + sb.append(voucher.getVoucherPolicy().getClass().getSimpleName()); sb.append(","); - sb.append(voucherPolicy.getDiscountDegree()); + sb.append(voucher.getDiscountDegree()); sb.append(System.lineSeparator()); return sb.toString(); diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index 1b9f5998bf..2550e0fe02 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -4,6 +4,7 @@ import org.prgms.springbootbasic.common.console.Console; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.prgms.springbootbasic.service.CustomerService; import org.prgms.springbootbasic.service.VoucherService; @@ -97,9 +98,9 @@ private void create(){ } private void list(){ - List voucherPolicies = voucherService.findAll(); + List vouchers = voucherService.findAll(); - Console.printList(voucherPolicies); + Console.printList(vouchers); } private void black(){ @@ -146,7 +147,7 @@ private void delete(){ private void showVoucherByCustomer(){ UUID customerId = typeCustomerId(); - List vouchers = walletService.searchVouchersFromCustomer(customerId); + List vouchers = walletService.searchVouchersFromCustomer(customerId); Console.printList(vouchers); } diff --git a/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java b/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java index f175ffb2cd..af7ffc191d 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java +++ b/src/main/java/org/prgms/springbootbasic/domain/VoucherType.java @@ -2,29 +2,28 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; -import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; +import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; -import java.util.UUID; -import java.util.function.BiFunction; +import java.util.function.Supplier; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_FIXED_AMOUNT_VOUCHER; import static org.prgms.springbootbasic.common.CommonConstant.INPUT_PERCENT_DISCOUNT_VOUCHER; @Slf4j public enum VoucherType { - FIXED_AMOUNT(INPUT_FIXED_AMOUNT_VOUCHER, FixedAmountVoucher::new, "FixedAmountVoucher"), - PERCENT_DISCOUNT(INPUT_PERCENT_DISCOUNT_VOUCHER, PercentDiscountVoucher::new, "PercentDiscountVoucher"); + FIXED_AMOUNT(INPUT_FIXED_AMOUNT_VOUCHER, FixedAmountPolicy::new, "FixedAmountPolicy"), + PERCENT_DISCOUNT(INPUT_PERCENT_DISCOUNT_VOUCHER, PercentDiscountPolicy::new, "PercentDiscountPolicy"); private final int seq; - private final BiFunction biFunction; + private final Supplier createVoucherPolicy; @Getter private final String displayName; - VoucherType(int seq, BiFunction biFunction, String displayName) { + VoucherType(int seq, Supplier createVoucherPolicy, String displayName) { this.seq = seq; - this.biFunction = biFunction; + this.createVoucherPolicy = createVoucherPolicy; this.displayName = displayName; } @@ -39,11 +38,7 @@ public static VoucherType getTypeFromSeq(int seq){ throw new IllegalArgumentException("Invalid seq"); } - public VoucherPolicy create(long discountDegree){ - return this.biFunction.apply(UUID.randomUUID(), discountDegree); - } - - public VoucherPolicy create(UUID uuid, long discountDegree){ - return this.biFunction.apply(uuid, discountDegree); + public VoucherPolicy create(){ + return this.createVoucherPolicy.get(); } } diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountPolicy.java similarity index 88% rename from src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java rename to src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountPolicy.java index 07314a0f28..b1fe39730c 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountVoucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/FixedAmountPolicy.java @@ -6,7 +6,7 @@ import static java.lang.Math.max; @Slf4j -public class FixedAmountVoucher implements VoucherPolicy { +public class FixedAmountPolicy implements VoucherPolicy { @Override public long discount(long beforeDiscount, long discountDegree) { if (beforeDiscount < 0) { diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountPolicy.java similarity index 87% rename from src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java rename to src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountPolicy.java index c6ad594649..542d923e8a 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountVoucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/PercentDiscountPolicy.java @@ -4,7 +4,7 @@ import org.prgms.springbootbasic.exception.OutOfRangeException; @Slf4j -public class PercentDiscountVoucher implements VoucherPolicy { +public class PercentDiscountPolicy implements VoucherPolicy { @Override public long discount(long beforeDiscount, long discountDegree) { if (beforeDiscount < 0) { diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java index 5603895ba2..441485b3e5 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java @@ -2,6 +2,7 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.common.file.VoucherCsvFileManager; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Profile; @@ -20,7 +21,7 @@ @Primary @Slf4j public class VoucherCsvFileRepository implements VoucherRepository { - private final ConcurrentHashMap vouchers = new ConcurrentHashMap<>(); + private final ConcurrentHashMap vouchers = new ConcurrentHashMap<>(); private final VoucherCsvFileManager voucherCsvFileManager; public VoucherCsvFileRepository(VoucherCsvFileManager voucherCsvFileManager) { @@ -30,23 +31,23 @@ public VoucherCsvFileRepository(VoucherCsvFileManager voucherCsvFileManager) { } @Override - public Optional findById(UUID voucherId) { + public Optional findById(UUID voucherId) { return Optional.ofNullable(vouchers.get(voucherId)); } @Override - public List findAll() { + public List findAll() { return new ArrayList<>(vouchers.values()); } @Override - public VoucherPolicy upsert(VoucherPolicy voucherPolicy) { - if (Optional.ofNullable(vouchers.get(voucherPolicy.getVoucherId())).isPresent()) { - vouchers.replace(voucherPolicy.getVoucherId(), voucherPolicy); + public Voucher upsert(Voucher voucher) { + if (Optional.ofNullable(vouchers.get(voucher.getVoucherId())).isPresent()) { + vouchers.replace(voucher.getVoucherId(), voucher); } else { - vouchers.put(voucherPolicy.getVoucherId(), voucherPolicy); + vouchers.put(voucher.getVoucherId(), voucher); } - return voucherPolicy; + return voucher; } @Override @@ -61,7 +62,7 @@ public void deleteAll() { @PostConstruct private void fileRead(){ - List voucherPolicies = voucherCsvFileManager.read(); + List voucherPolicies = voucherCsvFileManager.read(); voucherPolicies.forEach(this::upsert); } diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java index f39bce2eba..6ecaa2bad1 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java @@ -2,6 +2,7 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.VoucherType; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.context.annotation.Profile; import org.springframework.dao.EmptyResultDataAccessException; @@ -16,7 +17,7 @@ @Repository @Slf4j @Profile({"dev", "prod"}) -public class VoucherDatabaseRepository implements VoucherRepository { +public class VoucherDatabaseRepository implements VoucherRepository { // 네이밍 고민 private final NamedParameterJdbcTemplate jdbcTemplate; public VoucherDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { @@ -24,8 +25,8 @@ public VoucherDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { } @Override - public VoucherPolicy upsert(VoucherPolicy voucher) { - Optional foundVoucher = findById(voucher.getVoucherId()); + public Voucher upsert(Voucher voucher) { + Optional foundVoucher = findById(voucher.getVoucherId()); // 재사용 지양 if (foundVoucher.isPresent()){ jdbcTemplate.update("UPDATE vouchers SET discount_degree = :discountDegree, voucher_type = :voucherType WHERE voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", @@ -40,7 +41,7 @@ public VoucherPolicy upsert(VoucherPolicy voucher) { } @Override - public Optional findById(UUID voucherId) { + public Optional findById(UUID voucherId) { try { return Optional.ofNullable( jdbcTemplate.queryForObject("SELECT * FROM vouchers WHERE voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", @@ -53,7 +54,7 @@ public Optional findById(UUID voucherId) { } @Override - public List findAll() { + public List findAll() { return jdbcTemplate.query("SELECT * FROM vouchers", mapToVoucher); } @@ -68,15 +69,15 @@ public void deleteAll() { jdbcTemplate.update("DELETE FROM vouchers", Collections.emptyMap()); } - private static Map toParamMap(VoucherPolicy voucher) { + private static Map toParamMap(Voucher voucher) { return new HashMap<>(){{ put("voucherId", voucher.getVoucherId().toString().getBytes()); put("discountDegree", voucher.getDiscountDegree()); - put("voucherType", voucher.getClass().getSimpleName()); + put("voucherType", voucher.getVoucherPolicy().getClass().getSimpleName()); }}; } - private static RowMapper mapToVoucher = (rs, rowNum) -> { + private static RowMapper mapToVoucher = (rs, rowNum) -> { UUID voucherId = bytesToUUID(rs.getBytes("voucher_id")); long discountDegree = rs.getLong("discount_degree"); String voucherTypeString = rs.getString("voucher_type"); @@ -84,8 +85,9 @@ private static Map toParamMap(VoucherPolicy voucher) { .filter(vt -> vt.getDisplayName().equals(voucherTypeString)) .findAny() .orElseThrow(() -> new NoSuchElementException("해당 VoucherType이 존재하지 않음.")); + VoucherPolicy voucherPolicy = voucherType.create(); - return voucherType.create(voucherId, discountDegree); + return new Voucher(voucherId, discountDegree, voucherPolicy); }; diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java index e5094612b3..9643605b32 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java @@ -1,6 +1,7 @@ package org.prgms.springbootbasic.repository.voucher; import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository; @@ -15,26 +16,26 @@ @Profile({"local"}) @Slf4j public class VoucherMemoryRepository implements VoucherRepository{ - private final ConcurrentHashMap vouchers = new ConcurrentHashMap<>(); + private final ConcurrentHashMap vouchers = new ConcurrentHashMap<>(); @Override - public Optional findById(UUID voucherId) { + public Optional findById(UUID voucherId) { return Optional.ofNullable(vouchers.get(voucherId)); } @Override - public List findAll() { + public List findAll() { return new ArrayList<>(vouchers.values()); } @Override - public VoucherPolicy upsert(VoucherPolicy voucherPolicy) { - if (Optional.ofNullable(vouchers.get(voucherPolicy.getVoucherId())).isPresent()) { - vouchers.replace(voucherPolicy.getVoucherId(), voucherPolicy); + public Voucher upsert(Voucher voucher) { + if (Optional.ofNullable(vouchers.get(voucher.getVoucherId())).isPresent()) { + vouchers.replace(voucher.getVoucherId(), voucher); } else { - vouchers.put(voucherPolicy.getVoucherId(), voucherPolicy); + vouchers.put(voucher.getVoucherId(), voucher); } - return voucherPolicy; + return voucher; } @Override diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java index 0b4bf8e3cf..22479b9b3e 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java @@ -1,5 +1,6 @@ package org.prgms.springbootbasic.repository.voucher; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import java.util.List; @@ -7,9 +8,9 @@ import java.util.UUID; public interface VoucherRepository { - Optional findById(UUID voucherId); - List findAll(); - VoucherPolicy upsert(VoucherPolicy voucherPolicy); + Optional findById(UUID voucherId); + List findAll(); + Voucher upsert(Voucher voucher); void deleteById(UUID voucherId); void deleteAll(); } diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java index 107b34dca0..3c2a56c8df 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java @@ -3,6 +3,7 @@ import org.prgms.springbootbasic.common.UtilMethod; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; @@ -21,6 +22,8 @@ public WalletDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } + + @Override public void allocateVoucherById(UUID customerId, UUID voucherId) { jdbcTemplate.update("INSERT INTO wallet VALUES (UNHEX(REPLACE(:customerId, '-', '')), UNHEX(REPLACE(:voucherId, '-', '')))", @@ -34,7 +37,7 @@ public void deleteVoucherById(UUID customerId, UUID voucherId) { } @Override - public List searchVouchersByCustomerId(UUID customerId) { + public List searchVouchersByCustomerId(UUID customerId) { return jdbcTemplate.query("SELECT v.voucher_id, v.discount_degree, v.voucher_type " + "FROM vouchers v JOIN wallet w ON v.voucher_id = w.voucher_id " + "WHERE w.customer_id = UNHEX(REPLACE(:customerId, '-', ''))", @@ -58,7 +61,8 @@ private Map toParamMap(UUID customerId, UUID voucherId){ put("voucherId", voucherId.toString().getBytes()); }}; } - private static RowMapper mapToVoucher = (rs, rowNum) -> { + + private static RowMapper mapToVoucher = (rs, rowNum) -> { UUID voucherId = bytesToUUID(rs.getBytes("voucher_id")); long discountDegree = rs.getLong("discount_degree"); String voucherTypeString = rs.getString("voucher_type"); @@ -66,8 +70,9 @@ private Map toParamMap(UUID customerId, UUID voucherId){ .filter(vt -> vt.getDisplayName().equals(voucherTypeString)) .findAny() .orElseThrow(() -> new NoSuchElementException("해당 VoucherType이 존재하지 않음.")); + VoucherPolicy voucherPolicy = voucherType.create(); - return voucherType.create(voucherId, discountDegree); + return new Voucher(voucherId, discountDegree, voucherPolicy); }; private static RowMapper mapToCustomer = (rs, rowNum) -> { diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java index 1d4f737a0a..ff08c34a22 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java @@ -1,6 +1,7 @@ package org.prgms.springbootbasic.repository.wallet; import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import java.util.List; @@ -9,6 +10,6 @@ public interface WalletRepository { void allocateVoucherById(UUID customerId, UUID voucherId); void deleteVoucherById(UUID customerId, UUID voucherId); - List searchVouchersByCustomerId(UUID customerId); + List searchVouchersByCustomerId(UUID customerId); List searchCustomersByVoucherId(UUID voucherId); } diff --git a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java index 336ccafdf7..a9cafac70a 100644 --- a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java +++ b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java @@ -2,11 +2,13 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.VoucherType; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.prgms.springbootbasic.repository.voucher.VoucherRepository; import org.springframework.stereotype.Service; import java.util.List; +import java.util.UUID; @Service @Slf4j @@ -22,12 +24,13 @@ public VoucherType seqToType(int voucherSeq) { } public void create(VoucherType voucherType, int discountDegree) { - VoucherPolicy voucherPolicy = voucherType.create(discountDegree); + VoucherPolicy voucherPolicy = voucherType.create(); + Voucher voucher = new Voucher(UUID.randomUUID(), discountDegree, voucherPolicy); - voucherRepository.upsert(voucherPolicy); + voucherRepository.upsert(voucher); } - public List findAll(){ + public List findAll(){ return voucherRepository.findAll(); } } diff --git a/src/main/java/org/prgms/springbootbasic/service/WalletService.java b/src/main/java/org/prgms/springbootbasic/service/WalletService.java index 98f3b07e64..fedc82b440 100644 --- a/src/main/java/org/prgms/springbootbasic/service/WalletService.java +++ b/src/main/java/org/prgms/springbootbasic/service/WalletService.java @@ -24,7 +24,7 @@ public void delete(UUID customerId, UUID voucherId){ walletRepository.deleteVoucherById(customerId, voucherId); } - public List searchVouchersFromCustomer(UUID customerId){ + public List searchVouchersFromCustomer(UUID customerId){ return walletRepository.searchVouchersByCustomerId(customerId); } diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java index 7b785c547f..eb255580ac 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java @@ -2,13 +2,15 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; -import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; +import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -29,65 +31,77 @@ void cleanUp(){ @Test void findVoucherByIdFromFile() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy()); - voucherCsvFileRepository.upsert(fixedAmountVoucher); + voucherCsvFileRepository.upsert(fixedVoucher); - Optional retrievedVoucher = voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()); + Optional retrievedVoucher = voucherCsvFileRepository.findById(fixedVoucher.getVoucherId()); assertThat(retrievedVoucher.isPresent(), is(true)); - assertThat(retrievedVoucher.get(), samePropertyValuesAs(fixedAmountVoucher)); + compareVoucher(retrievedVoucher.get(), fixedVoucher); } @Test void findAllVoucherFromFile() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); - PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 10); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy()); + Voucher percentVoucher = new Voucher(UUID.randomUUID(), 10, new PercentDiscountPolicy()); - voucherCsvFileRepository.upsert(fixedAmountVoucher); - voucherCsvFileRepository.upsert(percentDiscountVoucher); + voucherCsvFileRepository.upsert(fixedVoucher); + voucherCsvFileRepository.upsert(percentVoucher); - assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); - assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); - assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(percentDiscountVoucher))); + List vouchers = voucherCsvFileRepository.findAll(); + List voucherUUIDs = vouchers.stream().map(Voucher::getVoucherId).toList(); + + assertThat(vouchers, hasSize(2)); + assertThat(voucherUUIDs, hasItem(fixedVoucher.getVoucherId())); + assertThat(voucherUUIDs, hasItem(percentVoucher.getVoucherId())); } @Test void createVoucherToFile() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 2000); - PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 2000, new FixedAmountPolicy()); + Voucher percentVoucher = new Voucher(UUID.randomUUID(), 20, new PercentDiscountPolicy()); + + voucherCsvFileRepository.upsert(fixedVoucher); + voucherCsvFileRepository.upsert(percentVoucher); - voucherCsvFileRepository.upsert(fixedAmountVoucher); - voucherCsvFileRepository.upsert(percentDiscountVoucher); + List vouchers = voucherCsvFileRepository.findAll(); + List voucherUUIDs = vouchers.stream().map(Voucher::getVoucherId).toList(); - assertThat(voucherCsvFileRepository.findAll(), hasSize(2)); - assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); - assertThat(voucherCsvFileRepository.findAll(), hasItem(samePropertyValuesAs(percentDiscountVoucher))); - assertThat(voucherCsvFileRepository.findAll(), - not(hasItem(new FixedAmountVoucher(UUID.randomUUID(), 2000)))); + assertThat(vouchers, hasSize(2)); + assertThat(voucherUUIDs, hasItem(fixedVoucher.getVoucherId())); + assertThat(voucherUUIDs, hasItem(percentVoucher.getVoucherId())); + assertThat(vouchers, + not(hasItem(samePropertyValuesAs(new Voucher(UUID.randomUUID(), 2000, new FixedAmountPolicy()))))); } @Test void deleteVoucherById() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy()); - voucherCsvFileRepository.upsert(fixedAmountVoucher); + voucherCsvFileRepository.upsert(fixedVoucher); - assertThat(voucherCsvFileRepository.findById(fixedAmountVoucher.getVoucherId()).isPresent(), is(true)); + assertThat(voucherCsvFileRepository.findById(fixedVoucher.getVoucherId()).isPresent(), is(true)); voucherCsvFileRepository.deleteById(UUID.randomUUID()); assertThat(voucherCsvFileRepository.findAll(), hasSize(1)); - voucherCsvFileRepository.deleteById(fixedAmountVoucher.getVoucherId()); + voucherCsvFileRepository.deleteById(fixedVoucher.getVoucherId()); assertThat(voucherCsvFileRepository.findAll(), hasSize(0)); } @Test void deleteAllVoucher() { - voucherCsvFileRepository.upsert(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + voucherCsvFileRepository.upsert(new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy())); voucherCsvFileRepository.deleteAll(); assertThat(voucherCsvFileRepository.findAll(), hasSize(0)); } + + private void compareVoucher(Voucher v1, Voucher v2){ + assertThat(v1.getVoucherId(), is(v2.getVoucherId())); + assertThat(v1.getDiscountDegree(), is(v2.getDiscountDegree())); + assertThat(v1.getVoucherPolicy().getClass().getSimpleName(), is(v2.getVoucherPolicy().getClass().getSimpleName())); + } } diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java index fb232fc33d..3636cbbb6a 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java @@ -3,8 +3,9 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; -import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; +import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -24,10 +25,10 @@ class VoucherDatabaseRepositoryTest { @Autowired private VoucherDatabaseRepository voucherDatabaseRepository; - private PercentDiscountVoucher setUpVoucher; + private Voucher setUpVoucher; @BeforeEach void setUp() { - setUpVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 40); + setUpVoucher = new Voucher(UUID.randomUUID(), 40, new PercentDiscountPolicy()); voucherDatabaseRepository.upsert(setUpVoucher); } @@ -38,67 +39,74 @@ void clean() { @Test void saveNewVoucherToDB() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy()); - voucherDatabaseRepository.upsert(fixedAmountVoucher); + voucherDatabaseRepository.upsert(fixedVoucher); - Optional retrievedVoucher = voucherDatabaseRepository.findById(fixedAmountVoucher.getVoucherId()); + Optional retrievedVoucher = voucherDatabaseRepository.findById(fixedVoucher.getVoucherId()); assertThat(retrievedVoucher.isPresent(), is(true)); - assertThat(retrievedVoucher.get(), samePropertyValuesAs(fixedAmountVoucher)); + compareVoucher(retrievedVoucher.get(), fixedVoucher); } @Test void updateVoucherInDB() { - FixedAmountVoucher updateVoucher = new FixedAmountVoucher(setUpVoucher.getVoucherId(), 2000); + Voucher updateVoucher = new Voucher(setUpVoucher.getVoucherId(), 2000, new FixedAmountPolicy()); voucherDatabaseRepository.upsert(updateVoucher); - Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); + Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); assertThat(retrievedVoucher.isPresent(), is(true)); - assertThat(retrievedVoucher.get(), samePropertyValuesAs(updateVoucher)); + compareVoucher(retrievedVoucher.get(), updateVoucher); } @Test void findVoucherByIdInDB() { - Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); + Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); assertThat(retrievedVoucher.isPresent(), is(true)); - assertThat(retrievedVoucher.get(), samePropertyValuesAs(setUpVoucher)); + compareVoucher(retrievedVoucher.get(), setUpVoucher); } @Test void findAllVouchersInDB() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 3000); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 3000, new FixedAmountPolicy()); - voucherDatabaseRepository.upsert(fixedAmountVoucher); + voucherDatabaseRepository.upsert(fixedVoucher); - List vouchers = voucherDatabaseRepository.findAll(); + List vouchers = voucherDatabaseRepository.findAll(); + List voucherUUIDs = vouchers.stream().map(Voucher::getVoucherId).toList(); assertThat(vouchers, hasSize(2)); - assertThat(vouchers, hasItem(samePropertyValuesAs(fixedAmountVoucher))); - assertThat(vouchers, hasItem(samePropertyValuesAs(setUpVoucher))); + assertThat(voucherUUIDs, hasItem(fixedVoucher.getVoucherId())); + assertThat(voucherUUIDs, hasItem(setUpVoucher.getVoucherId())); } @Test void deleteVoucherByIdInDB() { voucherDatabaseRepository.deleteById(setUpVoucher.getVoucherId()); - List vouchers = voucherDatabaseRepository.findAll(); + List vouchers = voucherDatabaseRepository.findAll(); assertThat(vouchers, hasSize(0)); } @Test void deleteAllVouchersInDB() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 3000); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 3000, new FixedAmountPolicy()); - voucherDatabaseRepository.upsert(fixedAmountVoucher); + voucherDatabaseRepository.upsert(fixedVoucher); voucherDatabaseRepository.deleteAll(); - List vouchers = voucherDatabaseRepository.findAll(); + List vouchers = voucherDatabaseRepository.findAll(); assertThat(vouchers.size(), is(0)); } + + private void compareVoucher(Voucher v1, Voucher v2){ + assertThat(v1.getVoucherId(), is(v2.getVoucherId())); + assertThat(v1.getDiscountDegree(), is(v2.getDiscountDegree())); + assertThat(v1.getVoucherPolicy().getClass().getSimpleName(), is(v2.getVoucherPolicy().getClass().getSimpleName())); + } } diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java index 882e00a4de..99dd659821 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java @@ -2,8 +2,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.prgms.springbootbasic.domain.voucher.FixedAmountVoucher; -import org.prgms.springbootbasic.domain.voucher.PercentDiscountVoucher; +import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; @@ -31,49 +32,49 @@ void cleanUp(){ @Test void findVoucherByIdFromMemory() { UUID voucherId = UUID.randomUUID(); - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(voucherId, 1000); + Voucher fixedVoucher = new Voucher(voucherId, 1000, new FixedAmountPolicy()); - voucherMemoryRepository.upsert(fixedAmountVoucher); + voucherMemoryRepository.upsert(fixedVoucher); - Optional retrievedVoucher = voucherMemoryRepository.findById(voucherId); - Optional randomRetrievedVoucher = voucherMemoryRepository.findById(UUID.randomUUID()); + Optional retrievedVoucher = voucherMemoryRepository.findById(voucherId); + Optional randomRetrievedVoucher = voucherMemoryRepository.findById(UUID.randomUUID()); assertThat(randomRetrievedVoucher.isEmpty(), is(true)); assertThat(retrievedVoucher.isEmpty(), is(false)); - assertThat(retrievedVoucher.get(), samePropertyValuesAs(fixedAmountVoucher)); + assertThat(retrievedVoucher.get(), samePropertyValuesAs(fixedVoucher)); } @Test void findAllVoucherFromMemory() { - PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 20); - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 6000); + Voucher percentDiscountVoucher = new Voucher(UUID.randomUUID(), 20, new PercentDiscountPolicy()); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 6000, new FixedAmountPolicy()); voucherMemoryRepository.upsert(percentDiscountVoucher); - voucherMemoryRepository.upsert(fixedAmountVoucher); + voucherMemoryRepository.upsert(fixedVoucher); assertThat(voucherMemoryRepository.findAll(), hasSize(2)); assertThat(voucherMemoryRepository.findAll(), hasItem(samePropertyValuesAs(percentDiscountVoucher))); - assertThat(voucherMemoryRepository.findAll(), hasItem(samePropertyValuesAs(fixedAmountVoucher))); + assertThat(voucherMemoryRepository.findAll(), hasItem(samePropertyValuesAs(fixedVoucher))); } @Test void createNewVouchersToMemory() { - FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(UUID.randomUUID(), 1000); - PercentDiscountVoucher percentDiscountVoucher = new PercentDiscountVoucher(UUID.randomUUID(), 30); + Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy()); + Voucher percentDiscountVoucher = new Voucher(UUID.randomUUID(), 30, new PercentDiscountPolicy()); - this.voucherMemoryRepository.upsert(fixedAmountVoucher); + this.voucherMemoryRepository.upsert(fixedVoucher); this.voucherMemoryRepository.upsert(percentDiscountVoucher); assertThat(voucherMemoryRepository.findAll(), hasSize(2)); assertThrows(IllegalArgumentException.class, - () -> voucherMemoryRepository.upsert(new PercentDiscountVoucher(UUID.randomUUID(), 0))); + () -> voucherMemoryRepository.upsert(new Voucher(UUID.randomUUID(), 0, new PercentDiscountPolicy()))); // 여기에 이걸 넣는게 맞나? } @Test void deleteAllVouchersFromMemory() { - voucherMemoryRepository.upsert(new PercentDiscountVoucher(UUID.randomUUID(), 10)); - voucherMemoryRepository.upsert(new PercentDiscountVoucher(UUID.randomUUID(), 20)); - voucherMemoryRepository.upsert(new FixedAmountVoucher(UUID.randomUUID(), 1000)); + voucherMemoryRepository.upsert(new Voucher(UUID.randomUUID(), 10, new PercentDiscountPolicy())); + voucherMemoryRepository.upsert(new Voucher(UUID.randomUUID(), 20, new PercentDiscountPolicy())); + voucherMemoryRepository.upsert(new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy())); voucherMemoryRepository.deleteAll(); From 3bb2f99c6c6c469fc6d1cb3a305fdd8b22ab7f41 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Wed, 1 Nov 2023 09:25:06 +0900 Subject: [PATCH 53/62] =?UTF-8?q?Feat:=20Policy=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=9D=BC=20=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=A1=B0=EA=B1=B4?= =?UTF-8?q?=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=98=EA=B3=A0=20VoucherPolic?= =?UTF-8?q?y=EC=9D=98=20getter=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/domain/voucher/Voucher.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java index dbf5c91c3c..b294dbe806 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java @@ -12,7 +12,7 @@ public class Voucher { private final VoucherPolicy voucherPolicy; public Voucher(UUID voucherId, long discountDegree, VoucherPolicy voucherPolicy) { - if (voucherPolicy instanceof PercentDiscountVoucher) { + if (voucherPolicy instanceof PercentDiscountPolicy) { if (discountDegree <= 0 || discountDegree > 100) { log.error("percent value is out of range."); throw new OutOfRangeException("percent value is out of range."); @@ -32,6 +32,10 @@ public long getDiscountDegree() { return discountDegree; } + public VoucherPolicy getVoucherPolicy() { + return voucherPolicy; + } + public long discount(long beforeDiscount){ return voucherPolicy.discount(beforeDiscount, this.discountDegree); } From c6e8871f14aeec325e51f9283d9dbe9f36d1f505 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Wed, 1 Nov 2023 09:27:24 +0900 Subject: [PATCH 54/62] =?UTF-8?q?Refactor:=20import=20=EB=AC=B8=EC=9D=84?= =?UTF-8?q?=20=EC=B5=9C=EC=A0=81=ED=99=94=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/controller/MainController.java | 2 -- .../repository/voucher/VoucherCsvFileRepository.java | 1 - .../repository/voucher/VoucherMemoryRepository.java | 1 - .../springbootbasic/repository/voucher/VoucherRepository.java | 1 - .../springbootbasic/repository/wallet/WalletRepository.java | 1 - .../java/org/prgms/springbootbasic/service/WalletService.java | 2 +- .../repository/voucher/VoucherCsvFileRepositoryTest.java | 1 - .../repository/voucher/VoucherDatabaseRepositoryTest.java | 1 - .../repository/voucher/VoucherMemoryRepositoryTest.java | 1 - 9 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index 2550e0fe02..6054412506 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -5,7 +5,6 @@ import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.customer.Customer; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.prgms.springbootbasic.service.CustomerService; import org.prgms.springbootbasic.service.VoucherService; import org.prgms.springbootbasic.service.WalletService; @@ -18,7 +17,6 @@ import java.util.UUID; import static org.prgms.springbootbasic.common.console.Console.*; -import static org.prgms.springbootbasic.common.console.Console.typeVoucherId; @Controller @Slf4j diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java index 441485b3e5..24ba617d8d 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepository.java @@ -3,7 +3,6 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.common.file.VoucherCsvFileManager; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository; diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java index 9643605b32..e100d1b8e1 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepository.java @@ -2,7 +2,6 @@ import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository; diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java index 22479b9b3e..23c68afd9d 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherRepository.java @@ -1,7 +1,6 @@ package org.prgms.springbootbasic.repository.voucher; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import java.util.List; import java.util.Optional; diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java index ff08c34a22..5ec03e60a0 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java @@ -2,7 +2,6 @@ import org.prgms.springbootbasic.domain.customer.Customer; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import java.util.List; import java.util.UUID; diff --git a/src/main/java/org/prgms/springbootbasic/service/WalletService.java b/src/main/java/org/prgms/springbootbasic/service/WalletService.java index fedc82b440..15d6fa8812 100644 --- a/src/main/java/org/prgms/springbootbasic/service/WalletService.java +++ b/src/main/java/org/prgms/springbootbasic/service/WalletService.java @@ -1,7 +1,7 @@ package org.prgms.springbootbasic.service; import org.prgms.springbootbasic.domain.customer.Customer; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; +import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.repository.wallet.WalletRepository; import org.springframework.stereotype.Service; diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java index eb255580ac..93406f8dba 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherCsvFileRepositoryTest.java @@ -5,7 +5,6 @@ import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java index 3636cbbb6a..c8149325d3 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java @@ -6,7 +6,6 @@ import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java index 99dd659821..1845a29fdc 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherMemoryRepositoryTest.java @@ -5,7 +5,6 @@ import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.domain.voucher.VoucherPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; From 0ddbbfb93d811d5222317688e6991a6bf73c3a8b Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Wed, 1 Nov 2023 09:36:04 +0900 Subject: [PATCH 55/62] =?UTF-8?q?Refactor:=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EB=AA=85=EC=9D=84=20=EC=A7=81=EA=B4=80=EC=A0=81=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=88=98=EC=A0=95=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...itory.java => CustomerJdbcRepository.java} | 4 +-- ...sitory.java => VoucherJdbcRepository.java} | 4 +-- ...ository.java => WalletJdbcRepository.java} | 7 ++-- ...t.java => CustomerJdbcRepositoryTest.java} | 5 ++- ...st.java => VoucherJdbcRepositoryTest.java} | 32 +++++++++---------- 5 files changed, 28 insertions(+), 24 deletions(-) rename src/main/java/org/prgms/springbootbasic/repository/customer/{CustomerDatabaseRepository.java => CustomerJdbcRepository.java} (96%) rename src/main/java/org/prgms/springbootbasic/repository/voucher/{VoucherDatabaseRepository.java => VoucherJdbcRepository.java} (95%) rename src/main/java/org/prgms/springbootbasic/repository/wallet/{WalletDatabaseRepository.java => WalletJdbcRepository.java} (95%) rename src/test/java/org/prgms/springbootbasic/repository/customer/{CustomerDatabaseRepositoryTest.java => CustomerJdbcRepositoryTest.java} (89%) rename src/test/java/org/prgms/springbootbasic/repository/voucher/{VoucherDatabaseRepositoryTest.java => VoucherJdbcRepositoryTest.java} (71%) diff --git a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerJdbcRepository.java similarity index 96% rename from src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/customer/CustomerJdbcRepository.java index 9d40d2266a..63db16a8c2 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customer/CustomerJdbcRepository.java @@ -15,10 +15,10 @@ @Repository @Slf4j @Profile({"dev", "prod"}) -public class CustomerDatabaseRepository implements CustomerRepository{ +public class CustomerJdbcRepository implements CustomerRepository{ private final NamedParameterJdbcTemplate jdbcTemplate; - public CustomerDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { + public CustomerJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } diff --git a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherJdbcRepository.java similarity index 95% rename from src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherJdbcRepository.java index 6ecaa2bad1..e8847bfff1 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/voucher/VoucherJdbcRepository.java @@ -17,10 +17,10 @@ @Repository @Slf4j @Profile({"dev", "prod"}) -public class VoucherDatabaseRepository implements VoucherRepository { // 네이밍 고민 +public class VoucherJdbcRepository implements VoucherRepository { // 네이밍 고민 private final NamedParameterJdbcTemplate jdbcTemplate; - public VoucherDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { + public VoucherJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletJdbcRepository.java similarity index 95% rename from src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/wallet/WalletJdbcRepository.java index 3c2a56c8df..6da874172e 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletDatabaseRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletJdbcRepository.java @@ -15,10 +15,10 @@ import static org.prgms.springbootbasic.common.UtilMethod.bytesToUUID; @Repository -public class WalletDatabaseRepository implements WalletRepository { +public class WalletJdbcRepository implements WalletRepository { private final NamedParameterJdbcTemplate jdbcTemplate; - public WalletDatabaseRepository(NamedParameterJdbcTemplate jdbcTemplate) { + public WalletJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @@ -85,5 +85,6 @@ private Map toParamMap(UUID customerId, UUID voucherId){ boolean isBlacked = rs.getBoolean("is_blacked"); return new Customer(customerId, customerName, email, lastLoginAt, createdAt, isBlacked); - }; + }; // static 메서드의 위치 조정. + } diff --git a/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerJdbcRepositoryTest.java similarity index 89% rename from src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java rename to src/test/java/org/prgms/springbootbasic/repository/customer/CustomerJdbcRepositoryTest.java index 91dbf9d532..318363cb74 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerDatabaseRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/customer/CustomerJdbcRepositoryTest.java @@ -20,7 +20,10 @@ @SpringBootTest @ActiveProfiles("dev") -class CustomerDatabaseRepositoryTest { +class CustomerJdbcRepositoryTest { // @Transactional은 위험할 수 있다. "A"CID. 트랜잭션은 전파 전략이 부모 레벨을 따라간다. 상위도 롤백될 가능성이. 테스트가 부모, 운영 코드가 자식. 트랜잭션 처리 공부. + // 즉, 테스트에서 @Transactional을 사용하고, 운영 코드에서도 @Transactional을 사용한다면 기본 전파 전략에 따라 테스트 @Transactional을 우선시한다. + // 하지만 이를 인지하고 잘 사용한다면 @Transactional을 사용해도 괜찮다. + @Autowired private CustomerRepository customerRepository; diff --git a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherJdbcRepositoryTest.java similarity index 71% rename from src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java rename to src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherJdbcRepositoryTest.java index c8149325d3..3463057883 100644 --- a/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherDatabaseRepositoryTest.java +++ b/src/test/java/org/prgms/springbootbasic/repository/voucher/VoucherJdbcRepositoryTest.java @@ -20,29 +20,29 @@ @SpringBootTest @ActiveProfiles("dev") -class VoucherDatabaseRepositoryTest { +class VoucherJdbcRepositoryTest { @Autowired - private VoucherDatabaseRepository voucherDatabaseRepository; + private VoucherJdbcRepository voucherJdbcRepository; private Voucher setUpVoucher; @BeforeEach void setUp() { setUpVoucher = new Voucher(UUID.randomUUID(), 40, new PercentDiscountPolicy()); - voucherDatabaseRepository.upsert(setUpVoucher); + voucherJdbcRepository.upsert(setUpVoucher); } @AfterEach void clean() { - voucherDatabaseRepository.deleteAll(); + voucherJdbcRepository.deleteAll(); } @Test void saveNewVoucherToDB() { Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 1000, new FixedAmountPolicy()); - voucherDatabaseRepository.upsert(fixedVoucher); + voucherJdbcRepository.upsert(fixedVoucher); - Optional retrievedVoucher = voucherDatabaseRepository.findById(fixedVoucher.getVoucherId()); + Optional retrievedVoucher = voucherJdbcRepository.findById(fixedVoucher.getVoucherId()); assertThat(retrievedVoucher.isPresent(), is(true)); compareVoucher(retrievedVoucher.get(), fixedVoucher); @@ -52,9 +52,9 @@ void saveNewVoucherToDB() { void updateVoucherInDB() { Voucher updateVoucher = new Voucher(setUpVoucher.getVoucherId(), 2000, new FixedAmountPolicy()); - voucherDatabaseRepository.upsert(updateVoucher); + voucherJdbcRepository.upsert(updateVoucher); - Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); + Optional retrievedVoucher = voucherJdbcRepository.findById(setUpVoucher.getVoucherId()); assertThat(retrievedVoucher.isPresent(), is(true)); compareVoucher(retrievedVoucher.get(), updateVoucher); @@ -62,7 +62,7 @@ void updateVoucherInDB() { @Test void findVoucherByIdInDB() { - Optional retrievedVoucher = voucherDatabaseRepository.findById(setUpVoucher.getVoucherId()); + Optional retrievedVoucher = voucherJdbcRepository.findById(setUpVoucher.getVoucherId()); assertThat(retrievedVoucher.isPresent(), is(true)); compareVoucher(retrievedVoucher.get(), setUpVoucher); @@ -72,9 +72,9 @@ void findVoucherByIdInDB() { void findAllVouchersInDB() { Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 3000, new FixedAmountPolicy()); - voucherDatabaseRepository.upsert(fixedVoucher); + voucherJdbcRepository.upsert(fixedVoucher); - List vouchers = voucherDatabaseRepository.findAll(); + List vouchers = voucherJdbcRepository.findAll(); List voucherUUIDs = vouchers.stream().map(Voucher::getVoucherId).toList(); assertThat(vouchers, hasSize(2)); @@ -84,9 +84,9 @@ void findAllVouchersInDB() { @Test void deleteVoucherByIdInDB() { - voucherDatabaseRepository.deleteById(setUpVoucher.getVoucherId()); + voucherJdbcRepository.deleteById(setUpVoucher.getVoucherId()); - List vouchers = voucherDatabaseRepository.findAll(); + List vouchers = voucherJdbcRepository.findAll(); assertThat(vouchers, hasSize(0)); } @@ -95,10 +95,10 @@ void deleteVoucherByIdInDB() { void deleteAllVouchersInDB() { Voucher fixedVoucher = new Voucher(UUID.randomUUID(), 3000, new FixedAmountPolicy()); - voucherDatabaseRepository.upsert(fixedVoucher); - voucherDatabaseRepository.deleteAll(); + voucherJdbcRepository.upsert(fixedVoucher); + voucherJdbcRepository.deleteAll(); - List vouchers = voucherDatabaseRepository.findAll(); + List vouchers = voucherJdbcRepository.findAll(); assertThat(vouchers.size(), is(0)); } From 80b422af2e09eb345f47b8da611aae778ee71916 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Wed, 1 Nov 2023 09:37:11 +0900 Subject: [PATCH 56/62] =?UTF-8?q?Chore:=20DataSource=20=EB=B9=88=EC=9D=84?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=ED=95=98=EC=A7=80=20=EB=AA=BB=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=AC=B8=EC=A0=9C=EB=A5=BC=20=ED=95=B4=EA=B2=B0?= =?UTF-8?q?=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=B4=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=9A=A9=20DB=EC=99=80=20=EC=97=B0=EA=B2=B0=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-test.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index 00d124a133..46d45d35fd 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -1,3 +1,9 @@ basic: file: path: ./src/test/resources/voucher.csv +spring: + datasource: + url: jdbc:mysql://localhost:3306/test + username: test + password: 1234 + driver-class-name: com.mysql.cj.jdbc.Driver From 716ee1943f67f72ac245b5709e6db14ea9e0d445 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Wed, 1 Nov 2023 10:23:35 +0900 Subject: [PATCH 57/62] =?UTF-8?q?Feat:=20Customer=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=9C=20create,=20findAll=EC=9D=84=20=EC=BD=98=EC=86=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EC=97=90=20=EC=B6=94=EA=B0=80=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/console/Console.java | 23 +++-- .../controller/MainController.java | 87 +++++++++++++------ .../domain/voucher/Voucher.java | 9 ++ .../service/CustomerService.java | 17 ++++ .../service/VoucherService.java | 2 +- 5 files changed, 104 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/common/console/Console.java b/src/main/java/org/prgms/springbootbasic/common/console/Console.java index 81464fcb29..e10f9c8dca 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/Console.java +++ b/src/main/java/org/prgms/springbootbasic/common/console/Console.java @@ -19,8 +19,10 @@ public class Console { // Console이 common인가? MVC 생각하며 고민하자 public static String readCommand() { System.out.println("=== Voucher Program ==="); - System.out.println("Type 'create' to create a new voucher."); - System.out.println("Type 'list' to list all vouchers."); + System.out.println("Type 'createVoucher' to create a new voucher."); + System.out.println("Type 'createCustomer' to create a new customer."); + System.out.println("Type 'listVoucher' to list all vouchers."); + System.out.println("Type 'listCustomer' to list all customers."); System.out.println("Type 'black' to list customers blacked."); System.out.println("Type 'wallet' to enter wallet service."); System.out.println("Type 'exit' to exit the program."); @@ -28,15 +30,24 @@ public static String readCommand() { return consoleInput.next(); } - public static int selectCreateType() { + public static int selectPolicyType() { System.out.println(); System.out.println("Which voucher would you like to create? Just type number."); - System.out.println(MessageFormat.format("{0}. FixedAmountVoucher", INPUT_FIXED_AMOUNT_VOUCHER)); - System.out.println(MessageFormat.format("{0}. PercentDiscountVoucher", INPUT_PERCENT_DISCOUNT_VOUCHER)); + System.out.println(MessageFormat.format("{0}. with fixed amount discount policy", INPUT_FIXED_AMOUNT_VOUCHER)); + System.out.println(MessageFormat.format("{0}. with percent discount policy", INPUT_PERCENT_DISCOUNT_VOUCHER)); return consoleInput.nextInt(); } + public static String putCustomerInfo() { + ignoreLine(); + + System.out.println(); + System.out.println("Please enter the name and email of the customer you want to create, separated by a space on a single line."); + + return consoleInput.nextLine(); + } + public static int putDiscountDegree(VoucherType type) { switch (type){ case FIXED_AMOUNT -> System.out.println("Enter the fixed discount amount."); @@ -58,7 +69,7 @@ public static void printRuntimeException() { System.out.println("Invalid input or system error. redirect to beginning."); } - public static String readWalletCommand(){ // MVC 제대로 알아옴!! Controller 중재. 뷰와 모델은 컨트롤러 모름. 콘솔이 도메인(Wallet)을 알고 있다. 수정. Console이 두개일 이유도 없다. + public static String readWalletCommand() { System.out.println(); System.out.println("Welcome to our wallet service."); System.out.println("Type 'allocate' if you want to allocate voucher to a customer." ); diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index 6054412506..b0d89ae233 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -1,7 +1,6 @@ package org.prgms.springbootbasic.controller; import lombok.extern.slf4j.Slf4j; -import org.prgms.springbootbasic.common.console.Console; import org.prgms.springbootbasic.domain.VoucherType; import org.prgms.springbootbasic.domain.customer.Customer; import org.prgms.springbootbasic.domain.voucher.Voucher; @@ -9,6 +8,7 @@ import org.prgms.springbootbasic.service.VoucherService; import org.prgms.springbootbasic.service.WalletService; import org.springframework.dao.DataAccessException; +import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Controller; import java.util.InputMismatchException; @@ -22,8 +22,10 @@ @Slf4j public class MainController { private static final String EXIT = "exit"; - private static final String LIST = "list"; - private static final String CREATE = "create"; + private static final String LIST_VOUCHER = "listVoucher"; + private static final String LIST_CUSTOMER = "listCustomer"; + private static final String CREATE_VOUCHER = "createVoucher"; + private static final String CREATE_CUSTOMER = "createCustomer"; private static final String BLACK = "black"; private static final String WALLET = "wallet"; private static final String ALLOCATE = "allocate"; @@ -46,11 +48,16 @@ public void run() { String command = ""; while (!command.equals(EXIT)) { try { - command = Console.readCommand(); + command = readCommand(); executeCommand(command); + success(command); + } catch (IllegalArgumentException e) { + log.error("{}", e.toString()); + + printArgException(); } catch (InputMismatchException e) { - String invalidVal = Console.ignoreLine(); + String invalidVal = ignoreLine(); log.warn("User input = {}", invalidVal); throw new IllegalArgumentException("Not integer."); @@ -60,21 +67,28 @@ public void run() { } catch (IllegalStateException e) { log.error("Scanner is closed"); throw new RuntimeException("Scanner is closed."); - } catch (IllegalArgumentException e) { - Console.printArgException(); + } catch (DuplicateKeyException e) { + log.error("Key duplicate error.", e); + printDuplicateKeyException(); } catch (DataAccessException e) { log.error("Database error.", e); - Console.printRuntimeException(); + printRuntimeException(); } catch (RuntimeException e) { - Console.printRuntimeException(); + printRuntimeException(); } } } // 프론트 컨트롤러 패턴에 대해. 공부해보자. 컨트롤러 일관성 없음. 왜 wallet만 따로 존재하는? 고민... -> 그냥 통일합시다. + private void printDuplicateKeyException() { + System.out.println("There is duplicate key in the values."); + } + private void executeCommand(String command) { switch (command) { - case CREATE -> create(); - case LIST -> list(); + case CREATE_VOUCHER -> createVoucher(); + case CREATE_CUSTOMER -> createCustomer(); + case LIST_VOUCHER -> listVoucher(); + case LIST_CUSTOMER -> listCustomer(); case BLACK -> black(); case WALLET -> runWallet(); case EXIT -> {} @@ -85,37 +99,56 @@ private void executeCommand(String command) { } } - private void create(){ - int voucherSeq = Console.selectCreateType(); + private void createVoucher() { + int voucherSeq = selectPolicyType(); VoucherType voucherType = voucherService.seqToType(voucherSeq); - int discountDegree = Console.putDiscountDegree(voucherType); + int discountDegree = putDiscountDegree(voucherType); + + voucherService.upsert(voucherType, discountDegree); + } + + private void createCustomer() { + String[] info = putCustomerInfo().split(" "); + + if (info.length != 2) { + throw new IllegalArgumentException("Customer info is not valid."); + } + + String name = info[0]; + String email = info[1]; - voucherService.create(voucherType, discountDegree); + customerService.upsert(name, email); } - private void list(){ + private void listVoucher() { List vouchers = voucherService.findAll(); - Console.printList(vouchers); + printList(vouchers); + } + + private void listCustomer() { + List customers = customerService.findAll(); + + printList(customers); } - private void black(){ + private void black() { List blacklist = customerService.findBlackAll(); - Console.printList(blacklist); + printList(blacklist); } - private void runWallet(){ + private void runWallet() { String command = readWalletCommand(); executeWalletCommand(command); success(command); } - private void executeWalletCommand(String command){ + private void executeWalletCommand(String command) { switch (command){ case ALLOCATE -> allocate(); case DELETE -> delete(); @@ -129,31 +162,31 @@ private void executeWalletCommand(String command){ } } - private void allocate(){ + private void allocate() { UUID customerId = typeCustomerId(); UUID voucherId = typeVoucherId(); walletService.allocate(customerId, voucherId); } - private void delete(){ + private void delete() { UUID customerId = typeCustomerId(); UUID voucherId = typeVoucherId(); walletService.delete(customerId, voucherId); } - private void showVoucherByCustomer(){ + private void showVoucherByCustomer() { UUID customerId = typeCustomerId(); List vouchers = walletService.searchVouchersFromCustomer(customerId); - Console.printList(vouchers); + printList(vouchers); } - private void showCustomerByVoucher(){ + private void showCustomerByVoucher() { UUID voucherId = typeVoucherId(); List customers = walletService.searchCustomerFromVoucher(voucherId); - Console.printList(customers); + printList(customers); } } diff --git a/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java b/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java index b294dbe806..c5a536e744 100644 --- a/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java +++ b/src/main/java/org/prgms/springbootbasic/domain/voucher/Voucher.java @@ -39,4 +39,13 @@ public VoucherPolicy getVoucherPolicy() { public long discount(long beforeDiscount){ return voucherPolicy.discount(beforeDiscount, this.discountDegree); } + + @Override + public String toString() { + return "Voucher{" + + "voucherId=" + voucherId + + ", discountDegree=" + discountDegree + + ", voucherPolicy=" + voucherPolicy.getClass().getSimpleName() + "@" + voucherPolicy.hashCode() + + '}'; + } } diff --git a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java index c246059acd..dab56f7720 100644 --- a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java +++ b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java @@ -5,7 +5,9 @@ import org.prgms.springbootbasic.repository.customer.CustomerRepository; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; import java.util.List; +import java.util.UUID; @Service @Slf4j @@ -16,7 +18,22 @@ public CustomerService(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } + public Customer upsert(String name, String email){ + Customer customer = new Customer(UUID.randomUUID(), name, email, LocalDateTime.now()); + + return this.customerRepository.upsert(customer); + } + + public List findAll() { + return customerRepository.findAll(); + } + + public void deleteAll(){ + customerRepository.deleteAll(); + } + public List findBlackAll(){ return customerRepository.findBlackAll(); } } +// 전체적으로 피드백 반영한 후에, 반영 생각과 왜 그렇게 반영했는지. PR에. 테스트 코드 보완도 좀 하고. diff --git a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java index a9cafac70a..2600109934 100644 --- a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java +++ b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java @@ -23,7 +23,7 @@ public VoucherType seqToType(int voucherSeq) { return VoucherType.getTypeFromSeq(voucherSeq); } - public void create(VoucherType voucherType, int discountDegree) { + public void upsert(VoucherType voucherType, int discountDegree) { VoucherPolicy voucherPolicy = voucherType.create(); Voucher voucher = new Voucher(UUID.randomUUID(), discountDegree, voucherPolicy); From 26019b03ba4cea93f19a3cbbc6dcfdc931e3fc89 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Wed, 1 Nov 2023 11:58:54 +0900 Subject: [PATCH 58/62] =?UTF-8?q?Test:=20CustomerService=EC=9D=98=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/CustomerService.java | 2 +- .../service/CustomerServiceTest.java | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java diff --git a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java index dab56f7720..6564966488 100644 --- a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java +++ b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java @@ -18,7 +18,7 @@ public CustomerService(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } - public Customer upsert(String name, String email){ + public Customer upsert(String name, String email) { Customer customer = new Customer(UUID.randomUUID(), name, email, LocalDateTime.now()); return this.customerRepository.upsert(customer); diff --git a/src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java b/src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java new file mode 100644 index 0000000000..71d9be2fee --- /dev/null +++ b/src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java @@ -0,0 +1,78 @@ +package org.prgms.springbootbasic.service; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.repository.customer.CustomerRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +@ActiveProfiles("dev") +class CustomerServiceTest { + @Autowired + private CustomerService customerService; + @MockBean + private CustomerRepository customerRepository; + + @Test + @DisplayName("CustomerService 내 upsert 동작 확인") + void upsertCustomer() { + Customer customer = new Customer(UUID.randomUUID(), "name", "email", LocalDateTime.now()); + + when(customerRepository.upsert(any())).thenReturn(customer); + + Customer createdCustomer = customerService.upsert("name", "email"); + + assertThat(createdCustomer).isEqualTo(customer); + + verify(customerRepository).upsert(any()); + } + + @Test + @DisplayName("CustomerService 내 findAll 동작 확인") + void findAllCustomers() { + when(customerRepository.findAll()) + .thenReturn(List.of(new Customer(UUID.randomUUID(), "c1", "cmail1", LocalDateTime.now()), + new Customer(UUID.randomUUID(), "c2", "cmail2", LocalDateTime.now()))); + List customers = customerService.findAll(); + + assertThat(customers).hasSize(2); + + verify(customerRepository).findAll(); + } + + @Test + @DisplayName("CustomerService 내 deleteAll 동작 확인") + void deleteAllCustomers() { + this.customerService.deleteAll(); + + verify(customerRepository).deleteAll(); + } + + @Test + @DisplayName("CustomerService 내 findAllBlack 동작 확인") + void findAllBlackedCustomers() { + Customer customer = new Customer(UUID.randomUUID(), "c", "cmail", LocalDateTime.now()); + customer.changeInfo(customer.getName(), true); + + when(customerRepository.findBlackAll()).thenReturn(List.of(customer)); + + List blackCustomers = customerService.findBlackAll(); + + assertThat(blackCustomers).hasSize(1); + + verify(customerRepository).findBlackAll(); + } +} From 96392b01ba62f14d3ebac0b30efcb33969f1620b Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Thu, 2 Nov 2023 11:56:39 +0900 Subject: [PATCH 59/62] =?UTF-8?q?Feat:=20findById=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/CustomerService.java | 5 +++++ .../service/VoucherService.java | 5 +++++ .../service/CustomerServiceTest.java | 21 +++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java index 6564966488..82fac0e6ef 100644 --- a/src/main/java/org/prgms/springbootbasic/service/CustomerService.java +++ b/src/main/java/org/prgms/springbootbasic/service/CustomerService.java @@ -7,6 +7,7 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; import java.util.UUID; @Service @@ -24,6 +25,10 @@ public Customer upsert(String name, String email) { return this.customerRepository.upsert(customer); } + public Optional findById(UUID customerId){ + return customerRepository.findById(customerId); + } + public List findAll() { return customerRepository.findAll(); } diff --git a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java index 2600109934..48b17a3105 100644 --- a/src/main/java/org/prgms/springbootbasic/service/VoucherService.java +++ b/src/main/java/org/prgms/springbootbasic/service/VoucherService.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; import java.util.UUID; @Service @@ -30,6 +31,10 @@ public void upsert(VoucherType voucherType, int discountDegree) { voucherRepository.upsert(voucher); } + public Optional findById(UUID voucherId){ + return voucherRepository.findById(voucherId); + } + public List findAll(){ return voucherRepository.findAll(); } diff --git a/src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java b/src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java index 71d9be2fee..4dbd2849af 100644 --- a/src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java +++ b/src/test/java/org/prgms/springbootbasic/service/CustomerServiceTest.java @@ -11,6 +11,7 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @@ -27,7 +28,7 @@ class CustomerServiceTest { private CustomerRepository customerRepository; @Test - @DisplayName("CustomerService 내 upsert 동작 확인") + @DisplayName("upsert가 제대로 동작하는지 확인") void upsertCustomer() { Customer customer = new Customer(UUID.randomUUID(), "name", "email", LocalDateTime.now()); @@ -41,7 +42,23 @@ void upsertCustomer() { } @Test - @DisplayName("CustomerService 내 findAll 동작 확인") + @DisplayName("findById로 제대로 고객을 가져오는지 확인") + void findById(){ + UUID customerId = UUID.randomUUID(); + Customer customer = new Customer(customerId, "name", "email", LocalDateTime.now()); + + when(customerRepository.findById(customerId)).thenReturn(Optional.of(customer)); + + Optional retrievedCustomer = customerService.findById(customerId); + Optional noCustomer = customerService.findById(UUID.randomUUID()); + + assertThat(retrievedCustomer.isPresent()).isTrue(); + assertThat(retrievedCustomer.get()).isEqualTo(customer); + assertThat(noCustomer.isPresent()).isFalse(); + } + + @Test + @DisplayName("findAll로 모든 고객을 가져오는지 확인") void findAllCustomers() { when(customerRepository.findAll()) .thenReturn(List.of(new Customer(UUID.randomUUID(), "c1", "cmail1", LocalDateTime.now()), From f3e265cd6a2d897bfdabcbe89638e71dedcaec25 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Thu, 2 Nov 2023 11:57:40 +0900 Subject: [PATCH 60/62] =?UTF-8?q?Refactor:=20=EB=84=A4=EC=9D=B4=EB=B0=8D?= =?UTF-8?q?=EC=9D=84=20=EC=A0=81=EC=A0=88=ED=9E=88=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MainController.java | 16 ++++----- ...tomerVoucherManagementJdbcRepository.java} | 17 ++++----- .../CustomerVoucherManagementRepository.java} | 4 +-- .../CustomerVoucherManagementService.java | 36 +++++++++++++++++++ .../service/WalletService.java | 34 ------------------ src/main/resources/schema.sql | 2 +- 6 files changed, 56 insertions(+), 53 deletions(-) rename src/main/java/org/prgms/springbootbasic/repository/{wallet/WalletJdbcRepository.java => customervouchermanagement/CustomerVoucherManagementJdbcRepository.java} (79%) rename src/main/java/org/prgms/springbootbasic/repository/{wallet/WalletRepository.java => customervouchermanagement/CustomerVoucherManagementRepository.java} (76%) create mode 100644 src/main/java/org/prgms/springbootbasic/service/CustomerVoucherManagementService.java delete mode 100644 src/main/java/org/prgms/springbootbasic/service/WalletService.java diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index b0d89ae233..af2671b746 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -5,8 +5,8 @@ import org.prgms.springbootbasic.domain.customer.Customer; import org.prgms.springbootbasic.domain.voucher.Voucher; import org.prgms.springbootbasic.service.CustomerService; +import org.prgms.springbootbasic.service.CustomerVoucherManagementService; import org.prgms.springbootbasic.service.VoucherService; -import org.prgms.springbootbasic.service.WalletService; import org.springframework.dao.DataAccessException; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Controller; @@ -36,12 +36,12 @@ public class MainController { private final VoucherService voucherService; private final CustomerService customerService; - private final WalletService walletService; + private final CustomerVoucherManagementService managementService; - public MainController(VoucherService voucherService, CustomerService customerService, WalletService walletService) { + public MainController(VoucherService voucherService, CustomerService customerService, CustomerVoucherManagementService managementService) { this.voucherService = voucherService; this.customerService = customerService; - this.walletService = walletService; + this.managementService = managementService; } public void run() { @@ -166,26 +166,26 @@ private void allocate() { UUID customerId = typeCustomerId(); UUID voucherId = typeVoucherId(); - walletService.allocate(customerId, voucherId); + managementService.allocate(customerId, voucherId); } private void delete() { UUID customerId = typeCustomerId(); UUID voucherId = typeVoucherId(); - walletService.delete(customerId, voucherId); + managementService.delete(customerId, voucherId); } private void showVoucherByCustomer() { UUID customerId = typeCustomerId(); - List vouchers = walletService.searchVouchersFromCustomer(customerId); + List vouchers = managementService.searchVouchersFromCustomer(customerId); printList(vouchers); } private void showCustomerByVoucher() { UUID voucherId = typeVoucherId(); - List customers = walletService.searchCustomerFromVoucher(voucherId); + List customers = managementService.searchCustomerFromVoucher(voucherId); printList(customers); } diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletJdbcRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepository.java similarity index 79% rename from src/main/java/org/prgms/springbootbasic/repository/wallet/WalletJdbcRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepository.java index 6da874172e..5a4fe48316 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletJdbcRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository.wallet; +package org.prgms.springbootbasic.repository.customervouchermanagement; import org.prgms.springbootbasic.common.UtilMethod; import org.prgms.springbootbasic.domain.VoucherType; @@ -15,10 +15,10 @@ import static org.prgms.springbootbasic.common.UtilMethod.bytesToUUID; @Repository -public class WalletJdbcRepository implements WalletRepository { +public class CustomerVoucherManagementJdbcRepository implements CustomerVoucherManagementRepository { private final NamedParameterJdbcTemplate jdbcTemplate; - public WalletJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) { + public CustomerVoucherManagementJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @@ -26,21 +26,22 @@ public WalletJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) { @Override public void allocateVoucherById(UUID customerId, UUID voucherId) { - jdbcTemplate.update("INSERT INTO wallet VALUES (UNHEX(REPLACE(:customerId, '-', '')), UNHEX(REPLACE(:voucherId, '-', '')))", + jdbcTemplate.update("INSERT INTO customers_vouchers VALUES (UNHEX(REPLACE(:customerId, '-', '')), UNHEX(REPLACE(:voucherId, '-', '')))", toParamMap(customerId, voucherId)); } @Override public void deleteVoucherById(UUID customerId, UUID voucherId) { - jdbcTemplate.update("DELETE FROM wallet WHERE customer_id = UNHEX(REPLACE(:customerId, '-', '')) AND voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", + jdbcTemplate.update("DELETE FROM customers_vouchers " + + "WHERE customer_id = UNHEX(REPLACE(:customerId, '-', '')) AND voucher_id = UNHEX(REPLACE(:voucherId, '-', ''))", toParamMap(customerId, voucherId)); } @Override public List searchVouchersByCustomerId(UUID customerId) { return jdbcTemplate.query("SELECT v.voucher_id, v.discount_degree, v.voucher_type " + - "FROM vouchers v JOIN wallet w ON v.voucher_id = w.voucher_id " + - "WHERE w.customer_id = UNHEX(REPLACE(:customerId, '-', ''))", + "FROM vouchers v JOIN customers_vouchers w ON v.voucher_id = w.voucher_id " + + "WHERE w.customer_id = UNHEX(REPLACE(:customerId, '-', ''))", Collections.singletonMap("customerId", customerId.toString().getBytes()), mapToVoucher); } @@ -49,7 +50,7 @@ public List searchVouchersByCustomerId(UUID customerId) { public List searchCustomersByVoucherId(UUID voucherId) { return jdbcTemplate.query("SELECT c.customer_id, c.name, c.email, c.last_login_at, c.created_at, c.is_blacked " + "FROM customers c " + - "JOIN wallet w ON c.customer_id = w.customer_id " + + "JOIN customers_vouchers w ON c.customer_id = w.customer_id " + "WHERE w.voucher_id = UUID_TO_BIN(:voucherId)", Collections.singletonMap("voucherId", voucherId.toString().getBytes()), mapToCustomer); diff --git a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementRepository.java similarity index 76% rename from src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java rename to src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementRepository.java index 5ec03e60a0..175cad4a49 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/wallet/WalletRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementRepository.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.repository.wallet; +package org.prgms.springbootbasic.repository.customervouchermanagement; import org.prgms.springbootbasic.domain.customer.Customer; import org.prgms.springbootbasic.domain.voucher.Voucher; @@ -6,7 +6,7 @@ import java.util.List; import java.util.UUID; -public interface WalletRepository { +public interface CustomerVoucherManagementRepository { void allocateVoucherById(UUID customerId, UUID voucherId); void deleteVoucherById(UUID customerId, UUID voucherId); List searchVouchersByCustomerId(UUID customerId); diff --git a/src/main/java/org/prgms/springbootbasic/service/CustomerVoucherManagementService.java b/src/main/java/org/prgms/springbootbasic/service/CustomerVoucherManagementService.java new file mode 100644 index 0000000000..7dc1b42b03 --- /dev/null +++ b/src/main/java/org/prgms/springbootbasic/service/CustomerVoucherManagementService.java @@ -0,0 +1,36 @@ +package org.prgms.springbootbasic.service; + +import lombok.extern.slf4j.Slf4j; +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.Voucher; +import org.prgms.springbootbasic.repository.customervouchermanagement.CustomerVoucherManagementRepository; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.UUID; + +@Service +@Slf4j +public class CustomerVoucherManagementService { + private final CustomerVoucherManagementRepository managementRepository; + + public CustomerVoucherManagementService(CustomerVoucherManagementRepository managementRepository) { + this.managementRepository = managementRepository; + } + + public void allocate(UUID customerId, UUID voucherId){ + managementRepository.allocateVoucherById(customerId, voucherId); + } + + public void delete(UUID customerId, UUID voucherId){ + managementRepository.deleteVoucherById(customerId, voucherId); + } + + public List searchVouchersFromCustomer(UUID customerId){ + return managementRepository.searchVouchersByCustomerId(customerId); + } + + public List searchCustomerFromVoucher(UUID voucherId){ + return managementRepository.searchCustomersByVoucherId(voucherId); + } +} diff --git a/src/main/java/org/prgms/springbootbasic/service/WalletService.java b/src/main/java/org/prgms/springbootbasic/service/WalletService.java deleted file mode 100644 index 15d6fa8812..0000000000 --- a/src/main/java/org/prgms/springbootbasic/service/WalletService.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.prgms.springbootbasic.service; - -import org.prgms.springbootbasic.domain.customer.Customer; -import org.prgms.springbootbasic.domain.voucher.Voucher; -import org.prgms.springbootbasic.repository.wallet.WalletRepository; -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.UUID; - -@Service -public class WalletService { - private final WalletRepository walletRepository; - - public WalletService(WalletRepository walletRepository) { - this.walletRepository = walletRepository; - } - - public void allocate(UUID customerId, UUID voucherId){ - walletRepository.allocateVoucherById(customerId, voucherId); - } - - public void delete(UUID customerId, UUID voucherId){ - walletRepository.deleteVoucherById(customerId, voucherId); - } - - public List searchVouchersFromCustomer(UUID customerId){ - return walletRepository.searchVouchersByCustomerId(customerId); - } - - public List searchCustomerFromVoucher(UUID voucherId){ - return walletRepository.searchCustomersByVoucherId(voucherId); - } -} diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index dbf51628dd..f6ed7761a3 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS vouchers ( voucher_type varchar(64) NOT NULL ); -CREATE TABLE IF NOT EXISTS wallet ( +CREATE TABLE IF NOT EXISTS customers_vouchers ( -- 이 이름이 맞는지? 이건 wallet이라기 보다는 그냥 고객-바우처 매핑 테이블인데? customer_id BINARY(16) NOT NULL, voucher_id BINARY(16) NOT NULL, FOREIGN KEY (customer_id) references customers(customer_id), From 3b423d722a00cbd8356cb85f22eac5e10df79921 Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Thu, 2 Nov 2023 14:17:27 +0900 Subject: [PATCH 61/62] =?UTF-8?q?Test:=20=EB=B0=94=EC=9A=B0=EC=B2=98=20?= =?UTF-8?q?=EC=A7=80=EA=B0=91=EC=9D=98=20=EC=97=AD=ED=95=A0=EC=9D=84=20?= =?UTF-8?q?=ED=95=98=EB=8A=94=20Customer,=20Voucher=20=EA=B4=80=EB=A6=AC?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...stomerVoucherManagementJdbcRepository.java | 21 ++-- .../CustomerVoucherManagementRepository.java | 1 + ...erVoucherManagementJdbcRepositoryTest.java | 110 ++++++++++++++++++ 3 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepositoryTest.java diff --git a/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepository.java index 5a4fe48316..2e376dd00c 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepository.java @@ -22,8 +22,6 @@ public CustomerVoucherManagementJdbcRepository(NamedParameterJdbcTemplate jdbcTe this.jdbcTemplate = jdbcTemplate; } - - @Override public void allocateVoucherById(UUID customerId, UUID voucherId) { jdbcTemplate.update("INSERT INTO customers_vouchers VALUES (UNHEX(REPLACE(:customerId, '-', '')), UNHEX(REPLACE(:voucherId, '-', '')))", @@ -37,6 +35,11 @@ public void deleteVoucherById(UUID customerId, UUID voucherId) { toParamMap(customerId, voucherId)); } + @Override + public void deleteAll() { + jdbcTemplate.update("DELETE FROM customers_vouchers", Collections.emptyMap()); + } + @Override public List searchVouchersByCustomerId(UUID customerId) { return jdbcTemplate.query("SELECT v.voucher_id, v.discount_degree, v.voucher_type " + @@ -56,13 +59,6 @@ public List searchCustomersByVoucherId(UUID voucherId) { mapToCustomer); } - private Map toParamMap(UUID customerId, UUID voucherId){ - return new HashMap<>(){{ - put("customerId", customerId.toString().getBytes()); - put("voucherId", voucherId.toString().getBytes()); - }}; - } - private static RowMapper mapToVoucher = (rs, rowNum) -> { UUID voucherId = bytesToUUID(rs.getBytes("voucher_id")); long discountDegree = rs.getLong("discount_degree"); @@ -88,4 +84,11 @@ private Map toParamMap(UUID customerId, UUID voucherId){ return new Customer(customerId, customerName, email, lastLoginAt, createdAt, isBlacked); }; // static 메서드의 위치 조정. + private Map toParamMap(UUID customerId, UUID voucherId){ + return new HashMap<>(){{ + put("customerId", customerId.toString().getBytes()); + put("voucherId", voucherId.toString().getBytes()); + }}; + } + } diff --git a/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementRepository.java b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementRepository.java index 175cad4a49..a6ca57401f 100644 --- a/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementRepository.java +++ b/src/main/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementRepository.java @@ -9,6 +9,7 @@ public interface CustomerVoucherManagementRepository { void allocateVoucherById(UUID customerId, UUID voucherId); void deleteVoucherById(UUID customerId, UUID voucherId); + void deleteAll(); List searchVouchersByCustomerId(UUID customerId); List searchCustomersByVoucherId(UUID voucherId); } diff --git a/src/test/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepositoryTest.java b/src/test/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepositoryTest.java new file mode 100644 index 0000000000..38f4df68ac --- /dev/null +++ b/src/test/java/org/prgms/springbootbasic/repository/customervouchermanagement/CustomerVoucherManagementJdbcRepositoryTest.java @@ -0,0 +1,110 @@ +package org.prgms.springbootbasic.repository.customervouchermanagement; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.prgms.springbootbasic.domain.customer.Customer; +import org.prgms.springbootbasic.domain.voucher.FixedAmountPolicy; +import org.prgms.springbootbasic.domain.voucher.PercentDiscountPolicy; +import org.prgms.springbootbasic.domain.voucher.Voucher; +import org.prgms.springbootbasic.repository.customer.CustomerRepository; +import org.prgms.springbootbasic.repository.voucher.VoucherRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +@SpringBootTest +@ActiveProfiles("dev") +class CustomerVoucherManagementJdbcRepositoryTest { + @Autowired + private CustomerVoucherManagementRepository managementRepository; + @Autowired + private CustomerRepository customerRepository; + @Autowired + private VoucherRepository voucherRepository; + + private UUID setUpCustomerId; + private UUID setUpVoucherId; + + @BeforeEach + void setUp() { + setUpCustomerId = UUID.randomUUID(); + setUpVoucherId = UUID.randomUUID(); + customerRepository.upsert(new Customer(setUpCustomerId, "name", "email", LocalDateTime.now())); + voucherRepository.upsert(new Voucher(setUpVoucherId, 1000, new FixedAmountPolicy())); + } + + @AfterEach + void clean() { + managementRepository.deleteAll(); + customerRepository.deleteAll(); + voucherRepository.deleteAll(); + } + + @Test + @DisplayName("고객과 바우처가 제대로 매핑되는지 확인") + void allocateVoucherToCustomerById() { + UUID anotherVoucherId = UUID.randomUUID(); + voucherRepository.upsert(new Voucher(anotherVoucherId, 20, new PercentDiscountPolicy())); + + managementRepository.allocateVoucherById(setUpCustomerId, setUpVoucherId); + managementRepository.allocateVoucherById(setUpCustomerId, anotherVoucherId); + + List customers = managementRepository.searchCustomersByVoucherId(setUpVoucherId); + List vouchers = managementRepository.searchVouchersByCustomerId(setUpCustomerId); + + assertThat(customers, hasSize(1)); + assertThat(vouchers, hasSize(2)); + } + + @Test + @DisplayName("특정 고객과 특정 바우처 간 관계 제거") + void deleteRelationBetweenCustomerAndVoucherById() { + UUID anotherVoucherId = UUID.randomUUID(); + voucherRepository.upsert(new Voucher(anotherVoucherId, 100, new FixedAmountPolicy())); + + managementRepository.allocateVoucherById(setUpCustomerId, setUpVoucherId); + managementRepository.allocateVoucherById(setUpCustomerId, anotherVoucherId); + managementRepository.deleteVoucherById(setUpCustomerId, setUpVoucherId); + + List vouchers = managementRepository.searchVouchersByCustomerId(setUpCustomerId); + List customers = managementRepository.searchCustomersByVoucherId(setUpVoucherId); + + assertThat(vouchers, hasSize(1)); + assertThat(customers, hasSize(0)); + } + + @Test + @DisplayName("고객 id를 이용해 바우처들을 조회") + void searchVouchersRelatedToACustomerByCustomerId() { + managementRepository.allocateVoucherById(setUpCustomerId, setUpVoucherId); + + List vouchers = managementRepository.searchVouchersByCustomerId(setUpCustomerId); + List noVouchers = managementRepository.searchVouchersByCustomerId(UUID.randomUUID()); + + assertThat(vouchers, hasSize(1)); + assertThat(vouchers.get(0).getVoucherId(), is(setUpVoucherId)); + assertThat(noVouchers, hasSize(0)); + } + + @Test + @DisplayName("바우처 id를 통해 연관 고객들을 조회") + void searchCustomersRelatedToAVoucherByVoucherId() { + managementRepository.allocateVoucherById(setUpCustomerId, setUpVoucherId); + + List customers = managementRepository.searchCustomersByVoucherId(setUpVoucherId); + List noCustomers = managementRepository.searchCustomersByVoucherId(UUID.randomUUID()); + + assertThat(customers, hasSize(1)); + assertThat(customers.get(0).getCustomerId(), is(setUpCustomerId)); + assertThat(noCustomers, hasSize(0)); + } +} From 23ed012f984e6e5bc28d24183b27ea294d016fde Mon Sep 17 00:00:00 2001 From: ZZAMBAs Date: Thu, 2 Nov 2023 14:18:58 +0900 Subject: [PATCH 62/62] =?UTF-8?q?Rename:=20Console=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=EB=A5=BC=20=EB=94=B0=EB=A1=9C=20?= =?UTF-8?q?=EB=BA=80=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/prgms/springbootbasic/{common => }/console/Console.java | 2 +- .../org/prgms/springbootbasic/controller/MainController.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/org/prgms/springbootbasic/{common => }/console/Console.java (98%) diff --git a/src/main/java/org/prgms/springbootbasic/common/console/Console.java b/src/main/java/org/prgms/springbootbasic/console/Console.java similarity index 98% rename from src/main/java/org/prgms/springbootbasic/common/console/Console.java rename to src/main/java/org/prgms/springbootbasic/console/Console.java index e10f9c8dca..dcbd5793d5 100644 --- a/src/main/java/org/prgms/springbootbasic/common/console/Console.java +++ b/src/main/java/org/prgms/springbootbasic/console/Console.java @@ -1,4 +1,4 @@ -package org.prgms.springbootbasic.common.console; +package org.prgms.springbootbasic.console; import lombok.extern.slf4j.Slf4j; import org.prgms.springbootbasic.domain.VoucherType; diff --git a/src/main/java/org/prgms/springbootbasic/controller/MainController.java b/src/main/java/org/prgms/springbootbasic/controller/MainController.java index af2671b746..7abe4e3fb8 100644 --- a/src/main/java/org/prgms/springbootbasic/controller/MainController.java +++ b/src/main/java/org/prgms/springbootbasic/controller/MainController.java @@ -16,7 +16,7 @@ import java.util.NoSuchElementException; import java.util.UUID; -import static org.prgms.springbootbasic.common.console.Console.*; +import static org.prgms.springbootbasic.console.Console.*; @Controller @Slf4j