Skip to content

Commit 77dfeb5

Browse files
authored
Merge pull request #34 from KB-JAKKU/feat/account
feat: 주거래 계좌 설정 엔드포인트 추가 및 AccountService 리팩토링
2 parents b9848bf + adbb2f4 commit 77dfeb5

5 files changed

Lines changed: 46 additions & 28 deletions

File tree

src/main/java/org/scoula/jakku/account/controller/AccountController.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.web.bind.annotation.*;
88

99
import java.util.List;
10+
import java.util.Map;
1011

1112
@RestController
1213
@RequestMapping("/accounts")
@@ -57,11 +58,15 @@ public ResponseEntity<List<BankLoginResponseDTO>> getBankLoginsByChild(@PathVari
5758
return ResponseEntity.ok(bankLoginService.getBankLoginsByChildId(childId));
5859
}
5960

60-
// 추후 새로고침 기능 시 사용
61-
// 계좌 실시간 조회
62-
// @GetMapping("/child/{childId}/fetch")
63-
// public ResponseEntity<List<AccountResponseDTO>> fetchAccounts(@PathVariable Long childId) {
64-
// return ResponseEntity.ok(accountService.fetchAccountsByChildId(childId));
65-
// }
61+
// 주거래 계좌 설정
62+
@PatchMapping("/{accountId}/main")
63+
public ResponseEntity<Void> setMainAccount(
64+
@PathVariable Long accountId,
65+
@RequestBody Map<String, Long> body
66+
) {
67+
Long childId = body.get("childId");
68+
accountService.setMainAccount(childId, accountId);
69+
return ResponseEntity.ok().build();
70+
}
6671

6772
}

src/main/java/org/scoula/jakku/account/dto/AccountResponseDTO.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
public class AccountResponseDTO {
1616
private Long accountId; // 계좌 PK
1717
private Long bankLoginId; // 뱅크 로그인 ID
18-
private String bankName;
18+
private String bankName; // 은행이름
1919
private String accountName; // 계좌명
2020
private String accountNumber; // 계좌번호
2121
private String status; // 계좌 상태 (ACTIVE / INACTIVE)
2222
private BigDecimal balance; // 잔액
2323
private LocalDateTime createdAt; // 생성일
2424
private String qrBase64; // QR 이미지 Base64
25+
private boolean mainAccount ; // 주거래 계좌 여부
2526

2627
public static AccountResponseDTO fromEntity(Account entity) {
2728
return AccountResponseDTO.builder()
@@ -34,6 +35,9 @@ public static AccountResponseDTO fromEntity(Account entity) {
3435
.balance(entity.getBalance())
3536
.createdAt(entity.getCreatedAt())
3637
.qrBase64(entity.getQrBase64())
38+
.mainAccount(entity.isMainAccount())
3739
.build();
3840
}
39-
}
41+
42+
43+
}

src/main/java/org/scoula/jakku/account/repository/AccountRepository.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.scoula.jakku.account.entity.Account;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Modifying;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
58

69
import java.util.List;
710
import java.util.Optional;
@@ -17,5 +20,7 @@ public interface AccountRepository extends JpaRepository<Account, Long> {
1720
// bankLoginId 기준 주거래 계좌 조회
1821
Optional<Account> findByBankLoginIdAndMainAccountTrue(Long bankLoginId);
1922

20-
23+
@Modifying
24+
@Query("UPDATE Account a SET a.mainAccount = false WHERE a.bankLogin.child.childId = :childId")
25+
void clearMainAccountByChild(@Param("childId") Long childId);
2126
}

src/main/java/org/scoula/jakku/account/service/AccountService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ public interface AccountService {
2626

2727
// 계좌 엔티티 저장 또는 업데이트
2828
Account save(Account account);
29+
30+
// 주거래 은행 지정
31+
void setMainAccount(Long childId, Long accountId);
2932
}

src/main/java/org/scoula/jakku/account/service/AccountServiceImpl.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class AccountServiceImpl implements AccountService {
4343
public List<AccountResponseDTO> getAccountsByChildId(Long childId) {
4444
return accountRepository.findByBankLogin_Child_ChildId(childId)
4545
.stream()
46-
.map(this::toDTO)
46+
.map(AccountResponseDTO::fromEntity)
4747
.collect(Collectors.toList());
4848
}
4949

@@ -109,7 +109,7 @@ public List<AccountResponseDTO> fetchAccountsByChildId(Long childId) {
109109
}
110110

111111
accountRepository.save(entity);
112-
results.add(toDTO(entity));
112+
results.add(AccountResponseDTO.fromEntity(entity));
113113
}
114114

115115
} catch (Exception e) {
@@ -172,7 +172,7 @@ public List<AccountResponseDTO> fetchAccounts(String bankCode, String connectedI
172172
}
173173

174174
accountRepository.save(entity);
175-
results.add(toDTO(entity));
175+
results.add(AccountResponseDTO.fromEntity(entity));
176176
}
177177

178178
} catch (Exception e) {
@@ -182,22 +182,6 @@ public List<AccountResponseDTO> fetchAccounts(String bankCode, String connectedI
182182
return results;
183183
}
184184

185-
// Entity → DTO 변환
186-
private AccountResponseDTO toDTO(Account entity) {
187-
return AccountResponseDTO.builder()
188-
.accountId(entity.getId())
189-
.bankLoginId(entity.getBankLogin().getId())
190-
.bankName(entity.getBankLogin().getBank().getBankName())
191-
.accountName(entity.getAccountName())
192-
.accountNumber(entity.getAccountNumber())
193-
.status(entity.getStatus().name())
194-
.balance(entity.getBalance())
195-
.createdAt(entity.getCreatedAt())
196-
.qrBase64(entity.getQrBase64())
197-
.build();
198-
}
199-
200-
201185
@Override
202186
public Account getAccountEntityByNumber(String accountNumber) {
203187
return accountRepository.findByAccountNumber(accountNumber)
@@ -228,4 +212,21 @@ public Account save(Account account) {
228212
return accountRepository.save(account);
229213
}
230214
}
215+
216+
@Override
217+
@Transactional
218+
public void setMainAccount(Long childId, Long accountId) {
219+
// 1. childId의 모든 계좌 mainAccount 초기화
220+
accountRepository.clearMainAccountByChild(childId);
221+
222+
// 2. 지정한 계좌만 true로 설정
223+
Account account = accountRepository.findById(accountId)
224+
.orElseThrow(() -> new RuntimeException("계좌를 찾을 수 없습니다: " + accountId));
225+
226+
account.setMainAccount(true);
227+
accountRepository.save(account);
228+
229+
log.info("주거래 계좌 설정 완료 - childId={}, accountId={}", childId, accountId);
230+
}
231+
231232
}

0 commit comments

Comments
 (0)