-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c42016
commit db67577
Showing
7 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/eum/bank/common/dto/request/AccountRequestDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.eum.bank.common.dto.request; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.Getter; | ||
|
||
public class AccountRequestDTO { | ||
// 계좌 생성 요청 | ||
@Getter | ||
public static class CreateAccount { | ||
@NotEmpty(message = "비밀번호를 입력해주세요.") | ||
private Long password; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/eum/bank/common/dto/response/AccountResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.eum.bank.common.dto.response; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
public class AccountResponseDTO { | ||
// 계좌 생성 응답 | ||
@Builder | ||
@Getter | ||
public static class Create { | ||
@NotEmpty(message = "계좌 번호가 생성되어야 합니다.") | ||
private String accountNumber; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/eum/bank/controller/AccountController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.eum.bank.controller; | ||
|
||
import com.eum.bank.common.APIResponse; | ||
import com.eum.bank.common.dto.request.AccountRequestDTO; | ||
import com.eum.bank.common.dto.response.AccountResponseDTO; | ||
import com.eum.bank.common.enums.SuccessCode; | ||
import com.eum.bank.service.AccountService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/account") | ||
@Slf4j | ||
public class AccountController { | ||
private final AccountService accountService; | ||
@PostMapping("/create") | ||
public ResponseEntity<?> create(@RequestBody AccountRequestDTO.CreateAccount createAccount) { | ||
|
||
Long password = createAccount.getPassword(); | ||
APIResponse<?> response = accountService.createAccount(password); | ||
|
||
return ResponseEntity.ok(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.eum.bank.service; | ||
|
||
import com.eum.bank.common.APIResponse; | ||
import com.eum.bank.common.dto.response.AccountResponseDTO; | ||
import com.eum.bank.common.enums.SuccessCode; | ||
import com.eum.bank.domain.account.entity.Account; | ||
import com.eum.bank.repository.AccountRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Random; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AccountService { | ||
|
||
private final AccountRepository accountRepository; | ||
|
||
public APIResponse<?> createAccount(Long password) { | ||
|
||
String accountNumber; | ||
do { | ||
accountNumber = generateAccountNumber(); | ||
}while ( | ||
validateAccountNumber(accountNumber) | ||
); | ||
|
||
Account account = Account.builder() | ||
.accountNumber(accountNumber) | ||
.password(password.toString()) | ||
.totalBudget(0L) | ||
.availableBudget(0L) | ||
.build(); | ||
|
||
accountRepository.save(account); | ||
|
||
return APIResponse.of(SuccessCode.SELECT_SUCCESS, AccountResponseDTO.Create.builder() | ||
.accountNumber(account.getAccountNumber()) | ||
.build()); | ||
} | ||
|
||
public String generateAccountNumber() { | ||
Random random = new Random(); | ||
StringBuilder uniqueNumber = new StringBuilder(); | ||
|
||
for (int i = 0; i < 12; i++) { | ||
int digit = random.nextInt(10); | ||
uniqueNumber.append(digit); | ||
} | ||
|
||
if( validateAccountNumber(uniqueNumber.toString()) ) | ||
generateAccountNumber(); | ||
|
||
return uniqueNumber.toString(); | ||
} | ||
public Boolean validateAccountNumber(String accountNumber) { | ||
if (accountNumber.length() != 12) { | ||
return false; | ||
} | ||
|
||
try { | ||
Long.parseLong(accountNumber); | ||
accountRepository.findByAccountNumber(accountNumber); | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
} |