diff --git a/.gitignore b/.gitignore index 53a914e691..d9b737b40b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ .idea/**/dictionaries .idea/**/shelf +**/csv/** + # AWS User-specific .idea/**/aws.xml diff --git a/README.md b/README.md index 4fa860d2cb..dedab3860b 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,13 @@ 스프링부트 basic 위클리미션을 코드리뷰하는 Repository입니다. 주차별 과제는 데브코스 노션에서 확인하세요! + + +### 사용방법 +1. pull 받는다. +2. docker를 띄운다. +3. `docker build -t mysql-image .` +4. `docker run -dit --name mysql-container -p 3300:3306 mysql-image` +5. docker 올라온 것을 확인 후 프로젝트 실행 + +기존 3000포트 -> 3300포트로 변경(react 충돌) diff --git a/build.gradle b/build.gradle index b2f544c651..8944c5731a 100644 --- a/build.gradle +++ b/build.gradle @@ -18,10 +18,9 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-jdbc' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:3.1.5' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'com.mysql:mysql-connector-j:8.1.0' - // https://mvnrepository.com/artifact/com.wix/wix-embedded-mysql - testImplementation 'com.wix:wix-embedded-mysql:4.6.1' } tasks.named('test') { diff --git a/mysql/init.sql b/mysql/init.sql index 7f719edf55..a7ba5847f7 100644 --- a/mysql/init.sql +++ b/mysql/init.sql @@ -6,6 +6,8 @@ CREATE TABLE customers ( customer_id BINARY(16), customer_name VARCHAR(30), customer_type VARCHAR(30), + created_at datetime(6) not null , + updated_at datetime(6) DEFAULT null, CONSTRAINT customer_pk PRIMARY KEY(customer_id) ); @@ -14,6 +16,8 @@ CREATE TABLE vouchers ( discount_value BIGINT, voucher_type VARCHAR(30), customer_id BINARY(16), + created_at datetime(6) not null , + updated_at datetime(6) DEFAULT null, CONSTRAINT voucher_pk PRIMARY KEY(voucher_id), CONSTRAINT voucher_fk FOREIGN KEY (customer_id) REFERENCES customers (customer_id) ); @@ -26,6 +30,8 @@ CREATE TABLE customers ( customer_id BINARY(16), customer_name VARCHAR(30), customer_type VARCHAR(30), + created_at datetime(6) not null , + updated_at datetime(6) DEFAULT null, CONSTRAINT customer_pk PRIMARY KEY(customer_id) ); @@ -34,6 +40,8 @@ CREATE TABLE vouchers ( discount_value BIGINT, voucher_type VARCHAR(30), customer_id BINARY(16), + created_at datetime(6) not null , + updated_at datetime(6) DEFAULT null, CONSTRAINT voucher_pk PRIMARY KEY(voucher_id), CONSTRAINT voucher_fk FOREIGN KEY (customer_id) REFERENCES customers (customer_id) ); \ No newline at end of file diff --git a/src/main/java/org/programmers/springorder/HomeController.java b/src/main/java/org/programmers/springorder/HomeController.java new file mode 100644 index 0000000000..38eac24f8b --- /dev/null +++ b/src/main/java/org/programmers/springorder/HomeController.java @@ -0,0 +1,12 @@ +package org.programmers.springorder; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class HomeController { + @GetMapping("/") + public String home(){ + return "index"; + } +} diff --git a/src/main/java/org/programmers/springorder/SpringOrderApplication.java b/src/main/java/org/programmers/springorder/SpringOrderApplication.java index fea496bebc..805501cc18 100644 --- a/src/main/java/org/programmers/springorder/SpringOrderApplication.java +++ b/src/main/java/org/programmers/springorder/SpringOrderApplication.java @@ -1,17 +1,21 @@ package org.programmers.springorder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.List; @SpringBootApplication -public class SpringOrderApplication { - private static final Logger log = LoggerFactory.getLogger(SpringOrderApplication.class); +public class SpringOrderApplication implements WebMvcConfigurer { - public static void main(String[] args) { - log.info("Voucher 관리 애플리케이션 구동"); - SpringApplication.run(SpringOrderApplication.class, args).close(); - } + public static void main(String[] args) { + SpringApplication.run(SpringOrderApplication.class, args); + } + @Override + public void configureMessageConverters(List> converters) { + WebMvcConfigurer.super.configureMessageConverters(converters); + } } diff --git a/src/main/java/org/programmers/springorder/VoucherApplication.java b/src/main/java/org/programmers/springorder/VoucherApplication.java index 774fe33a6d..7b95cd9632 100644 --- a/src/main/java/org/programmers/springorder/VoucherApplication.java +++ b/src/main/java/org/programmers/springorder/VoucherApplication.java @@ -1,30 +1,38 @@ package org.programmers.springorder; import org.programmers.springorder.console.Console; +import org.programmers.springorder.consts.ErrorMessage; import org.programmers.springorder.consts.Message; import org.programmers.springorder.customer.controller.CustomerController; import org.programmers.springorder.utils.MenuType; -import org.programmers.springorder.voucher.controller.VoucherController; +import org.programmers.springorder.voucher.controller.VoucherConsoleController; import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; +@Profile("test") @Component public class VoucherApplication implements CommandLineRunner { private final Console console; - private final VoucherController voucherController; + private final VoucherConsoleController voucherConsoleController; private final CustomerController customerController; - public VoucherApplication(Console console, VoucherController voucherController, CustomerController customerController) { + public VoucherApplication(Console console, VoucherConsoleController voucherConsoleController, CustomerController customerController) { this.console = console; - this.voucherController = voucherController; + this.voucherConsoleController = voucherConsoleController; this.customerController = customerController; } @Override public void run(String... args) { - boolean isRunning = true; + boolean isRunning = true; + boolean isWeb = false; + if(chooseMedia()){ + isWeb = true; + }; while(isRunning) { + if(isWeb) break; MenuType menu = console.inputMenu(); switch (menu) { @@ -32,15 +40,26 @@ public void run(String... args) { isRunning = false; console.printMessage(Message.EXIT_PROGRAM_MESSAGE); } - case CREATE -> voucherController.createVoucher(); - case LIST -> voucherController.getVoucherList(); + case CREATE -> voucherConsoleController.createVoucher(); + case LIST -> voucherConsoleController.getVoucherList(); case BLACK -> customerController.printBlackList(); - case ALLOCATE -> voucherController.giveVoucher(); - case GET_OWNER_VOUCHER -> voucherController.getVouchersOfOwner(); - case DELETE_VOUCHER -> voucherController.deleteVoucher(); + case ALLOCATE -> voucherConsoleController.giveVoucher(); + case GET_OWNER_VOUCHER -> voucherConsoleController.getVouchersOfOwner(); + case DELETE_VOUCHER -> voucherConsoleController.deleteVoucher(); case SEARCH_VOUCHER_OWNER -> customerController.getVoucherOwner(); } } } + private boolean chooseMedia() { + String media = console.chooseMedia(); + if(media.equals("1")){ + return true; + } + if(media.equals("2")){ + return false; + }; + throw new RuntimeException(ErrorMessage.INVALID_VALUE_MESSAGE); + } + } diff --git a/src/main/java/org/programmers/springorder/console/Console.java b/src/main/java/org/programmers/springorder/console/Console.java index 856f9a6352..4c640c4ff6 100644 --- a/src/main/java/org/programmers/springorder/console/Console.java +++ b/src/main/java/org/programmers/springorder/console/Console.java @@ -15,7 +15,6 @@ import java.util.InputMismatchException; import java.util.List; -import java.util.Optional; import java.util.UUID; @Component @@ -122,4 +121,9 @@ public UUID getVoucherId() { public void showCustomer(CustomerResponseDto customer) { printMessage(customer.toString()); } + + public String chooseMedia() { + printMessage(Message.CHOOSING_MEDIA); + return input.getInput(); + } } diff --git a/src/main/java/org/programmers/springorder/console/Input.java b/src/main/java/org/programmers/springorder/console/Input.java index 4facaee99e..298ee57a66 100644 --- a/src/main/java/org/programmers/springorder/console/Input.java +++ b/src/main/java/org/programmers/springorder/console/Input.java @@ -1,6 +1,5 @@ package org.programmers.springorder.console; -import org.programmers.springorder.utils.Validation; import java.util.Scanner; diff --git a/src/main/java/org/programmers/springorder/consts/ErrorMessage.java b/src/main/java/org/programmers/springorder/consts/ErrorMessage.java index 219e171bdc..bc59578980 100644 --- a/src/main/java/org/programmers/springorder/consts/ErrorMessage.java +++ b/src/main/java/org/programmers/springorder/consts/ErrorMessage.java @@ -4,6 +4,8 @@ public class ErrorMessage { private ErrorMessage(){} + public static final String CUSTOMER_NOT_FOUND = "등록된 회원이 존재하지 않습니다."; + public static final String VOUCHER_NOT_EXIST_MESSAGE = "등록된 바우처가 존재하지 않습니다."; public static final String EMPTY_VALUE_MESSAGE = "값이 존재하지 않습니다. 다시 입력해주세요."; diff --git a/src/main/java/org/programmers/springorder/consts/Message.java b/src/main/java/org/programmers/springorder/consts/Message.java index 6714e0efc7..c8242a649d 100644 --- a/src/main/java/org/programmers/springorder/consts/Message.java +++ b/src/main/java/org/programmers/springorder/consts/Message.java @@ -2,6 +2,12 @@ public class Message { + public static final String CHOOSING_MEDIA = """ + 어떤 방식으로 구동하시겠습니까? + 1. web 방식 + 2. console 방식 + """; + private Message(){} public static final String MENU_SELECT_MESSAGE = """ === Voucher Program === diff --git a/src/main/java/org/programmers/springorder/customer/controller/CustomerController.java b/src/main/java/org/programmers/springorder/customer/controller/CustomerController.java index eb4a51f8aa..831ee428d6 100644 --- a/src/main/java/org/programmers/springorder/customer/controller/CustomerController.java +++ b/src/main/java/org/programmers/springorder/customer/controller/CustomerController.java @@ -2,10 +2,12 @@ import org.programmers.springorder.console.Console; import org.programmers.springorder.customer.service.CustomerService; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Controller; import java.util.UUID; +@Profile("test") @Controller public class CustomerController { private final Console console; diff --git a/src/main/java/org/programmers/springorder/customer/controller/CustomerPageController.java b/src/main/java/org/programmers/springorder/customer/controller/CustomerPageController.java new file mode 100644 index 0000000000..ebdbde3957 --- /dev/null +++ b/src/main/java/org/programmers/springorder/customer/controller/CustomerPageController.java @@ -0,0 +1,64 @@ +package org.programmers.springorder.customer.controller; + +import org.programmers.springorder.customer.dto.CustomerRequestDto; +import org.programmers.springorder.customer.dto.CustomerResponseDto; +import org.programmers.springorder.customer.service.CustomerService; +import org.programmers.springorder.voucher.dto.VoucherResponseDto; +import org.programmers.springorder.voucher.service.VoucherService; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +import java.util.UUID; + +@Profile("prod") +@Controller +public class CustomerPageController { + + private final CustomerService customerService; + private final VoucherService voucherService; + + public CustomerPageController(CustomerService customerService, VoucherService voucherService) { + this.customerService = customerService; + this.voucherService = voucherService; + } + + @GetMapping("/customers") + public String getCustomerListPage(Model model){ + List allCustomers = customerService.getAllCustomers(); + model.addAttribute("customerList", allCustomers); + return "customers"; + } + + @GetMapping("/blackCustomers") + public String getBlackCustomerListPage(Model model){ + List allCustomers = customerService.getBlackList(); + model.addAttribute("customerList", allCustomers); + return "customers"; + } + + @GetMapping("/customers/{customerId}") + public String getCustomerListPage(@PathVariable UUID customerId, Model model){ + CustomerResponseDto customer = customerService.findCustomer(customerId); + List customerOwnedVouchers = voucherService.getCustomerOwnedVouchers(customerId); + model.addAttribute("customer", customer); + model.addAttribute("customerVoucherList", customerOwnedVouchers); + return "customer-detail"; + } + + @GetMapping("/new-customer") + public String getNewCustomerPage(){ + return "new-customer"; + } + + @PostMapping("/customers") + public String enrollCustomer(CustomerRequestDto requestDto){ + customerService.newCustomer(requestDto); + return "redirect:/customers"; + } + +} diff --git a/src/main/java/org/programmers/springorder/customer/controller/CustomerRestController.java b/src/main/java/org/programmers/springorder/customer/controller/CustomerRestController.java new file mode 100644 index 0000000000..df5dae08f7 --- /dev/null +++ b/src/main/java/org/programmers/springorder/customer/controller/CustomerRestController.java @@ -0,0 +1,49 @@ +package org.programmers.springorder.customer.controller; + +import org.programmers.springorder.customer.dto.CustomerRequestDto; +import org.programmers.springorder.customer.dto.CustomerResponseDto; +import org.programmers.springorder.customer.service.CustomerService; +import org.programmers.springorder.voucher.dto.Response; +import org.springframework.context.annotation.Profile; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.UUID; + +@Profile("default") +@RestController +public class CustomerRestController { + + private final CustomerService customerService; + + public CustomerRestController(CustomerService customerService) { + this.customerService = customerService; + } + + @GetMapping("api/v1/customers") + public Response> getCustomerListPage(){ + return Response.success(customerService.getAllCustomers()); + } + + @GetMapping("api/v1/blackCustomers") + public Response> getBlackCustomerListPage(){ + return Response.success(customerService.getBlackList()); + } + + @GetMapping("api/v1/customers/{customerId}") + public Response getCustomerDetail(@PathVariable UUID customerId, Model model){ + return Response.success(customerService.findCustomer(customerId)); + } + + + @PostMapping("api/v1/customers") + public Response enrollCustomer(CustomerRequestDto requestDto){ + customerService.newCustomer(requestDto); + return Response.success(); + } + +} diff --git a/src/main/java/org/programmers/springorder/customer/dto/CustomerRequestDto.java b/src/main/java/org/programmers/springorder/customer/dto/CustomerRequestDto.java new file mode 100644 index 0000000000..3e2c6c0cb3 --- /dev/null +++ b/src/main/java/org/programmers/springorder/customer/dto/CustomerRequestDto.java @@ -0,0 +1,9 @@ +package org.programmers.springorder.customer.dto; + +import org.programmers.springorder.customer.model.CustomerType; +import org.springframework.lang.NonNull; + +public record CustomerRequestDto( + @NonNull String name, + @NonNull CustomerType customerType) { +} diff --git a/src/main/java/org/programmers/springorder/customer/dto/CustomerResponseDto.java b/src/main/java/org/programmers/springorder/customer/dto/CustomerResponseDto.java index 59a8685cd4..c4ec069a28 100644 --- a/src/main/java/org/programmers/springorder/customer/dto/CustomerResponseDto.java +++ b/src/main/java/org/programmers/springorder/customer/dto/CustomerResponseDto.java @@ -24,6 +24,14 @@ public UUID getCustomerId() { return customerId; } + public String getName() { + return name; + } + + public String getCustomerType() { + return customerType; + } + @Override public String toString() { return "customerId : " + customerId +'\n' + diff --git a/src/main/java/org/programmers/springorder/customer/model/Customer.java b/src/main/java/org/programmers/springorder/customer/model/Customer.java index 632b9b8c39..7cf31a7d57 100644 --- a/src/main/java/org/programmers/springorder/customer/model/Customer.java +++ b/src/main/java/org/programmers/springorder/customer/model/Customer.java @@ -1,5 +1,6 @@ package org.programmers.springorder.customer.model; +import java.time.LocalDateTime; import java.util.Objects; import java.util.UUID; @@ -7,17 +8,33 @@ public class Customer { private final UUID customerId; private final String name; private final CustomerType customerType; + private final LocalDateTime createdAt; + private final LocalDateTime updatedAt; private Customer(UUID customerId, String name, CustomerType customerType) { this.customerId = customerId; this.name = name; this.customerType = customerType; + this.createdAt = LocalDateTime.now(); + this.updatedAt = this.createdAt; } - public static Customer toCustomer(UUID customerId, String name, CustomerType customerType) { + private Customer(UUID customerId, String name, CustomerType customerType, LocalDateTime createdAt, LocalDateTime updatedAt) { + this.customerId = customerId; + this.name = name; + this.customerType = customerType; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + + public static Customer toNewCustomer(UUID customerId, String name, CustomerType customerType) { return new Customer(customerId, name, customerType); } + public static Customer fromDbCustomer(UUID customerId, String name, CustomerType customerType, LocalDateTime createdAt, LocalDateTime updatedAt) { + return new Customer(customerId, name, customerType, createdAt, updatedAt); + } + public boolean sameCustomerId(UUID customerId){ return this.customerId.equals(customerId); } @@ -29,7 +46,9 @@ public String insertCustomerDataInFile() { StringBuilder data = new StringBuilder(); data.append(this.customerId).append(",") .append(this.name).append(",") - .append(this.customerType.name()); + .append(this.customerType.name()).append(",") + .append(this.createdAt).append(",") + .append(this.updatedAt); return data.toString(); } @@ -45,15 +64,34 @@ public CustomerType getCustomerType() { return customerType; } + public LocalDateTime getCreatedAt() { + return createdAt; + } + + public LocalDateTime getUpdatedAt() { + return updatedAt; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Customer customer)) return false; - return Objects.equals(customerId, customer.customerId) && Objects.equals(name, customer.name) && customerType == customer.customerType; + return Objects.equals(customerId, customer.customerId) && Objects.equals(name, customer.name) && customerType == customer.customerType && Objects.equals(createdAt, customer.createdAt) && Objects.equals(updatedAt, customer.updatedAt); } @Override public int hashCode() { - return Objects.hash(customerId, name, customerType); + return Objects.hash(customerId, name, customerType, createdAt, updatedAt); + } + + @Override + public String toString() { + return "Customer{" + + "customerId=" + customerId + + ", name='" + name + '\'' + + ", customerType=" + customerType + + ", createdAt=" + createdAt + + ", updatedAt=" + updatedAt + + '}'; } } diff --git a/src/main/java/org/programmers/springorder/customer/repository/FileCustomerRepository.java b/src/main/java/org/programmers/springorder/customer/repository/FileCustomerRepository.java index cbc4aa7338..8af9d6a622 100644 --- a/src/main/java/org/programmers/springorder/customer/repository/FileCustomerRepository.java +++ b/src/main/java/org/programmers/springorder/customer/repository/FileCustomerRepository.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Repository; import java.io.*; +import java.time.LocalDateTime; import java.util.*; @Profile({"dev", "test"}) @@ -41,7 +42,9 @@ public List findAll() { UUID customerId = UUID.fromString(data[0]); String name = data[1]; CustomerType customerType = CustomerType.valueOf(data[2]); - Customer customer = Customer.toCustomer(customerId, name, customerType); + LocalDateTime createdAt = LocalDateTime.parse(data[3]); + LocalDateTime updatedAt = LocalDateTime.parse(data[3]); + Customer customer = Customer.fromDbCustomer(customerId, name, customerType, createdAt, updatedAt); customerList.add(customer); } } catch (FileNotFoundException e) { @@ -55,7 +58,7 @@ public List findAll() { return Collections.unmodifiableList(customerList); } - //TODO: 추후 구현 예정 + @Override public Optional findByID(UUID customerId) { return findAll().stream() diff --git a/src/main/java/org/programmers/springorder/customer/repository/JdbcCustomerRepository.java b/src/main/java/org/programmers/springorder/customer/repository/JdbcCustomerRepository.java index 0a3dd52d8a..46544510a6 100644 --- a/src/main/java/org/programmers/springorder/customer/repository/JdbcCustomerRepository.java +++ b/src/main/java/org/programmers/springorder/customer/repository/JdbcCustomerRepository.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Repository; import java.nio.ByteBuffer; +import java.time.LocalDateTime; import java.util.*; @Profile("default") @@ -18,7 +19,7 @@ public class JdbcCustomerRepository implements CustomerRepository { private final NamedParameterJdbcTemplate jdbcTemplate; private final String SELECT_ALL_CUSTOMER = "SELECT * FROM customers"; - private final String INSERT_CUSTOMER = "INSERT INTO customers(customer_id, customer_name, customer_type) VALUES(UUID_TO_BIN(:customerId), :customerName, :customerType)"; + private final String INSERT_CUSTOMER = "INSERT INTO customers(customer_id, customer_name, customer_type, created_at, updated_at) VALUES(UUID_TO_BIN(:customerId), :customerName, :customerType, :createdAt, :updatedAt)"; private final String FIND_BLACKLIST = "SELECT * FROM customers where customer_type = 'BLACK'"; private final String FIND_BY_CUSTOMER_ID = "SELECT * FROM customers where customer_id = UUID_TO_BIN(:customerId)"; @@ -30,7 +31,9 @@ public JdbcCustomerRepository(NamedParameterJdbcTemplate jdbcTemplate) { UUID customerId = toUUID(resultSet.getBytes("customer_id")); String customerName = resultSet.getString("customer_name"); CustomerType customerType = CustomerType.valueOf(resultSet.getString("customer_type")); - return Customer.toCustomer(customerId, customerName, customerType); + LocalDateTime createdAt = resultSet.getTimestamp("created_at").toLocalDateTime(); + LocalDateTime updatedAt = resultSet.getTimestamp("created_at").toLocalDateTime(); + return Customer.fromDbCustomer(customerId, customerName, customerType, createdAt, updatedAt); }; @Override @@ -63,21 +66,24 @@ public Customer insert(Customer customer) { return customer; } - public void clear(){ + public void clear() { jdbcTemplate.getJdbcOperations().update("delete from customers"); } + private Map toParamMap(Customer customer) { - return new HashMap<>() {{ - put("customerId", customer.getCustomerId().toString().getBytes()); - put("customerName", customer.getName()); - put("customerType", customer.getCustomerType().name()); - }}; + Map map = new HashMap<>(); + map.put("customerId", customer.getCustomerId().toString().getBytes()); + map.put("customerName", customer.getName()); + map.put("customerType", customer.getCustomerType().name()); + map.put("createdAt", customer.getCreatedAt()); + map.put("updatedAt", customer.getCreatedAt()); + return map; } private Map findByIdMap(UUID customerId) { - return new HashMap<>() {{ - put("customerId", customerId.toString().getBytes()); - }}; + Map map = new HashMap<>(); + map.put("customerId", customerId.toString().getBytes()); + return map; } static UUID toUUID(byte[] bytes) { diff --git a/src/main/java/org/programmers/springorder/customer/service/CustomerService.java b/src/main/java/org/programmers/springorder/customer/service/CustomerService.java index 21c5a3442e..e2814ac5a1 100644 --- a/src/main/java/org/programmers/springorder/customer/service/CustomerService.java +++ b/src/main/java/org/programmers/springorder/customer/service/CustomerService.java @@ -1,7 +1,11 @@ package org.programmers.springorder.customer.service; +import org.programmers.springorder.customer.dto.CustomerRequestDto; import org.programmers.springorder.customer.dto.CustomerResponseDto; +import org.programmers.springorder.customer.model.Customer; import org.programmers.springorder.customer.repository.CustomerRepository; +import org.programmers.springorder.exception.ErrorCode; +import org.programmers.springorder.exception.VoucherException; import org.programmers.springorder.voucher.model.Voucher; import org.programmers.springorder.voucher.repository.VoucherRepository; import org.springframework.stereotype.Service; @@ -29,12 +33,33 @@ public List getBlackList() { public CustomerResponseDto findOwnerOfVoucher(UUID voucherId){ Voucher voucher = voucherRepository.findById(voucherId) - .orElseThrow(() -> new RuntimeException("찾으시는 voucher가 존재하지 않습니다.")); + .orElseThrow(() -> new VoucherException(ErrorCode.VOUCHER_NOT_FOUND)); if(voucher.getCustomerId() == null) { - throw new RuntimeException("해당 voucher는 주인이 존재하지 않습니다."); + throw new VoucherException(ErrorCode.VOUCHER_OWNER_NOT_EXIST); } return customerRepository.findByID(voucher.getCustomerId()) .map(CustomerResponseDto::of) - .orElseThrow(() -> new RuntimeException("해당 고객을 찾을 수 없습니다.")); + .orElseThrow(() -> new VoucherException(ErrorCode.VOUCHER_NOT_FOUND)); + } + + public List getAllCustomers() { + return customerRepository.findAll() + .stream() + .map(CustomerResponseDto::of) + .toList(); + } + + public void newCustomer(CustomerRequestDto requestDto) { + Customer customer = Customer.toNewCustomer( + UUID.randomUUID(), + requestDto.name(), + requestDto.customerType()); + customerRepository.insert(customer); + } + + public CustomerResponseDto findCustomer(UUID customerId){ + return customerRepository.findByID(customerId) + .map(CustomerResponseDto::of) + .orElseThrow(() -> new VoucherException(ErrorCode.CUSTOMER_NOT_FOUND)); } } diff --git a/src/main/java/org/programmers/springorder/exception/ErrorCode.java b/src/main/java/org/programmers/springorder/exception/ErrorCode.java new file mode 100644 index 0000000000..6023dbfb41 --- /dev/null +++ b/src/main/java/org/programmers/springorder/exception/ErrorCode.java @@ -0,0 +1,29 @@ +package org.programmers.springorder.exception; + +import org.springframework.http.HttpStatus; + +public enum ErrorCode { + SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 장애로 인해 잠시 문제가 발생하였습니다." ), + VOUCHER_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 바우처를 찾을 수 없습니다."), + CUSTOMER_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 고객을 찾을 수 없습니다."), + VOUCHER_OWNER_NOT_EXIST(HttpStatus.NOT_FOUND, "해당 바우처를 소유한 고객이 없습니다."), + INVALID_DISCOUNT_VALUE(HttpStatus.BAD_REQUEST, "잘못된 고정 할인 금액입니다."), + INVALID_DISCOUNT_PERCENT(HttpStatus.BAD_REQUEST, "잘못된 할인율입니다."), + + INVALID_INPUT(HttpStatus.BAD_REQUEST, "잘못된 입력입니다.") ; + private final HttpStatus httpStatus; + private final String message; + + ErrorCode(HttpStatus httpStatus, String message) { + this.httpStatus = httpStatus; + this.message = message; + } + + public HttpStatus getHttpStatus() { + return httpStatus; + } + + public String getMessage() { + return this.message; + } +} diff --git a/src/main/java/org/programmers/springorder/exception/PageExceptionHandler.java b/src/main/java/org/programmers/springorder/exception/PageExceptionHandler.java new file mode 100644 index 0000000000..9f9337d448 --- /dev/null +++ b/src/main/java/org/programmers/springorder/exception/PageExceptionHandler.java @@ -0,0 +1,20 @@ +package org.programmers.springorder.exception; + +import org.springframework.context.annotation.Profile; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@Profile("prod") +@ControllerAdvice +public class PageExceptionHandler { + + @ExceptionHandler(VoucherException.class) + public String applicationHandler(VoucherException e){ + return "no-voucher"; + } + + @ExceptionHandler(RuntimeException.class) + public String applicationHandler(RuntimeException e){ + return "no-voucher"; + } +} diff --git a/src/main/java/org/programmers/springorder/exception/RestExceptionHandler.java b/src/main/java/org/programmers/springorder/exception/RestExceptionHandler.java new file mode 100644 index 0000000000..6aa2ecd902 --- /dev/null +++ b/src/main/java/org/programmers/springorder/exception/RestExceptionHandler.java @@ -0,0 +1,35 @@ +package org.programmers.springorder.exception; + +import jakarta.servlet.http.HttpServletRequest; +import org.programmers.springorder.voucher.dto.Response; +import org.springframework.context.annotation.Profile; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Profile("default") +@RestControllerAdvice +public class RestExceptionHandler { + + + @ExceptionHandler(VoucherException.class) + public ResponseEntity applicationHandler(VoucherException e){ + return ResponseEntity.status( + e.getErrorCode().getHttpStatus()) + .body(e.getMessage()); + } + + @ExceptionHandler(RuntimeException.class) + public ResponseEntity applicationHandler(RuntimeException e){ + return ResponseEntity.status(ErrorCode.SERVER_ERROR.getHttpStatus()) + .body(Response.error(ErrorCode.SERVER_ERROR.getMessage())); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity methodValidException(MethodArgumentNotValidException e, HttpServletRequest request){ + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ErrorCode.INVALID_INPUT.getMessage()); + } +} diff --git a/src/main/java/org/programmers/springorder/exception/VoucherException.java b/src/main/java/org/programmers/springorder/exception/VoucherException.java new file mode 100644 index 0000000000..a1893bf887 --- /dev/null +++ b/src/main/java/org/programmers/springorder/exception/VoucherException.java @@ -0,0 +1,20 @@ +package org.programmers.springorder.exception; + +public class VoucherException extends RuntimeException{ + private final ErrorCode errorCode; + private final String message; + + public VoucherException(ErrorCode errorCode){ + this.errorCode = errorCode; + this.message = errorCode.getMessage(); + } + + public ErrorCode getErrorCode() { + return errorCode; + } + + @Override + public String getMessage() { + return message; + } +} diff --git a/src/main/java/org/programmers/springorder/voucher/controller/VoucherController.java b/src/main/java/org/programmers/springorder/voucher/controller/VoucherConsoleController.java similarity index 80% rename from src/main/java/org/programmers/springorder/voucher/controller/VoucherController.java rename to src/main/java/org/programmers/springorder/voucher/controller/VoucherConsoleController.java index 57542e2631..3ef5d570e7 100644 --- a/src/main/java/org/programmers/springorder/voucher/controller/VoucherController.java +++ b/src/main/java/org/programmers/springorder/voucher/controller/VoucherConsoleController.java @@ -5,17 +5,19 @@ import org.programmers.springorder.voucher.dto.GiveVoucherRequestDto; import org.programmers.springorder.voucher.dto.VoucherRequestDto; import org.programmers.springorder.voucher.service.VoucherService; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Controller; import java.util.UUID; +@Profile("test") @Controller -public class VoucherController { +public class VoucherConsoleController { private final Console console; private final VoucherService voucherService; - public VoucherController(Console console, VoucherService voucherService) { + public VoucherConsoleController(Console console, VoucherService voucherService) { this.console = console; this.voucherService = voucherService; } @@ -26,13 +28,13 @@ public void getVoucherList() { public void createVoucher() { VoucherRequestDto request = console.inputVoucherInfo(); - voucherService.save(request); + voucherService.saveNewVoucher(request); console.printMessage(Message.VOUCHER_REGISTERED); } public void giveVoucher(){ GiveVoucherRequestDto requestDto = console.giveVoucherInfo(); - voucherService.update(requestDto.getVoucherId(), requestDto.getCustomerId()); + voucherService.allocateVoucher(requestDto.voucherId(), requestDto.customerId()); console.printMessage(Message.VOUCHER_ALLOCATED); } diff --git a/src/main/java/org/programmers/springorder/voucher/controller/VoucherPageController.java b/src/main/java/org/programmers/springorder/voucher/controller/VoucherPageController.java new file mode 100644 index 0000000000..3eaedf7c09 --- /dev/null +++ b/src/main/java/org/programmers/springorder/voucher/controller/VoucherPageController.java @@ -0,0 +1,86 @@ +package org.programmers.springorder.voucher.controller; + +import jakarta.servlet.http.HttpServletRequest; +import org.programmers.springorder.voucher.dto.VoucherRequestDto; +import org.programmers.springorder.voucher.dto.VoucherResponseDto; +import org.programmers.springorder.voucher.service.VoucherService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@Profile("prod") +@Controller +public class VoucherPageController { + private static final Logger log = LoggerFactory.getLogger(VoucherPageController.class); + + private final VoucherService voucherService; + + public VoucherPageController(VoucherService voucherService) { + this.voucherService = voucherService; + } + + @GetMapping("/vouchers") + public String getVoucherList(Model model) { + List allVoucher = voucherService.getAllVoucher(); + model.addAttribute("voucherList", allVoucher); + return "vouchers"; + } + + @GetMapping("/new-voucher") + public String getNewVoucherPage(){ + return "new-voucher"; + } + + @PostMapping("/vouchers") + public String createVoucher(VoucherRequestDto voucherRequestDto) { + voucherService.saveNewVoucher(voucherRequestDto); + return "redirect:/vouchers"; + } + + @GetMapping("/vouchers/{voucherId}") + public String getVoucherDetail(@PathVariable UUID voucherId, Model model, HttpServletRequest request){ + try { + VoucherResponseDto voucher = voucherService.getVoucherById(voucherId); + model.addAttribute("voucher", voucher); + return "voucher-detail"; + }catch (RuntimeException e){ + return "no-voucher"; + } + } + + @DeleteMapping("/vouchers") + public String deleteVoucher(@RequestParam(name = "voucherId", required = false) UUID voucherId){ + try { + voucherService.deleteVoucher(voucherId); + return "redirect:/vouchers"; + } catch (RuntimeException e){ + return "no-voucher"; + } + } + + @GetMapping("/voucherAllocate/{voucherId}") + public String getVoucherAllocatePage(@PathVariable UUID voucherId, Model model){ + try{ + model.addAttribute("voucherId", voucherId); + return "voucher-allocate"; + }catch (RuntimeException e){ + return "no-voucher"; + } + } + @PostMapping("/voucherAllocate/{voucherId}") + public String giveVoucher(@PathVariable UUID voucherId, @RequestParam UUID customerId){ + try { + voucherService.allocateVoucher(voucherId, customerId); + return "redirect:/vouchers"; + } catch (RuntimeException e){ + return "no-voucher"; + } + } +} + diff --git a/src/main/java/org/programmers/springorder/voucher/controller/VoucherRestController.java b/src/main/java/org/programmers/springorder/voucher/controller/VoucherRestController.java new file mode 100644 index 0000000000..4e43ef3f53 --- /dev/null +++ b/src/main/java/org/programmers/springorder/voucher/controller/VoucherRestController.java @@ -0,0 +1,63 @@ +package org.programmers.springorder.voucher.controller; + +import org.programmers.springorder.voucher.dto.Response; +import org.programmers.springorder.voucher.dto.VoucherRequestDto; +import org.programmers.springorder.voucher.dto.VoucherResponseDto; +import org.programmers.springorder.voucher.service.VoucherService; +import org.springframework.context.annotation.Profile; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +@Profile("default") +@RestController +public class VoucherRestController { + private final VoucherService voucherService; + + public VoucherRestController(VoucherService voucherService) { + this.voucherService = voucherService; + } + + @GetMapping("api/v1/vouchers") + public Response> getVoucherList( + @RequestParam(required = false) LocalDateTime startedAt, + @RequestParam(required = false) LocalDateTime endedAt + ) { + if (startedAt != null && endedAt != null){ + Response.success(voucherService.getAllVoucherByTime(startedAt, endedAt)); + } + return Response.success(voucherService.getAllVoucher()); + } + + @PostMapping("api/v1/vouchers") + public Response createVoucher(VoucherRequestDto voucherRequestDto) { + voucherService.saveNewVoucher(voucherRequestDto); + return Response.success(); + } + + @GetMapping("api/v1/vouchers/{voucherId}") + public Response getVoucherDetail(@PathVariable UUID voucherId) { + return Response.success(voucherService.getVoucherById(voucherId)); + } + + @DeleteMapping("api/v1/vouchers/{voucherId}") + public Response deleteVoucher(@PathVariable UUID voucherId) { + voucherService.deleteVoucher(voucherId); + return Response.success(); + } + + @PostMapping("api/v1/voucher/{voucherId}/allocate") + public Response giveVoucher(@PathVariable UUID voucherId, @RequestParam UUID customerId) { + voucherService.allocateVoucher(voucherId, customerId); + return Response.success(); + } + + @GetMapping("api/v1/vouchers/{customerId}/vouchers") + public Response> getCustomerOwnVoucherList(@PathVariable UUID customerId, Model model){ + return Response.success(voucherService.getCustomerOwnedVouchers(customerId)); + } +} + diff --git a/src/main/java/org/programmers/springorder/voucher/dto/GiveVoucherRequestDto.java b/src/main/java/org/programmers/springorder/voucher/dto/GiveVoucherRequestDto.java index 14101f7f4f..ddb82ad967 100644 --- a/src/main/java/org/programmers/springorder/voucher/dto/GiveVoucherRequestDto.java +++ b/src/main/java/org/programmers/springorder/voucher/dto/GiveVoucherRequestDto.java @@ -1,21 +1,12 @@ package org.programmers.springorder.voucher.dto; -import java.util.UUID; +import org.springframework.lang.NonNull; -public class GiveVoucherRequestDto { - private final UUID VoucherId; +import java.util.UUID; - private final UUID customerId; - public GiveVoucherRequestDto(UUID voucherId, UUID customerId) { - VoucherId = voucherId; - this.customerId = customerId; - } +public record GiveVoucherRequestDto( + @NonNull UUID voucherId, + @NonNull UUID customerId) { - public UUID getVoucherId() { - return VoucherId; - } - public UUID getCustomerId() { - return customerId; - } } diff --git a/src/main/java/org/programmers/springorder/voucher/dto/Response.java b/src/main/java/org/programmers/springorder/voucher/dto/Response.java new file mode 100644 index 0000000000..eabea7c468 --- /dev/null +++ b/src/main/java/org/programmers/springorder/voucher/dto/Response.java @@ -0,0 +1,29 @@ +package org.programmers.springorder.voucher.dto; + +public class Response { + private final String resultCode; + private final T data; + + public Response(String resultCode, T data) { + this.resultCode = resultCode; + this.data = data; + } + public static Response error(String errorCode){ + return new Response<>(errorCode, null); + } + + public static Response success(){ + return new Response("SUCCESS", null); + } + public static Response success(T result){ + return new Response<>("SUCCESS", result); + } + + public String getResultCode() { + return resultCode; + } + + public T getData() { + return data; + } +} diff --git a/src/main/java/org/programmers/springorder/voucher/dto/VoucherRequestDto.java b/src/main/java/org/programmers/springorder/voucher/dto/VoucherRequestDto.java index 7c7b286721..fc5740a95a 100644 --- a/src/main/java/org/programmers/springorder/voucher/dto/VoucherRequestDto.java +++ b/src/main/java/org/programmers/springorder/voucher/dto/VoucherRequestDto.java @@ -1,21 +1,25 @@ package org.programmers.springorder.voucher.dto; +import org.programmers.springorder.exception.ErrorCode; +import org.programmers.springorder.exception.VoucherException; import org.programmers.springorder.voucher.model.VoucherType; +import org.springframework.lang.NonNull; -public class VoucherRequestDto { - private final long discountValue; - private final VoucherType voucherType; - +public record VoucherRequestDto( + @NonNull long discountValue, + @NonNull VoucherType voucherType) { public VoucherRequestDto(long discountValue, VoucherType voucherType) { + validatingVoucherInfo(discountValue, voucherType); this.discountValue = discountValue; this.voucherType = voucherType; } - public long getDiscountValue() { - return discountValue; - } - - public VoucherType getVoucherType() { - return voucherType; + private static void validatingVoucherInfo(long discountValue, VoucherType voucherType) { + if (voucherType == VoucherType.FIXED && (discountValue < 100 || discountValue > 10000)) { + throw new VoucherException(ErrorCode.INVALID_DISCOUNT_VALUE); + } + if (voucherType == VoucherType.PERCENT && (discountValue < 3 || discountValue > 50)) { + throw new VoucherException(ErrorCode.INVALID_DISCOUNT_PERCENT); + } } } diff --git a/src/main/java/org/programmers/springorder/voucher/dto/VoucherResponseDto.java b/src/main/java/org/programmers/springorder/voucher/dto/VoucherResponseDto.java index 1b86398579..3435d24fef 100644 --- a/src/main/java/org/programmers/springorder/voucher/dto/VoucherResponseDto.java +++ b/src/main/java/org/programmers/springorder/voucher/dto/VoucherResponseDto.java @@ -2,33 +2,75 @@ import org.programmers.springorder.voucher.model.Voucher; +import java.util.Objects; import java.util.UUID; -public class VoucherResponseDto{ +public final class VoucherResponseDto { private final UUID voucherId; private final long discountValue; private final String voucherType; + private final String customerId; - private VoucherResponseDto(Voucher voucher) { - this.voucherId = voucher.getVoucherId(); - this.discountValue = voucher.getDiscountValue(); - this.voucherType = voucher.getVoucherType().name(); - } - public static VoucherResponseDto of(Voucher voucher){ - return new VoucherResponseDto(voucher); + public VoucherResponseDto(UUID voucherId, long discountValue, String voucherType, UUID customerId) { + this.voucherId = voucherId; + this.discountValue = discountValue; + this.voucherType = voucherType; + if (customerId != null) { + this.customerId = customerId.toString(); + } else { + this.customerId = ""; + } } - public UUID getVoucherId() { - return voucherId; + + public static VoucherResponseDto of(Voucher voucher) { + return new VoucherResponseDto( + voucher.getVoucherId(), + voucher.getDiscountValue(), + voucher.getVoucherType().name(), + voucher.getCustomerId()); } @Override public String toString() { return "ID : " + voucherId + '\n' + "Type : " + voucherType + '\n' + - "Value : " + discountValue +'\n'+ + "Value : " + discountValue + '\n' + "=============================="; } + + public UUID getVoucherId() { + return voucherId; + } + + public long getDiscountValue() { + return discountValue; + } + + public String getVoucherType() { + return voucherType; + } + + public String getCustomerId() { + return customerId; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (VoucherResponseDto) obj; + return Objects.equals(this.voucherId, that.voucherId) && + this.discountValue == that.discountValue && + Objects.equals(this.voucherType, that.voucherType) && + Objects.equals(this.customerId, that.customerId); + } + + @Override + public int hashCode() { + return Objects.hash(voucherId, discountValue, voucherType, customerId); + } + } diff --git a/src/main/java/org/programmers/springorder/voucher/model/Voucher.java b/src/main/java/org/programmers/springorder/voucher/model/Voucher.java index d3d48710b0..029d9dc795 100644 --- a/src/main/java/org/programmers/springorder/voucher/model/Voucher.java +++ b/src/main/java/org/programmers/springorder/voucher/model/Voucher.java @@ -3,6 +3,7 @@ import org.programmers.springorder.customer.model.Customer; import org.programmers.springorder.voucher.dto.VoucherRequestDto; +import java.time.LocalDateTime; import java.util.Objects; import java.util.UUID; @@ -10,46 +11,65 @@ public class Voucher { private final UUID voucherId; private final long discountValue; private final VoucherType voucherType; + private final LocalDateTime createdAt; + private final LocalDateTime updatedAt; private UUID customerId; private Voucher(UUID voucherId, long discountValue, VoucherType voucherType) { this.voucherId = voucherId; this.discountValue = discountValue; this.voucherType = voucherType; + this.createdAt = LocalDateTime.now(); + this.updatedAt = this.createdAt; } - private Voucher(UUID voucherId, long discountValue, VoucherType voucherType, UUID customerId) { + private Voucher(UUID voucherId, long discountValue, VoucherType voucherType, UUID customerId, LocalDateTime createdAt, LocalDateTime updatedAt) { this.voucherId = voucherId; this.discountValue = discountValue; this.voucherType = voucherType; this.customerId = customerId; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + private Voucher(UUID voucherId, long discountValue, VoucherType voucherType, LocalDateTime createdAt, LocalDateTime updatedAt) { + this.voucherId = voucherId; + this.discountValue = discountValue; + this.voucherType = voucherType; + this.createdAt = createdAt; + this.updatedAt = updatedAt; } - public static Voucher toVoucher(UUID voucherId, long discountValue, VoucherType voucherType){ + private Voucher(UUID voucherId, VoucherRequestDto voucherRequestDto) { + this.voucherId = voucherId; + this.discountValue = voucherRequestDto.discountValue(); + this.voucherType = voucherRequestDto.voucherType(); + this.createdAt = LocalDateTime.now(); + this.updatedAt = createdAt; + } + + public static Voucher toNewVoucher(UUID voucherId, long discountValue, VoucherType voucherType){ return new Voucher(voucherId, discountValue, voucherType); } - public static Voucher getVoucher(UUID voucherId, long discountValue, VoucherType voucherType, UUID customerId) { - return new Voucher(voucherId, discountValue, voucherType, customerId); + public static Voucher getFromDbVoucher(UUID voucherId, long discountValue, VoucherType voucherType, UUID customerId, LocalDateTime createdAt, LocalDateTime updatedAt) { + return new Voucher(voucherId, discountValue, voucherType, customerId, createdAt, updatedAt); } - private Voucher(UUID voucherId, VoucherRequestDto voucherRequestDto) { - this.voucherId = voucherId; - this.discountValue = voucherRequestDto.getDiscountValue(); - this.voucherType = voucherRequestDto.getVoucherType(); + public static Voucher getFromDbVoucherNoOwner(UUID voucherId, long discountValue, VoucherType voucherType, LocalDateTime createdAt, LocalDateTime updatedAt) { + return new Voucher(voucherId, discountValue, voucherType, createdAt, updatedAt); } public static Voucher of(UUID voucherId, VoucherRequestDto requestDto) { return new Voucher(voucherId, requestDto); } - public static Voucher toVoucher(UUID voucherId, long discountValue, VoucherType voucherType, UUID customerId) { - return new Voucher(voucherId, discountValue, voucherType, customerId); - } - public void updateOwner(Customer customer){ this.customerId = customer.getCustomerId(); } + public boolean voucherRange(LocalDateTime startedAt, LocalDateTime endedAt){ + return this.createdAt.isAfter(startedAt) && this.createdAt.isBefore(endedAt); + } + public boolean comparingCustomer(UUID customerId){ if(this.customerId == null) { return false; @@ -60,7 +80,9 @@ public String insertVoucherDataInFile() { StringBuilder data = new StringBuilder(); data.append(this.voucherId).append(",") .append(this.discountValue).append(",") - .append(this.voucherType.name()); + .append(this.voucherType.name()).append(",") + .append(this.createdAt).append(",") + .append(this.updatedAt); if(this.customerId != null){ data.append(",").append(this.customerId); } @@ -98,6 +120,14 @@ public UUID getCustomerId() { return customerId; } + public LocalDateTime getCreatedAt() { + return createdAt; + } + + public LocalDateTime getUpdatedAt() { + return updatedAt; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/org/programmers/springorder/voucher/repository/FileVoucherRepository.java b/src/main/java/org/programmers/springorder/voucher/repository/FileVoucherRepository.java index 33ecbf4682..9bca67ae27 100644 --- a/src/main/java/org/programmers/springorder/voucher/repository/FileVoucherRepository.java +++ b/src/main/java/org/programmers/springorder/voucher/repository/FileVoucherRepository.java @@ -12,13 +12,14 @@ import org.springframework.stereotype.Repository; import java.io.*; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; @Repository -@Profile("dev") +@Profile("test") public class FileVoucherRepository implements VoucherRepository { private static final Logger logger = LoggerFactory.getLogger(FileVoucherRepository.class); @@ -51,10 +52,12 @@ public List findAll() { UUID voucherId = UUID.fromString(data[0]); long discountValue = Long.parseLong(data[1]); VoucherType voucherType = VoucherType.valueOf(data[2]); - Voucher voucher = Voucher.toVoucher(voucherId, discountValue, voucherType); - if (data.length == 4) { - UUID customerId = UUID.fromString(data[3]); - voucher = Voucher.toVoucher(voucherId, discountValue, voucherType, customerId); + LocalDateTime createdAt = LocalDateTime.parse(data[3]); + LocalDateTime updatedAt = LocalDateTime.parse(data[4]); + Voucher voucher = Voucher.getFromDbVoucherNoOwner(voucherId, discountValue, voucherType, createdAt, updatedAt); + if (data.length == 6) { + UUID customerId = UUID.fromString(data[5]); + voucher = Voucher.getFromDbVoucher(voucherId, discountValue, voucherType, customerId, createdAt, updatedAt); } voucherList.add(voucher); } @@ -76,7 +79,6 @@ public Optional findById(UUID voucherId) { .findFirst(); } - //TODO: 구현 필요 기능(jdbc 우선) @Override public Voucher updateVoucherOwner(Voucher voucher, Customer customer) { List allVouchers = findAll(); @@ -108,6 +110,13 @@ public void deleteVoucher(Voucher voucher) { writeAllVouchers(voucherList); } + @Override + public List findAllByTimeLimit(LocalDateTime startedAt, LocalDateTime endedAt) { + return findAll().stream() + .filter(voucher -> voucher.voucherRange(startedAt, endedAt)) + .toList(); + } + public void clear() { try (FileOutputStream fos = new FileOutputStream(Properties.getVoucherFilePath(), false)) { } catch (IOException e) { diff --git a/src/main/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepository.java b/src/main/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepository.java index ba71c9d076..2cb0a839eb 100644 --- a/src/main/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepository.java +++ b/src/main/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepository.java @@ -12,18 +12,24 @@ import org.springframework.stereotype.Repository; import java.nio.ByteBuffer; +import java.time.LocalDateTime; import java.util.*; + @Profile("default") @Repository public class JdbcVoucherRepository implements VoucherRepository { private static final Logger logger = LoggerFactory.getLogger(JdbcVoucherRepository.class); private final NamedParameterJdbcTemplate jdbcTemplate; - private final String INSERT = "insert into vouchers(voucher_id, discount_value, voucher_type) values(UUID_TO_BIN(:voucherId), :discountValue, :voucherType)"; + private final String INSERT = "insert into vouchers(voucher_id, discount_value, voucher_type, created_at, updated_at) values(UUID_TO_BIN(:voucherId), :discountValue, :voucherType, :createdAt, :updatedAt)"; private final String UPDATE_VOUCER_OWNER = "update vouchers set customer_id = UUID_TO_BIN(:customerId) where voucher_id = UUID_TO_BIN(:voucherId)"; private final String FIND_ALL = "select * from vouchers"; private final String FIND_BY_VOUCHER_ID = "select * from vouchers where voucher_id = UUID_TO_BIN(:voucherId)"; - private final String FIND_BY_CUSTOMER_ID= "select * from vouchers where customer_id = UUID_TO_BIN(:customerId)"; + private final String FIND_BY_CUSTOMER_ID = "select * from vouchers where customer_id = UUID_TO_BIN(:customerId)"; + private final String DELETE_BY_VOUCHER_ID = "delete from vouchers where voucher_id = UUID_TO_BIN(:voucherId)"; + private final String DELETE_ALL = "delete from vouchers"; + private final String FIND_ALL_BY_TIME_BOUNDARY = "select * from vouchers where created_at >= :createdAt and created_at <= :endedAt"; + public JdbcVoucherRepository(NamedParameterJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @@ -31,7 +37,7 @@ public JdbcVoucherRepository(NamedParameterJdbcTemplate jdbcTemplate) { @Override public Voucher save(Voucher voucher) { int update = jdbcTemplate.update(INSERT, toParamMap(voucher)); - if( update != 1){ + if (update != 1) { throw new RuntimeException("Nothing was inserted"); } return voucher; @@ -44,12 +50,12 @@ public List findAll() { @Override public Optional findById(UUID voucherId) { - try{ + try { return Optional.ofNullable(jdbcTemplate.queryForObject( FIND_BY_VOUCHER_ID, Collections.singletonMap("voucherId", voucherId.toString().getBytes()), voucherRowMapper)); - } catch (EmptyResultDataAccessException e) { + } catch (EmptyResultDataAccessException e) { logger.error("Got empty result", e); return Optional.empty(); } @@ -59,7 +65,7 @@ public Optional findById(UUID voucherId) { @Override public Voucher updateVoucherOwner(Voucher voucher, Customer customer) { int update = jdbcTemplate.update(UPDATE_VOUCER_OWNER, toUpdateOwnerMap(voucher, customer)); - if( update != 1){ + if (update != 1) { throw new RuntimeException("Nothing was inserted"); } return voucher; @@ -76,36 +82,54 @@ public List findAllByCustomerId(Customer customer) { @Override public void deleteVoucher(Voucher voucher) { - jdbcTemplate.update("delete from vouchers where voucher_id = UUID_TO_BIN(:voucherId)", + jdbcTemplate.update(DELETE_BY_VOUCHER_ID, toParamMap(voucher)); } - public void clear(){ - jdbcTemplate.getJdbcOperations().update("delete from vouchers"); + @Override + public List findAllByTimeLimit(LocalDateTime startedAt, LocalDateTime endedAt) { + return jdbcTemplate.query(FIND_ALL_BY_TIME_BOUNDARY, + toDateMap(startedAt, endedAt), + voucherRowMapper); + } + + public void clear() { + jdbcTemplate.getJdbcOperations().update(DELETE_ALL); } private Map toParamMap(Voucher voucher) { - return new HashMap<>() {{ - put("voucherId", voucher.getVoucherId().toString().getBytes()); - put("discountValue", voucher.getDiscountValue()); - put("voucherType", voucher.getVoucherType().name()); - }}; + Map map = new HashMap<>(); + map.put("voucherId", voucher.getVoucherId().toString().getBytes()); + map.put("discountValue", voucher.getDiscountValue()); + map.put("voucherType", voucher.getVoucherType().name()); + map.put("createdAt", voucher.getCreatedAt()); + map.put("updatedAt", voucher.getUpdatedAt()); + return map; + } + + private Map toDateMap(LocalDateTime createdAt, LocalDateTime updatedAt) { + Map map = new HashMap<>(); + map.put("createdAt", createdAt); + map.put("updatedAt", updatedAt); + return map; } - private Map toUpdateOwnerMap(Voucher voucher,Customer customer) { - return new HashMap<>() {{ - put("voucherId", voucher.getVoucherId().toString().getBytes()); - put("customerId", customer.getCustomerId().toString().getBytes()); - }}; + private Map toUpdateOwnerMap(Voucher voucher, Customer customer) { + Map map = new HashMap<>(); + map.put("voucherId", voucher.getVoucherId().toString().getBytes()); + map.put("customerId", customer.getCustomerId().toString().getBytes()); + return map; } private final RowMapper voucherRowMapper = (resultSet, rowNum) -> { UUID voucherId = toUUID(resultSet.getBytes("voucher_id")); long discountValue = resultSet.getLong("discount_value"); VoucherType voucherType = VoucherType.valueOf(resultSet.getString("voucher_type")); - UUID customerID = resultSet.getBytes("customer_id") != null ? + UUID customerId = resultSet.getBytes("customer_id") != null ? toUUID(resultSet.getBytes("customer_id")) : null; - return Voucher.getVoucher(voucherId, discountValue, voucherType, customerID); + LocalDateTime createdAt = resultSet.getTimestamp("created_at").toLocalDateTime(); + LocalDateTime updatedAt = resultSet.getTimestamp("created_at").toLocalDateTime(); + return Voucher.getFromDbVoucher(voucherId, discountValue, voucherType, customerId, createdAt, updatedAt); }; static UUID toUUID(byte[] bytes) { diff --git a/src/main/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepository.java b/src/main/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepository.java index ffebc6bda6..ab31362320 100644 --- a/src/main/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepository.java +++ b/src/main/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepository.java @@ -5,6 +5,7 @@ import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository; +import java.time.LocalDateTime; import java.util.List; import java.util.Map; import java.util.Optional; @@ -12,7 +13,7 @@ import java.util.concurrent.ConcurrentHashMap; @Repository -@Profile("test") +@Profile("prod") public class MemoryVoucherRepository implements VoucherRepository{ private final Map storage = new ConcurrentHashMap<>(); @@ -53,6 +54,14 @@ public void deleteVoucher(Voucher voucher) { storage.remove(voucher.getVoucherId()); } + @Override + public List findAllByTimeLimit(LocalDateTime startedAt, LocalDateTime endedAt) { + return storage.values() + .stream() + .filter(voucher -> voucher.voucherRange(startedAt, endedAt)) + .toList(); + } + public void reset(){ storage.clear(); } diff --git a/src/main/java/org/programmers/springorder/voucher/repository/VoucherRepository.java b/src/main/java/org/programmers/springorder/voucher/repository/VoucherRepository.java index dcce0d49db..592b13ebc3 100644 --- a/src/main/java/org/programmers/springorder/voucher/repository/VoucherRepository.java +++ b/src/main/java/org/programmers/springorder/voucher/repository/VoucherRepository.java @@ -3,6 +3,7 @@ import org.programmers.springorder.customer.model.Customer; import org.programmers.springorder.voucher.model.Voucher; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -14,4 +15,5 @@ public interface VoucherRepository { Voucher updateVoucherOwner(Voucher voucher, Customer customer); List findAllByCustomerId(Customer customer); void deleteVoucher(Voucher voucher); + List findAllByTimeLimit(LocalDateTime startedAt, LocalDateTime endedAt); } diff --git a/src/main/java/org/programmers/springorder/voucher/service/VoucherService.java b/src/main/java/org/programmers/springorder/voucher/service/VoucherService.java index ea61aaa6c5..9c9a3ba2c5 100644 --- a/src/main/java/org/programmers/springorder/voucher/service/VoucherService.java +++ b/src/main/java/org/programmers/springorder/voucher/service/VoucherService.java @@ -2,6 +2,8 @@ import org.programmers.springorder.customer.model.Customer; import org.programmers.springorder.customer.repository.CustomerRepository; +import org.programmers.springorder.exception.ErrorCode; +import org.programmers.springorder.exception.VoucherException; import org.programmers.springorder.voucher.dto.VoucherRequestDto; import org.programmers.springorder.voucher.dto.VoucherResponseDto; import org.programmers.springorder.voucher.model.Voucher; @@ -10,6 +12,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; import java.util.List; import java.util.UUID; @@ -26,39 +29,51 @@ public VoucherService(VoucherRepository voucherRepository, CustomerRepository cu this.customerRepository = customerRepository; } - public List getAllVoucher(){ + public List getAllVoucher() { return voucherRepository.findAll() .stream() .map(VoucherResponseDto::of) .toList(); } - public void save(VoucherRequestDto voucherDto) { + public void saveNewVoucher(VoucherRequestDto voucherDto) { Voucher voucher = Voucher.of(UUID.randomUUID(), voucherDto); voucherRepository.save(voucher); - log.info("등록된 Voucher => iD: {}, type: {}, value: {}", voucher.getVoucherId(), voucher.getVoucherType(), voucher.getDiscountValue()); } - public void update(UUID voucherId, UUID customerId){ + public void allocateVoucher(UUID voucherId, UUID customerId) { Voucher voucher = voucherRepository.findById(voucherId) - .orElseThrow( () -> new RuntimeException("해당 바우처를 찾을 수 없습니다.")); + .orElseThrow(() -> new VoucherException(ErrorCode.VOUCHER_NOT_FOUND)); Customer customer = customerRepository.findByID(customerId) - .orElseThrow( () -> new RuntimeException("해당 고객을 찾을 수 없습니다.")); + .orElseThrow(() -> new VoucherException(ErrorCode.CUSTOMER_NOT_FOUND)); voucherRepository.updateVoucherOwner(voucher, customer); } - public List getCustomerOwnedVouchers(UUID customerId){ + public List getCustomerOwnedVouchers(UUID customerId) { Customer customer = customerRepository.findByID(customerId) - .orElseThrow( () -> new RuntimeException("해당 고객을 찾을 수 없습니다.")); + .orElseThrow(() -> new VoucherException(ErrorCode.CUSTOMER_NOT_FOUND)); return voucherRepository.findAllByCustomerId(customer) .stream() .map(VoucherResponseDto::of) .toList(); } - public void deleteVoucher(UUID voucherId){ + public void deleteVoucher(UUID voucherId) { Voucher voucher = voucherRepository.findById(voucherId) - .orElseThrow(() -> new RuntimeException("찾으시는 voucher가 존재하지 않습니다.")); + .orElseThrow(() -> new VoucherException(ErrorCode.VOUCHER_NOT_FOUND)); voucherRepository.deleteVoucher(voucher); } + + public VoucherResponseDto getVoucherById(UUID voucherId) { + return voucherRepository.findById(voucherId) + .map(VoucherResponseDto::of) + .orElseThrow(() -> new VoucherException(ErrorCode.VOUCHER_NOT_FOUND)); + } + + public List getAllVoucherByTime(LocalDateTime startedAt, LocalDateTime endedAt) { + return voucherRepository.findAllByTimeLimit(startedAt, endedAt) + .stream() + .map(VoucherResponseDto::of) + .toList(); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c5e1442e0f..b7c2ecf412 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,7 +3,7 @@ spring: active: default datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/vouchers + url: jdbc:mysql://localhost:3300/vouchers username: root password: 1234 diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 852fbefc4a..a6efc8b390 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -27,7 +27,7 @@ - + \ No newline at end of file diff --git a/src/main/resources/templates/customer-detail.html b/src/main/resources/templates/customer-detail.html new file mode 100644 index 0000000000..0473caff40 --- /dev/null +++ b/src/main/resources/templates/customer-detail.html @@ -0,0 +1,81 @@ + + + + + + + + Document + + + + + + + +
+

CUSTOMER-detail

+ customerId:
+ customerName:
+ customerType:
+ +

Owned VoucherList

+ + + + + + + + + + + + + + + + +
NameCustomerTypedelete
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/customers.html b/src/main/resources/templates/customers.html new file mode 100644 index 0000000000..68c312c6f2 --- /dev/null +++ b/src/main/resources/templates/customers.html @@ -0,0 +1,59 @@ + + + + + + + + Document + + + + + +
+

Customers

+ + + + + + + + + + + + + + + + + +
#CustomerIdNameCustomerType
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000000..93c2897b6b --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,59 @@ + + + + + + + + Document + + + + + + + +
+

Welcome-Voucher-Mangement

+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/new-customer.html b/src/main/resources/templates/new-customer.html new file mode 100644 index 0000000000..aca72df637 --- /dev/null +++ b/src/main/resources/templates/new-customer.html @@ -0,0 +1,54 @@ + + + + + + + + Document + + + + + +
+

MAKE NEW VOUCHER

+ +
+ +
+ + +
+ +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/new-voucher.html b/src/main/resources/templates/new-voucher.html new file mode 100644 index 0000000000..935ced02eb --- /dev/null +++ b/src/main/resources/templates/new-voucher.html @@ -0,0 +1,53 @@ + + + + + + + + Document + + + + + +
+

MAKE NEW VOUCHER

+
+ +
+ + +
+ +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/no-voucher.html b/src/main/resources/templates/no-voucher.html new file mode 100644 index 0000000000..1cd2cdeff0 --- /dev/null +++ b/src/main/resources/templates/no-voucher.html @@ -0,0 +1,59 @@ + + + + + + + + Document + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/voucher-allocate.html b/src/main/resources/templates/voucher-allocate.html new file mode 100644 index 0000000000..af8a176a89 --- /dev/null +++ b/src/main/resources/templates/voucher-allocate.html @@ -0,0 +1,49 @@ + + + + + + + + Document + + + + +
+

VOUCHER-ALLOCATE

+
+ voucherId : +
+ + +
+ +
+
+ + + diff --git a/src/main/resources/templates/voucher-detail.html b/src/main/resources/templates/voucher-detail.html new file mode 100644 index 0000000000..f50cd168e1 --- /dev/null +++ b/src/main/resources/templates/voucher-detail.html @@ -0,0 +1,87 @@ + + + + + + + + Document + + + + + + + +
+

VOUCHER-detail

+ + + + + + + + + + + + + + + + + + + + + +
Voucher IdVoucher TypeDiscount ValueVoucher OwnerAllocateDelete
+ + + +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/vouchers.html b/src/main/resources/templates/vouchers.html new file mode 100644 index 0000000000..56642c32c0 --- /dev/null +++ b/src/main/resources/templates/vouchers.html @@ -0,0 +1,58 @@ + + + + + + + + Document + + + + +
+

ALL VOUCHERS

+ + + + + + + + + + + + + + + + + +
#VoucherIdVoucherTypediscountValue
+
+ + \ No newline at end of file diff --git a/src/test/java/org/programmers/springorder/config/jdbc/JdbcConfig.java b/src/test/java/org/programmers/springorder/config/jdbc/JdbcConfig.java index 3229c89ca6..a2674c9ea9 100644 --- a/src/test/java/org/programmers/springorder/config/jdbc/JdbcConfig.java +++ b/src/test/java/org/programmers/springorder/config/jdbc/JdbcConfig.java @@ -23,7 +23,7 @@ public class JdbcConfig { @Bean public DataSource dataSource() { var dataSource = DataSourceBuilder.create() - .url("jdbc:mysql://localhost:3306/vouchers_test") + .url("jdbc:mysql://localhost:3300/vouchers_test") .username("root") .password("1234") .type(HikariDataSource.class) diff --git a/src/test/java/org/programmers/springorder/config/memory/MemoryConfig.java b/src/test/java/org/programmers/springorder/config/memory/MemoryConfig.java index a42eaee1a4..1a4ca194ae 100644 --- a/src/test/java/org/programmers/springorder/config/memory/MemoryConfig.java +++ b/src/test/java/org/programmers/springorder/config/memory/MemoryConfig.java @@ -21,7 +21,7 @@ public class MemoryConfig { @Bean public DataSource dataSource() { var dataSource = DataSourceBuilder.create() - .url("jdbc:mysql://localhost:3306/vouchers_test") + .url("jdbc:mysql://localhost:3300/vouchers_test") .username("root") .password("1234") .type(HikariDataSource.class) diff --git a/src/test/java/org/programmers/springorder/customer/repository/FileCustomerRepositoryTest.java b/src/test/java/org/programmers/springorder/customer/repository/FileCustomerRepositoryTest.java index ddd0ef76ac..e20cadd121 100644 --- a/src/test/java/org/programmers/springorder/customer/repository/FileCustomerRepositoryTest.java +++ b/src/test/java/org/programmers/springorder/customer/repository/FileCustomerRepositoryTest.java @@ -41,9 +41,9 @@ void clear(){ void save() { // given int currentSize = customerRepository.findAll().size(); - Customer customer1 = Customer.toCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); - Customer customer2 = Customer.toCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); - Customer customer3 = Customer.toCustomer(UUID.randomUUID(), "test3", CustomerType.BLACK); + Customer customer1 = Customer.toNewCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); + Customer customer2 = Customer.toNewCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); + Customer customer3 = Customer.toNewCustomer(UUID.randomUUID(), "test3", CustomerType.BLACK); // when Customer insertedCustomer1 = customerRepository.insert(customer1); @@ -63,8 +63,8 @@ void save() { @DisplayName("전체 회원 조회에 성공한다.") void findAll() { // given - Customer customer1 = Customer.toCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); - Customer customer2 = Customer.toCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); + Customer customer1 = Customer.toNewCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); + Customer customer2 = Customer.toNewCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); customerRepository.insert(customer1); customerRepository.insert(customer2); @@ -79,8 +79,8 @@ void findAll() { @DisplayName("블랙 리스트 조회 테스트") void findAllBlackList() { // given - Customer customer1 = Customer.toCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); - Customer customer2 = Customer.toCustomer(UUID.randomUUID(), "test2", CustomerType.BLACK); + Customer customer1 = Customer.toNewCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); + Customer customer2 = Customer.toNewCustomer(UUID.randomUUID(), "test2", CustomerType.BLACK); customerRepository.insert(customer1); customerRepository.insert(customer2); diff --git a/src/test/java/org/programmers/springorder/customer/repository/JdbcCustomerRepositoryTest.java b/src/test/java/org/programmers/springorder/customer/repository/JdbcCustomerRepositoryTest.java index a973ff3a5e..4ea6af3f90 100644 --- a/src/test/java/org/programmers/springorder/customer/repository/JdbcCustomerRepositoryTest.java +++ b/src/test/java/org/programmers/springorder/customer/repository/JdbcCustomerRepositoryTest.java @@ -39,9 +39,9 @@ void clear(){ void save() { // given int currentSize = customerRepository.findAll().size(); - Customer customer1 = Customer.toCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); - Customer customer2 = Customer.toCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); - Customer customer3 = Customer.toCustomer(UUID.randomUUID(), "test3", CustomerType.BLACK); + Customer customer1 = Customer.toNewCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); + Customer customer2 = Customer.toNewCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); + Customer customer3 = Customer.toNewCustomer(UUID.randomUUID(), "test3", CustomerType.BLACK); // when Customer insertedCustomer1 = customerRepository.insert(customer1); @@ -61,8 +61,8 @@ void save() { @DisplayName("전체 회원 조회에 성공한다.") void findAll() { // given - Customer customer1 = Customer.toCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); - Customer customer2 = Customer.toCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); + Customer customer1 = Customer.toNewCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); + Customer customer2 = Customer.toNewCustomer(UUID.randomUUID(), "test2", CustomerType.NORMAL); customerRepository.insert(customer1); customerRepository.insert(customer2); @@ -77,8 +77,8 @@ void findAll() { @DisplayName("블랙 리스트 조회 테스트") void findAllBlackList() { // given - Customer customer1 = Customer.toCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); - Customer customer2 = Customer.toCustomer(UUID.randomUUID(), "test2", CustomerType.BLACK); + Customer customer1 = Customer.toNewCustomer(UUID.randomUUID(), "test1", CustomerType.NORMAL); + Customer customer2 = Customer.toNewCustomer(UUID.randomUUID(), "test2", CustomerType.BLACK); customerRepository.insert(customer1); customerRepository.insert(customer2); diff --git a/src/test/java/org/programmers/springorder/customer/service/CustomerServiceTest.java b/src/test/java/org/programmers/springorder/customer/service/CustomerServiceTest.java index 766cd5780f..316191f94c 100644 --- a/src/test/java/org/programmers/springorder/customer/service/CustomerServiceTest.java +++ b/src/test/java/org/programmers/springorder/customer/service/CustomerServiceTest.java @@ -5,10 +5,12 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.programmers.springorder.config.jdbc.JdbcConfig; +import org.programmers.springorder.customer.dto.CustomerResponseDto; import org.programmers.springorder.customer.model.Customer; import org.programmers.springorder.customer.model.CustomerType; import org.programmers.springorder.customer.repository.CustomerRepository; import org.programmers.springorder.customer.repository.JdbcCustomerRepository; +import org.programmers.springorder.exception.ErrorCode; import org.programmers.springorder.voucher.model.Voucher; import org.programmers.springorder.voucher.model.VoucherType; import org.programmers.springorder.voucher.repository.JdbcVoucherRepository; @@ -19,6 +21,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import java.util.List; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @@ -63,36 +66,50 @@ public void findVoucherOwner(){ //given UUID voucherId = UUID.randomUUID(); UUID customerId = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(voucherId, 1000, VoucherType.FIXED); - Customer customer = Customer.toCustomer(customerId, "owner", CustomerType.NORMAL); + Voucher voucher = Voucher.toNewVoucher(voucherId, 1000, VoucherType.FIXED); + Customer customer = Customer.toNewCustomer(customerId, "owner", CustomerType.NORMAL); //when voucherRepository.save(voucher); customerRepository.insert(customer); - voucherService.update(voucherId, customerId); + voucherService.allocateVoucher(voucherId, customerId); UUID customerId1 = customerService.findOwnerOfVoucher(voucherId).getCustomerId(); assertThat(customerId1).isEqualTo(customerId); } + @Test + @DisplayName("black list를 불러오는 test") + public void findBlacklist(){ + Customer customer1 = Customer.toNewCustomer(UUID.randomUUID(), "owner", CustomerType.NORMAL); + Customer customer2 = Customer.toNewCustomer(UUID.randomUUID(), "owner", CustomerType.BLACK); + Customer customer3 = Customer.toNewCustomer(UUID.randomUUID(), "owner", CustomerType.BLACK); + + customerRepository.insert(customer1); + customerRepository.insert(customer2); + customerRepository.insert(customer3); + + List blackList = customerService.getBlackList(); + assertThat(blackList).hasSize(2); + } @Test @DisplayName("실패, voucher id로 검색을 실패한 경우") public void findVoucherOwnerNoVoucher(){ //given UUID voucherId = UUID.randomUUID(); UUID customerId = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(voucherId, 1000, VoucherType.FIXED); - Customer customer = Customer.toCustomer(customerId, "owner", CustomerType.NORMAL); + Voucher voucher = Voucher.toNewVoucher(voucherId, 1000, VoucherType.FIXED); + Customer customer = Customer.toNewCustomer(customerId, "owner", CustomerType.NORMAL); //when voucherRepository.save(voucher); customerRepository.insert(customer); - voucherService.update(voucherId, customerId); + voucherService.allocateVoucher(voucherId, customerId); assertThatThrownBy(() -> customerService.findOwnerOfVoucher(UUID.randomUUID())) .isInstanceOf(RuntimeException.class) - .hasMessage("찾으시는 voucher가 존재하지 않습니다."); + .hasMessage(ErrorCode.VOUCHER_NOT_FOUND.getMessage()); } @Test @@ -101,17 +118,17 @@ public void findVoucherOwnerNoOwner(){ //given UUID voucherId = UUID.randomUUID(); UUID customerId = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(voucherId, 1000, VoucherType.FIXED); - Customer customer = Customer.toCustomer(customerId, "owner", CustomerType.NORMAL); + Voucher voucher = Voucher.toNewVoucher(voucherId, 1000, VoucherType.FIXED); + Customer customer = Customer.toNewCustomer(customerId, "owner", CustomerType.NORMAL); //when voucherRepository.save(voucher); customerRepository.insert(customer); -// voucherService.update(voucherId, customerId); +// voucherService.allocateVoucher(voucherId, customerId); assertThatThrownBy(() -> customerService.findOwnerOfVoucher(voucherId)) .isInstanceOf(RuntimeException.class) - .hasMessage("해당 voucher는 주인이 존재하지 않습니다."); + .hasMessage("해당 바우처를 소유한 고객이 없습니다."); } } diff --git a/src/test/java/org/programmers/springorder/model/VoucherTest.java b/src/test/java/org/programmers/springorder/model/VoucherTest.java index 50ec5a53af..de89f44a49 100644 --- a/src/test/java/org/programmers/springorder/model/VoucherTest.java +++ b/src/test/java/org/programmers/springorder/model/VoucherTest.java @@ -25,8 +25,8 @@ public void voucherCreateTest() throws Exception { VoucherType voucherType2 = VoucherType.PERCENT; //when - Voucher voucher1 = Voucher.toVoucher(voucherId1, discountValue1, voucherType1); - Voucher voucher2 = Voucher.toVoucher(voucherId2, discountValue2, voucherType2); + Voucher voucher1 = Voucher.toNewVoucher(voucherId1, discountValue1, voucherType1); + Voucher voucher2 = Voucher.toNewVoucher(voucherId2, discountValue2, voucherType2); //then assertThat(voucher1.getVoucherId()).isEqualTo(voucherId1); diff --git a/src/test/java/org/programmers/springorder/repository/MemoryVoucherRepositoryTest.java b/src/test/java/org/programmers/springorder/repository/MemoryVoucherRepositoryTest.java index 6a47601bdf..d63777a327 100644 --- a/src/test/java/org/programmers/springorder/repository/MemoryVoucherRepositoryTest.java +++ b/src/test/java/org/programmers/springorder/repository/MemoryVoucherRepositoryTest.java @@ -28,9 +28,9 @@ void tearDown() { void saveVoucher() { // given UUID voucherId1 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(voucherId1, 1000, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(voucherId1, 1000, VoucherType.FIXED); UUID voucherId2 = UUID.randomUUID(); - Voucher voucher2 = Voucher.toVoucher(voucherId2, 10, VoucherType.PERCENT); + Voucher voucher2 = Voucher.toNewVoucher(voucherId2, 10, VoucherType.PERCENT); // when Voucher saveVoucherId1 = voucherRepository.save(voucher1); @@ -48,8 +48,8 @@ void saveVoucher() { @DisplayName("바우처 목록 조회에 성공한다.") void viewAllVouchers() { // given - Voucher voucher1 = Voucher.toVoucher(UUID.randomUUID(), 1000, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(UUID.randomUUID(), 10, VoucherType.PERCENT); + Voucher voucher1 = Voucher.toNewVoucher(UUID.randomUUID(), 1000, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(UUID.randomUUID(), 10, VoucherType.PERCENT); // when voucherRepository.save(voucher1); @@ -68,7 +68,7 @@ void viewAllVouchers() { void findByVoucherId() { // given UUID voucherId1 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(voucherId1, 1000, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(voucherId1, 1000, VoucherType.FIXED); // when Voucher saveVoucherId = voucherRepository.save(voucher1); @@ -83,7 +83,7 @@ void findByVoucherId() { void findByVoucherIdFail() { // given UUID voucherId1 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(voucherId1, 1000, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(voucherId1, 1000, VoucherType.FIXED); // when voucherRepository.save(voucher1); diff --git a/src/test/java/org/programmers/springorder/utils/MenuTypeTest.java b/src/test/java/org/programmers/springorder/utils/MenuTypeTest.java new file mode 100644 index 0000000000..246e703562 --- /dev/null +++ b/src/test/java/org/programmers/springorder/utils/MenuTypeTest.java @@ -0,0 +1,57 @@ +package org.programmers.springorder.utils; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.InputMismatchException; + +import static org.assertj.core.api.Assertions.assertThat; + +class MenuTypeTest { + + @Test + @DisplayName("올바른 input menu를 넣을 시에 선택한 menu를 반환한다.") + public void inputMenuCorrectTest(){ + + String input1 = "1"; + String input2 = "2"; + String input3 = "3"; + String input4 = "4"; + String input5 = "5"; + String input6 = "6"; + String input7 = "7"; + String input8 = "8"; + + MenuType menuType1 = MenuType.selectMenu(input1); + MenuType menuType2 = MenuType.selectMenu(input2); + MenuType menuType3 = MenuType.selectMenu(input3); + MenuType menuType4 = MenuType.selectMenu(input4); + MenuType menuType5 = MenuType.selectMenu(input5); + MenuType menuType6 = MenuType.selectMenu(input6); + MenuType menuType7 = MenuType.selectMenu(input7); + MenuType menuType8 = MenuType.selectMenu(input8); + + assertThat(menuType1).isEqualTo(MenuType.EXIT); + assertThat(menuType2).isEqualTo(MenuType.CREATE); + assertThat(menuType3).isEqualTo(MenuType.LIST); + assertThat(menuType4).isEqualTo(MenuType.BLACK); + assertThat(menuType5).isEqualTo(MenuType.ALLOCATE); + assertThat(menuType6).isEqualTo(MenuType.GET_OWNER_VOUCHER); + assertThat(menuType7).isEqualTo(MenuType.DELETE_VOUCHER); + assertThat(menuType8).isEqualTo(MenuType.SEARCH_VOUCHER_OWNER); + } + + @Test + @DisplayName("올바르지 않은 input menu를 넣을 시에 선택한 InputMismatchException을 반환한다..") + public void WrongInputMenuCorrectTest(){ + String wrongInput1 = "일번"; + String wrongInput2 = "13"; + String wrongInput3 = "선택안함"; + + Assertions.assertThatThrownBy(() ->MenuType.selectMenu(wrongInput1)).isInstanceOf(InputMismatchException.class); + Assertions.assertThatThrownBy(() ->MenuType.selectMenu(wrongInput2)).isInstanceOf(InputMismatchException.class); + Assertions.assertThatThrownBy(() ->MenuType.selectMenu(wrongInput3)).isInstanceOf(InputMismatchException.class); + } + +} \ No newline at end of file diff --git a/src/test/java/org/programmers/springorder/voucher/dto/VoucherRequestDtoTest.java b/src/test/java/org/programmers/springorder/voucher/dto/VoucherRequestDtoTest.java new file mode 100644 index 0000000000..7ca03abf59 --- /dev/null +++ b/src/test/java/org/programmers/springorder/voucher/dto/VoucherRequestDtoTest.java @@ -0,0 +1,49 @@ +package org.programmers.springorder.voucher.dto; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.programmers.springorder.exception.ErrorCode; +import org.programmers.springorder.exception.VoucherException; +import org.programmers.springorder.voucher.model.VoucherType; +import org.springframework.boot.test.context.SpringBootTest; + + +@SpringBootTest +class VoucherRequestDtoTest { + + @Test + @DisplayName("voucher 정보를 정상적으로 받아올 때 DTO 변환 테스트") + void voucherRequestDtoSuccess() { + long discountValue = 1000; + VoucherType fixed = VoucherType.FIXED; + + long percentValue = 10; + VoucherType percent = VoucherType.PERCENT; + + Assertions.assertThatCode(() -> new VoucherRequestDto(discountValue, fixed)) + .doesNotThrowAnyException(); + Assertions.assertThatCode(() -> new VoucherRequestDto(percentValue, percent)) + .doesNotThrowAnyException(); + + } + + @Test + @DisplayName("voucher discountValue를 정상적으로 받아오지 못할 때 오류 테스트") + void voucherRequestDtoNotValidDiscountValue() { + long discountValue = 100000; + VoucherType fixed = VoucherType.FIXED; + + long percentValue = 500; + VoucherType percent = VoucherType.PERCENT; + + Assertions.assertThatThrownBy(() -> new VoucherRequestDto(discountValue, fixed)) + .isInstanceOf(VoucherException.class) + .hasMessage(ErrorCode.INVALID_DISCOUNT_VALUE.getMessage()); + Assertions.assertThatThrownBy(() -> new VoucherRequestDto(percentValue, percent)) + .isInstanceOf(VoucherException.class) + .hasMessage(ErrorCode.INVALID_DISCOUNT_PERCENT.getMessage()); + } + + +} \ No newline at end of file diff --git a/src/test/java/org/programmers/springorder/voucher/repository/FileVoucherRepositoryTest.java b/src/test/java/org/programmers/springorder/voucher/repository/FileVoucherRepositoryTest.java index 7dd5f533e9..8939090a76 100644 --- a/src/test/java/org/programmers/springorder/voucher/repository/FileVoucherRepositoryTest.java +++ b/src/test/java/org/programmers/springorder/voucher/repository/FileVoucherRepositoryTest.java @@ -49,7 +49,7 @@ void clear(){ @DisplayName("voucher 생성 테스트") public void makeVoucherTest() { UUID uuid = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(uuid, 100, VoucherType.FIXED); + Voucher voucher = Voucher.toNewVoucher(uuid, 100, VoucherType.FIXED); Voucher save = voucherRepository.save(voucher); Voucher foundVoucher = voucherRepository.findById(uuid).get(); @@ -61,9 +61,9 @@ public void makeVoucherTest() { @Test @DisplayName("voucher 전체 조회 테스트") public void getAllVoucherTest() { - Voucher voucher1 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); Voucher save1 = voucherRepository.save(voucher1); Voucher save2 = voucherRepository.save(voucher2); @@ -83,9 +83,9 @@ public void getVoucherByIdTest() { UUID uuid2 = UUID.randomUUID(); UUID uuid3 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -108,10 +108,10 @@ public void setVoucherTest() { UUID uuid2 = UUID.randomUUID(); UUID uuidCustomer = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); - Customer customer = Customer.toCustomer(uuidCustomer, "owner", CustomerType.NORMAL); + Customer customer = Customer.toNewCustomer(uuidCustomer, "owner", CustomerType.NORMAL); customerRepository.insert(customer); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -137,11 +137,11 @@ public void customerOwnedVoucherTest() { UUID uuid3 = UUID.randomUUID(); UUID uuidCustomer = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED); - Customer customer = Customer.toCustomer(uuidCustomer, "owner", CustomerType.NORMAL); + Customer customer = Customer.toNewCustomer(uuidCustomer, "owner", CustomerType.NORMAL); customerRepository.insert(customer); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -165,9 +165,9 @@ public void deleteVoucher() { UUID uuid2 = UUID.randomUUID(); UUID uuid3 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED); voucherRepository.save(voucher1); voucherRepository.save(voucher2); diff --git a/src/test/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepositoryTest.java b/src/test/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepositoryTest.java index fcd363bddd..c8f53cd62f 100644 --- a/src/test/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepositoryTest.java +++ b/src/test/java/org/programmers/springorder/voucher/repository/JdbcVoucherRepositoryTest.java @@ -55,7 +55,7 @@ void clear() { @DisplayName("voucher 생성 테스트") public void makeVoucherTest(){ UUID uuid = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(uuid, 100, VoucherType.FIXED ); + Voucher voucher = Voucher.toNewVoucher(uuid, 100, VoucherType.FIXED ); Voucher save = voucherRepository.save(voucher); Voucher foundVoucher = voucherRepository.findById(uuid).get(); @@ -67,9 +67,9 @@ public void makeVoucherTest(){ @Test @DisplayName("voucher 전체 조회 테스트") public void getAllVoucherTest(){ - Voucher voucher1 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED ); - Voucher voucher2 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED ); - Voucher voucher3 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED ); + Voucher voucher1 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED ); + Voucher voucher2 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED ); + Voucher voucher3 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED ); Voucher save1 = voucherRepository.save(voucher1); Voucher save2 = voucherRepository.save(voucher2); @@ -89,9 +89,9 @@ public void getVoucherByIdTest(){ UUID uuid2 = UUID.randomUUID(); UUID uuid3 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED ); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED ); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED ); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED ); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED ); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED ); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -114,10 +114,10 @@ public void setVoucherTest(){ UUID uuid2 = UUID.randomUUID(); UUID uuidCustomer = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED ); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED ); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED ); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED ); - Customer customer = Customer.toCustomer(uuidCustomer, "owner", CustomerType.NORMAL); + Customer customer = Customer.toNewCustomer(uuidCustomer, "owner", CustomerType.NORMAL); customerRepository.insert(customer); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -143,11 +143,11 @@ public void customerOwnedVoucherTest(){ UUID uuid3 = UUID.randomUUID(); UUID uuidCustomer = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED ); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED ); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED ); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED ); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED ); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED ); - Customer customer = Customer.toCustomer(uuidCustomer, "owner", CustomerType.NORMAL); + Customer customer = Customer.toNewCustomer(uuidCustomer, "owner", CustomerType.NORMAL); customerRepository.insert(customer); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -171,9 +171,9 @@ public void deleteVoucher(){ UUID uuid2 = UUID.randomUUID(); UUID uuid3 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED ); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED ); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED ); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED ); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED ); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED ); voucherRepository.save(voucher1); voucherRepository.save(voucher2); diff --git a/src/test/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepositoryTest.java b/src/test/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepositoryTest.java index 12c4678b31..579a0e33a7 100644 --- a/src/test/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepositoryTest.java +++ b/src/test/java/org/programmers/springorder/voucher/repository/MemoryVoucherRepositoryTest.java @@ -50,7 +50,7 @@ void clear(){ @DisplayName("voucher 생성 테스트") public void makeVoucherTest() { UUID uuid = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(uuid, 100, VoucherType.FIXED); + Voucher voucher = Voucher.toNewVoucher(uuid, 100, VoucherType.FIXED); Voucher save = voucherRepository.save(voucher); Voucher foundVoucher = voucherRepository.findById(uuid).get(); @@ -62,9 +62,9 @@ public void makeVoucherTest() { @Test @DisplayName("voucher 전체 조회 테스트") public void getAllVoucherTest() { - Voucher voucher1 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(UUID.randomUUID(), 100, VoucherType.FIXED); Voucher save1 = voucherRepository.save(voucher1); Voucher save2 = voucherRepository.save(voucher2); @@ -84,9 +84,9 @@ public void getVoucherByIdTest() { UUID uuid2 = UUID.randomUUID(); UUID uuid3 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -109,10 +109,10 @@ public void setVoucherTest() { UUID uuid2 = UUID.randomUUID(); UUID uuidCustomer = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); - Customer customer = Customer.toCustomer(uuidCustomer, "owner", CustomerType.NORMAL); + Customer customer = Customer.toNewCustomer(uuidCustomer, "owner", CustomerType.NORMAL); customerRepository.insert(customer); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -138,11 +138,11 @@ public void customerOwnedVoucherTest() { UUID uuid3 = UUID.randomUUID(); UUID uuidCustomer = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED); - Customer customer = Customer.toCustomer(uuidCustomer, "owner", CustomerType.NORMAL); + Customer customer = Customer.toNewCustomer(uuidCustomer, "owner", CustomerType.NORMAL); customerRepository.insert(customer); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -166,9 +166,9 @@ public void deleteVoucher() { UUID uuid2 = UUID.randomUUID(); UUID uuid3 = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED); voucherRepository.save(voucher1); voucherRepository.save(voucher2); diff --git a/src/test/java/org/programmers/springorder/voucher/service/VoucherServiceTest.java b/src/test/java/org/programmers/springorder/voucher/service/VoucherServiceTest.java index 4f40328ca3..c2df8a0380 100644 --- a/src/test/java/org/programmers/springorder/voucher/service/VoucherServiceTest.java +++ b/src/test/java/org/programmers/springorder/voucher/service/VoucherServiceTest.java @@ -11,6 +11,7 @@ import org.programmers.springorder.customer.model.CustomerType; import org.programmers.springorder.customer.repository.CustomerRepository; import org.programmers.springorder.customer.repository.JdbcCustomerRepository; +import org.programmers.springorder.exception.VoucherException; import org.programmers.springorder.voucher.dto.VoucherRequestDto; import org.programmers.springorder.voucher.dto.VoucherResponseDto; import org.programmers.springorder.voucher.model.Voucher; @@ -64,10 +65,10 @@ void clear(){ @DisplayName("모든 Voucher 리스트를 가져오는 Service 로직") void getAllVoucher() { List uuids = Arrays.asList(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()); - voucherRepository.save(Voucher.toVoucher(uuids.get(0), 10, VoucherType.PERCENT)); - voucherRepository.save(Voucher.toVoucher(uuids.get(1), 5, VoucherType.PERCENT)); - voucherRepository.save(Voucher.toVoucher(uuids.get(2), 1000, VoucherType.FIXED)); - voucherRepository.save(Voucher.toVoucher(uuids.get(3), 2000, VoucherType.FIXED)); + voucherRepository.save(Voucher.toNewVoucher(uuids.get(0), 10, VoucherType.PERCENT)); + voucherRepository.save(Voucher.toNewVoucher(uuids.get(1), 5, VoucherType.PERCENT)); + voucherRepository.save(Voucher.toNewVoucher(uuids.get(2), 1000, VoucherType.FIXED)); + voucherRepository.save(Voucher.toNewVoucher(uuids.get(3), 2000, VoucherType.FIXED)); List allVoucher = voucherService.getAllVoucher(); List rs = allVoucher.stream().map(VoucherResponseDto::getVoucherId).toList(); @@ -86,7 +87,7 @@ void saveNewVoucher() { assertThat(beforeSaveVoucher).hasSize(0); //when - voucherService.save(requestDto); + voucherService.saveNewVoucher(requestDto); List allVoucher = voucherService.getAllVoucher(); //then @@ -104,13 +105,13 @@ void allocateVoucherOwner() { //given UUID voucherId = UUID.randomUUID(); UUID customerId = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(voucherId, 1000, VoucherType.FIXED); - Customer customer = Customer.toCustomer(customerId, "owner", CustomerType.NORMAL); + Voucher voucher = Voucher.toNewVoucher(voucherId, 1000, VoucherType.FIXED); + Customer customer = Customer.toNewCustomer(customerId, "owner", CustomerType.NORMAL); //when voucherRepository.save(voucher); customerRepository.insert(customer); - voucherService.update(voucherId, customerId); + voucherService.allocateVoucher(voucherId, customerId); //then Voucher voucherWithOwner = voucherRepository.findById(voucherId).get(); @@ -124,16 +125,16 @@ void allocateVoucherOwnerWithoutValidUser() { //given UUID voucherId = UUID.randomUUID(); UUID customerId = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(voucherId, 1000, VoucherType.FIXED); - Customer customer = Customer.toCustomer(customerId, "owner", CustomerType.NORMAL); + Voucher voucher = Voucher.toNewVoucher(voucherId, 1000, VoucherType.FIXED); + Customer customer = Customer.toNewCustomer(customerId, "owner", CustomerType.NORMAL); //when voucherRepository.save(voucher); customerRepository.insert(customer); //then - Assertions.assertThatThrownBy(() -> voucherService.update(voucherId, UUID.randomUUID())) - .isInstanceOf(RuntimeException.class) + Assertions.assertThatThrownBy(() -> voucherService.allocateVoucher(voucherId, UUID.randomUUID())) + .isInstanceOf(VoucherException.class) .hasMessage("해당 고객을 찾을 수 없습니다."); } @@ -144,16 +145,16 @@ void allocateVoucherOwnerWithoutValidVoucher() { //given UUID voucherId = UUID.randomUUID(); UUID customerId = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(voucherId, 1000, VoucherType.FIXED); - Customer customer = Customer.toCustomer(customerId, "owner", CustomerType.NORMAL); + Voucher voucher = Voucher.toNewVoucher(voucherId, 1000, VoucherType.FIXED); + Customer customer = Customer.toNewCustomer(customerId, "owner", CustomerType.NORMAL); //when voucherRepository.save(voucher); customerRepository.insert(customer); //then - Assertions.assertThatThrownBy(() -> voucherService.update(UUID.randomUUID(), customerId)) - .isInstanceOf(RuntimeException.class) + Assertions.assertThatThrownBy(() -> voucherService.allocateVoucher(UUID.randomUUID(), customerId)) + .isInstanceOf(VoucherException.class) .hasMessage("해당 바우처를 찾을 수 없습니다."); } @@ -173,11 +174,11 @@ public void customerOwnedVoucherServiceTest() { UUID uuid3 = UUID.randomUUID(); UUID uuidCustomer = UUID.randomUUID(); - Voucher voucher1 = Voucher.toVoucher(uuid1, 100, VoucherType.FIXED); - Voucher voucher2 = Voucher.toVoucher(uuid2, 100, VoucherType.FIXED); - Voucher voucher3 = Voucher.toVoucher(uuid3, 100, VoucherType.FIXED); + Voucher voucher1 = Voucher.toNewVoucher(uuid1, 100, VoucherType.FIXED); + Voucher voucher2 = Voucher.toNewVoucher(uuid2, 100, VoucherType.FIXED); + Voucher voucher3 = Voucher.toNewVoucher(uuid3, 100, VoucherType.FIXED); - Customer customer = Customer.toCustomer(uuidCustomer, "owner", CustomerType.NORMAL); + Customer customer = Customer.toNewCustomer(uuidCustomer, "owner", CustomerType.NORMAL); customerRepository.insert(customer); voucherRepository.save(voucher1); voucherRepository.save(voucher2); @@ -202,7 +203,7 @@ public void customerOwnedVoucherServiceTest() { @DisplayName("실패, 고객이 존재하지 않는 경우") public void customerOwnedVoucherServiceWithAnonymousUserTest() { assertThatThrownBy(() -> voucherService.getCustomerOwnedVouchers(UUID.randomUUID())) - .isInstanceOf(RuntimeException.class) + .isInstanceOf(VoucherException.class) .hasMessage("해당 고객을 찾을 수 없습니다."); } } @@ -214,7 +215,7 @@ class DeleteVoucher { @DisplayName("성공") public void deleteVoucherTest() { UUID uuid = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(uuid, 1000, VoucherType.FIXED); + Voucher voucher = Voucher.toNewVoucher(uuid, 1000, VoucherType.FIXED); voucherRepository.save(voucher); assertThat(voucherRepository.findAll()).hasSize(1); @@ -233,15 +234,15 @@ public void deleteVoucherTest() { @DisplayName("실패, voucher 존재 x") public void deleteVoucherFailTest() { UUID uuid = UUID.randomUUID(); - Voucher voucher = Voucher.toVoucher(uuid, 1000, VoucherType.FIXED); + Voucher voucher = Voucher.toNewVoucher(uuid, 1000, VoucherType.FIXED); voucherRepository.save(voucher); assertThat(voucherRepository.findAll()).hasSize(1); assertThat(voucherRepository.findById(uuid).get()).isEqualTo(voucher); assertThatThrownBy(() -> voucherService.deleteVoucher(UUID.randomUUID())) - .isInstanceOf(RuntimeException.class) - .hasMessage("찾으시는 voucher가 존재하지 않습니다."); + .isInstanceOf(VoucherException.class) + .hasMessage("해당 바우처를 찾을 수 없습니다."); } }