Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'mysql:mysql-connector-java:8.0.33'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'com.h2database:h2'

Expand Down
20 changes: 9 additions & 11 deletions src/main/java/org/prgms/kdtspringweek1/AppCommandLineRunner.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package org.prgms.kdtspringweek1;

import org.prgms.kdtspringweek1.controller.AppController;
import org.prgms.kdtspringweek1.exception.FileExceptionCode;
import org.prgms.kdtspringweek1.controller.consoleController.AppController;
import org.prgms.kdtspringweek1.exception.ExitException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("!test")
@Profile({"console"})
public class AppCommandLineRunner implements CommandLineRunner {

private final AppController appController;
private boolean shouldExit = false;
private final static Logger logger = LoggerFactory.getLogger(AppCommandLineRunner.class);
private final AppController appController;
private static final Logger logger = LoggerFactory.getLogger(AppCommandLineRunner.class);

public AppCommandLineRunner(AppController appController) {
this.appController = appController;
}

@Override
public void run(String... args) {
while(!shouldExit){
while (!shouldExit) {
try {
appController.startVoucherProgram();
} catch(Exception exception){
} catch (ExitException exception) {
// 시스템이 종료되어야하는 예외를 구분하여 처리
System.out.println(exception.getMessage());
logger.error(exception.getMessage());
// 파일에서 정보를 읽어오는 것을 실패했다면, 서버가 켜지지 않는게 정상이므로, 예외 전파 후 어플을 종료 시키도록 한다.
if(exception.getMessage().equals(FileExceptionCode.FAIL_TO_READ_DATA_FROM_CSV)){
shouldExit = true;
}
shouldExit = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.prgms.kdtspringweek1;

import java.nio.ByteBuffer;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.UUID;

public class JdbcUtils {
public class UUIDUtils {

public static UUID toUUID(byte[] bytes) {
var byteBuffer = ByteBuffer.wrap(bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void printExitMessage() {
public void printVouchersToSelect() {
System.out.println("""
--------------------------------------------------
voucher 종류를 선택하여 정수로 입력해주세요.
1. FixedAmountVoucher
2. PercentDiscountVoucher"""
voucher 종류를 선택하여 문자열로 입력해주세요.
1. fixed amount voucher
2. percent discount voucher"""
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.prgms.kdtspringweek1.controller;
package org.prgms.kdtspringweek1.controller.consoleController;

import org.prgms.kdtspringweek1.console.ConsoleOutput;
import org.prgms.kdtspringweek1.controller.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.controller.consoleController.dto.SelectFunctionTypeDto;
import org.springframework.stereotype.Component;

// 뷰 영역(console 패키지)에 컨트롤러가 포함되어 있어, 따로 빼주었습니다.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.prgms.kdtspringweek1.controller;
package org.prgms.kdtspringweek1.controller.consoleController;

import org.prgms.kdtspringweek1.console.ConsoleInput;
import org.prgms.kdtspringweek1.voucher.service.dto.CreateVoucherRequestDto;
import org.prgms.kdtspringweek1.controller.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.controller.consoleController.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.voucher.service.dto.SelectVoucherTypeDto;
import org.prgms.kdtspringweek1.voucher.service.dto.UpdateVoucherRequestDto;
import org.prgms.kdtspringweek1.voucher.entity.VoucherType;
Expand All @@ -27,8 +26,12 @@ public VoucherType getVoucherType() {
return SelectVoucherTypeDto.getVoucherTypeByNum(Long.parseLong(consoleInput.getInput()));
}

public CreateVoucherRequestDto getCreateVoucherRequestDto() {
return new CreateVoucherRequestDto(Long.parseLong(consoleInput.getInput()));
public String getVoucherTypeString() {
return consoleInput.getInput();
}

public long getDiscountValue() {
return Long.parseLong(consoleInput.getInput());
}

public UUID getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.prgms.kdtspringweek1.controller;
package org.prgms.kdtspringweek1.controller.consoleController;

import org.prgms.kdtspringweek1.console.ConsoleOutput;
import org.prgms.kdtspringweek1.controller.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.controller.consoleController.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.customer.service.dto.FindCustomerResponseDto;
import org.prgms.kdtspringweek1.customer.service.dto.UpdateCustomerRequestDto;
import org.prgms.kdtspringweek1.customer.entity.Customer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.prgms.kdtspringweek1.controller;
package org.prgms.kdtspringweek1.controller.consoleController;

import org.prgms.kdtspringweek1.console.ConsoleOutput;
import org.prgms.kdtspringweek1.controller.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.controller.consoleController.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.voucher.service.dto.CreateVoucherRequestDto;
import org.prgms.kdtspringweek1.voucher.service.dto.FindVoucherResponseDto;
import org.prgms.kdtspringweek1.voucher.service.VoucherService;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -33,16 +34,10 @@ public void selectVoucherFunction(SelectFunctionTypeDto functionTypeDto) {

private void createVoucher() {
consoleOutput.printVouchersToSelect();
switch (consoleInputConverter.getVoucherType()) {
case FIXED_AMOUNT -> {
consoleOutput.printRequestMessageforDiscountValue();
voucherService.registerVoucher(consoleInputConverter.getCreateVoucherRequestDto().toFixedAmountVoucher());
}
case PERCENT_DISCOUNT -> {
consoleOutput.printRequestMessageforDiscountValue();
voucherService.registerVoucher(consoleInputConverter.getCreateVoucherRequestDto().toPercentDiscountVoucher());
}
}
String voucherType = consoleInputConverter.getVoucherTypeString();
consoleOutput.printRequestMessageforDiscountValue();
long discountValue = consoleInputConverter.getDiscountValue();
voucherService.registerVoucher(new CreateVoucherRequestDto(discountValue, voucherType).toVoucher());
consoleOutput.printSuccessToCreate();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.prgms.kdtspringweek1.controller;
package org.prgms.kdtspringweek1.controller.consoleController;

import org.prgms.kdtspringweek1.console.ConsoleOutput;
import org.prgms.kdtspringweek1.controller.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.controller.consoleController.dto.SelectFunctionTypeDto;
import org.prgms.kdtspringweek1.customer.service.dto.FindCustomerResponseDto;
import org.prgms.kdtspringweek1.voucher.service.dto.FindVoucherResponseDto;
import org.prgms.kdtspringweek1.wallet.entity.Wallet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.prgms.kdtspringweek1.controller.dto;
package org.prgms.kdtspringweek1.controller.consoleController.dto;

import org.prgms.kdtspringweek1.exception.InputExceptionCode;
import org.slf4j.Logger;
Expand Down Expand Up @@ -26,7 +26,7 @@ public enum SelectFunctionTypeDto {

private String type;
private String name;
private final static Logger logger = LoggerFactory.getLogger(SelectFunctionTypeDto.class);
private static final Logger logger = LoggerFactory.getLogger(SelectFunctionTypeDto.class);

SelectFunctionTypeDto(String type, String name) {
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.prgms.kdtspringweek1.controller.restController;

import org.prgms.kdtspringweek1.voucher.service.VoucherService;
import org.prgms.kdtspringweek1.voucher.service.dto.CreateVoucherRequestDto;
import org.prgms.kdtspringweek1.voucher.service.dto.FindVoucherResponseDto;
import org.prgms.kdtspringweek1.voucher.service.dto.SelectVoucherTypeDto;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("rest")
@Profile({"rest"})
public class RestVoucherController {

private final VoucherService voucherService;

public RestVoucherController(VoucherService voucherService) {
this.voucherService = voucherService;
}

@GetMapping("/vouchers")
public List<FindVoucherResponseDto> getAllVouchers() {
return voucherService.searchAllVouchers();
}

@GetMapping("/vouchers/type")
public List<FindVoucherResponseDto> getVoucherByVoucherType(@RequestParam String num) {
return voucherService.searchVouchersByVoucherType(SelectVoucherTypeDto.getVoucherTypeByNum(Long.parseLong(num)).getName());
}

@GetMapping("/voucher")
public FindVoucherResponseDto getVoucherByVoucherId(@RequestParam String id) {
return voucherService.searchVoucherById(UUID.fromString(id)).get();
}

@PostMapping("/voucher")
public void createVoucher(@RequestBody CreateVoucherRequestDto createVoucherRequestDto) {
voucherService.registerVoucher(createVoucherRequestDto.toVoucher());
}

@DeleteMapping("/voucher")
public void deleteVoucher(@RequestParam String id) {
voucherService.deleteVoucherById(UUID.fromString(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.prgms.kdtspringweek1.controller.thymeleafController;

import org.prgms.kdtspringweek1.voucher.service.VoucherService;
import org.prgms.kdtspringweek1.voucher.service.dto.CreateVoucherRequestDto;
import org.prgms.kdtspringweek1.voucher.service.dto.FindVoucherResponseDto;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("thymeleaf")
@Profile({"thymeleaf"})
public class ThymeleafVoucherController {

private final VoucherService voucherService;

public ThymeleafVoucherController(VoucherService voucherService) {
this.voucherService = voucherService;
}

@GetMapping("/vouchers")
public String listVouchers(Model model) {
List<FindVoucherResponseDto> vouchers = voucherService.searchAllVouchers();
model.addAttribute("serverTime", LocalDateTime.now());
model.addAttribute("vouchers", vouchers);
return "voucher/voucher-list";
}

@PostMapping("/voucher")
public String createVoucher(@ModelAttribute CreateVoucherRequestDto createVoucherRequestDto) {
voucherService.registerVoucher(createVoucherRequestDto.toVoucher());
return "voucher/voucher-create";
}

@PostMapping("/voucher-delete")
public String deleteVoucher(@RequestParam String voucherId) {
voucherService.deleteVoucherById(UUID.fromString(voucherId));
return "voucher/voucher-delete";
}

@GetMapping("/voucher")
public String searchVoucher(@RequestParam String voucherId, Model model) {
FindVoucherResponseDto voucher = voucherService.searchVoucherById(UUID.fromString(voucherId)).get();
model.addAttribute("voucher", voucher);
return "voucher/voucher-search";
}

@GetMapping("/voucher-create")
public String createVoucher() {
return "voucher/voucher-create";
}

@GetMapping("/voucher-delete")
public String deleteVoucher() {
return "voucher/voucher-delete";
}

@GetMapping("/voucher-search")
public String searchVoucher() {
return "voucher/voucher-search";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.prgms.kdtspringweek1.controller.thymeleafController;

import org.prgms.kdtspringweek1.customer.service.dto.FindCustomerResponseDto;
import org.prgms.kdtspringweek1.voucher.service.dto.FindVoucherResponseDto;
import org.prgms.kdtspringweek1.wallet.service.WalletService;
import org.prgms.kdtspringweek1.wallet.service.dto.CreateWalletRequestDto;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("thymeleaf")
@Profile({"thymeleaf"})
public class ThymeleafWalletController {

private final WalletService walletService;

public ThymeleafWalletController(WalletService walletService) {
this.walletService = walletService;
}

@PostMapping("/wallet")
public String createWallet(@ModelAttribute CreateWalletRequestDto createWalletRequestDto) {
walletService.registerWallet(createWalletRequestDto.toWallet());
return "wallet/wallet-create";
}

@GetMapping("/customer/wallets") // customerId로 PATHVARIABLE 가능
public String searchAllVouchersByCustomerId(@RequestParam String customerId, Model model) {
List<FindVoucherResponseDto> vouchers = walletService.searchAllVouchersByCustomerId(UUID.fromString(customerId));
model.addAttribute("serverTime", LocalDateTime.now());
model.addAttribute("vouchers", vouchers);
return "wallet/wallet-search";
}

@PostMapping("/wallet-delete")
public String deleteWalletByVoucherIdAndCustomerId(@RequestParam String voucherId, @RequestParam String customerId) {
walletService.deleteWalletByVoucherIdAndCustomerId(UUID.fromString(voucherId), UUID.fromString(customerId));
return "wallet/wallet-delete";
}

@GetMapping("/voucher/wallets")
public String searchAllCustomersByVoucherId(@RequestParam String voucherId, Model model) {
List<FindCustomerResponseDto> customers = walletService.searchAllCustomersByVoucherId(UUID.fromString(voucherId));
model.addAttribute("serverTime", LocalDateTime.now());
model.addAttribute("customers", customers);
return "wallet/wallet-search";
}

@GetMapping("/wallet")
public String searchWallet() {
return "wallet/wallet-search";
}

@GetMapping("/wallet-delete")
public String deleteWallet() {
return "wallet/wallet-delete";
}

@GetMapping("/wallet-create")
public String createWallet() {
return "wallet/wallet-create";
}
}
Loading