-
Notifications
You must be signed in to change notification settings - Fork 1
[FEAT] 복식부기를 고려한 코인 장부 저장 기능 구현 #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yooooonshine
merged 2 commits into
develop
from
feature/269-implement-recording-transaction
Nov 23, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
4 changes: 4 additions & 0 deletions
4
src/main/java/hanium/modic/backend/domain/transaction/repository/AccountRepository.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| package hanium.modic.backend.domain.transaction.repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import hanium.modic.backend.domain.transaction.entity.Account; | ||
|
|
||
| public interface AccountRepository extends JpaRepository<Account, Long> { | ||
|
|
||
| Optional<Account> findByUserId(Long userId); | ||
| } |
8 changes: 8 additions & 0 deletions
8
...a/hanium/modic/backend/domain/transaction/repository/CoinTransactionEntityRepository.java
This file contains hidden or 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,8 @@ | ||
| package hanium.modic.backend.domain.transaction.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import hanium.modic.backend.domain.transaction.entity.CoinTransactionEntity; | ||
|
|
||
| public interface CoinTransactionEntityRepository extends JpaRepository<CoinTransactionEntity, Long> { | ||
| } |
8 changes: 8 additions & 0 deletions
8
...in/java/hanium/modic/backend/domain/transaction/repository/CoinTransactionRepository.java
This file contains hidden or 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,8 @@ | ||
| package hanium.modic.backend.domain.transaction.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import hanium.modic.backend.domain.transaction.entity.CoinTransaction; | ||
|
|
||
| public interface CoinTransactionRepository extends JpaRepository<CoinTransaction, Long> { | ||
| } |
This file contains hidden or 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
80 changes: 80 additions & 0 deletions
80
src/main/java/hanium/modic/backend/domain/transaction/service/LedgerService.java
This file contains hidden or 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,80 @@ | ||
| package hanium.modic.backend.domain.transaction.service; | ||
|
|
||
| import static hanium.modic.backend.common.error.ErrorCode.*; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import hanium.modic.backend.common.error.exception.AppException; | ||
| import hanium.modic.backend.common.error.exception.LockException; | ||
| import hanium.modic.backend.domain.transaction.entity.Account; | ||
| import hanium.modic.backend.domain.transaction.entity.CoinTransaction; | ||
| import hanium.modic.backend.domain.transaction.entity.CoinTransactionEntity; | ||
| import hanium.modic.backend.domain.transaction.enums.TransactionDirection; | ||
| import hanium.modic.backend.domain.transaction.enums.TransactionStatus; | ||
| import hanium.modic.backend.domain.transaction.repository.AccountRepository; | ||
| import hanium.modic.backend.domain.transaction.repository.CoinTransactionEntityRepository; | ||
| import hanium.modic.backend.domain.transaction.repository.CoinTransactionRepository; | ||
| import hanium.modic.backend.infra.redis.distributedLock.LockManager; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class LedgerService { | ||
|
|
||
| // 계좌, 거래 관련 | ||
| private final AccountRepository accountRepository; | ||
| private final CoinTransactionRepository coinTransactionRepository; | ||
| private final CoinTransactionEntityRepository entryRepository; | ||
|
|
||
| // 기타 | ||
| private final LockManager lockManager; | ||
|
|
||
| // 코인 전송(게좌 변경, 트랜잭션, 엔티티 저장) 후 알림 처리 | ||
| public void transfer(final long fromUserId, final long toUserId, final long amount) { | ||
| // 락 걸고 양도 처리 | ||
| try { | ||
| lockManager.multipleAccountLock(List.of(fromUserId, toUserId), () -> { | ||
| // 1) 계좌 읽기 | ||
| Account fromAccount = accountRepository.findByUserId(fromUserId) | ||
| .orElseThrow(() -> new AppException(ACCOUNT_NOT_FOUND_EXCEPTION)); | ||
| Account toAccount = accountRepository.findByUserId(toUserId) | ||
| .orElseThrow(() -> new AppException(ACCOUNT_NOT_FOUND_EXCEPTION)); | ||
|
|
||
| // 2) posted_balance 업데이트 | ||
| fromAccount.updateBalance(fromAccount.getPostedBalance() - amount); | ||
| toAccount.updateBalance(toAccount.getPostedBalance() + amount); | ||
|
|
||
| // 3) 버전 캡처, 반드시 Account 업데이트 이후에 호출되어야 함 | ||
| long fromVersionBefore = fromAccount.getLedgerVersion(); | ||
| long toVersionBefore = toAccount.getLedgerVersion(); | ||
|
|
||
| LocalDateTime now = LocalDateTime.now(); | ||
|
|
||
| // 4) 트랜잭션 생성 (posted) | ||
| CoinTransaction txn = coinTransactionRepository.save( | ||
| CoinTransaction.builder() | ||
| .status(TransactionStatus.POSTED) | ||
| .effectiveAt(now) | ||
| .build() | ||
| ); | ||
|
|
||
| // 5) 엔트리 생성 | ||
| entryRepository.save(CoinTransactionEntity.createPostedTransaction( | ||
| fromAccount.getId(), fromVersionBefore, txn.getId(), | ||
| TransactionDirection.DEBIT, amount, now | ||
| )); | ||
| entryRepository.save(CoinTransactionEntity.createPostedTransaction( | ||
| toAccount.getId(), toVersionBefore, txn.getId(), | ||
| TransactionDirection.CREDIT, amount, now | ||
| )); | ||
| accountRepository.save(fromAccount); | ||
| accountRepository.save(toAccount); | ||
| }); | ||
| } catch (LockException e) { | ||
| throw new AppException(COIN_TRANSFER_FAIL_EXCEPTION); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.