Skip to content

Commit

Permalink
Merge pull request #27 from EUM-kmu/feat/#18
Browse files Browse the repository at this point in the history
feat deal create
  • Loading branch information
cheesecrust authored Jan 31, 2024
2 parents 6b875a6 + 4a63974 commit 0822ff6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/eum/bank/common/dto/request/DealRequestDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@

import jakarta.validation.constraints.NotEmpty;
import lombok.Getter;
import lombok.Builder;

public class DealRequestDTO {
@Builder
@Getter
public static class Create{
//송금자 계좌번호
private String accountNumber;
// 비밀번호
private String password;
// 예치금 ( 지급해야 하는 금액 )
private Long deposit;
// 최대 인원수
private Long maxPeople;
// 글 id
private Long postId;
}


@Getter
public static class completeDeal{
Expand Down Expand Up @@ -50,4 +66,5 @@ public static class cancelDeal{
@NotEmpty(message = "비밀번호를 입력해주세요.")
private String password;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.eum.bank.common.dto.response;

import jakarta.validation.GroupSequence;
import lombok.Builder;
import lombok.Getter;

public class DealResponseDTO {

@Getter
@Builder
public static class Create {
// 거래 id
private Long id;
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/eum/bank/controller/DealController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.eum.bank.controller;

import com.eum.bank.common.APIResponse;
import com.eum.bank.common.dto.request.DealRequestDTO;
import com.eum.bank.common.dto.response.DealResponseDTO;
import com.eum.bank.domain.account.entity.Account;
import com.eum.bank.service.AccountService;
import com.eum.bank.service.DealService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/deal")
@RequiredArgsConstructor
public class DealController {
private final DealService dealService;
private final AccountService accountService;

@PostMapping("/create")
public ResponseEntity<?> create(@RequestBody DealRequestDTO.Create create) {

String accountNumber = create.getAccountNumber();
String password = create.getPassword();
Long deposit = create.getDeposit();
Long maxPeople = create.getMaxPeople();
Long postId = create.getPostId();

APIResponse<?> apiResponse = accountService.getAccount(accountNumber, password);

Account account = accountService.getAccount(accountNumber);

if (apiResponse.getStatus() == 200){
// 계좌가 존재하고 비밀번호가 일치하는 경우
// 거래 생성
dealService.createDeal(account, deposit, maxPeople, postId);
} else if (apiResponse.getStatus() == 400){
// 계좌가 존재하지 않는 경우
return ResponseEntity.badRequest().body(apiResponse);
} else if (apiResponse.getStatus() == 401){
// 비밀번호가 일치하지 않는 경우
return ResponseEntity.status(401).body(apiResponse);
}

APIResponse<DealResponseDTO.Create> response = new APIResponse<>();
return ResponseEntity.ok(response);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/eum/bank/repository/DealRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface DealRepository extends JpaRepository<Deal, Long> {

}
4 changes: 4 additions & 0 deletions src/main/java/com/eum/bank/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ public APIResponse<AccountResponseDTO.AccountInfo> getAccount(String accountNumb
if (!passwordEncoder.matches(password, account.getPassword())) {
throw new IllegalArgumentException("Invalid password");
}

return APIResponse.of(SuccessCode.SELECT_SUCCESS, AccountResponseDTO.AccountInfo.builder()
.accountNumber(account.getAccountNumber())
.totalBudget(account.getTotalBudget())
.availableBudget(account.getAvailableBudget())
.build());
}

public Account getAccount(String accountNumber) {
return accountRepository.findByAccountNumber(accountNumber).orElseThrow(() -> new IllegalArgumentException("Invalid account number"));
}
// 자유송금
// 1. 송금자 계좌, 수신자 계좌 상태 검증
// 2. 송금자 잔액 확인
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/eum/bank/service/DealService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eum.bank.service;

import com.eum.bank.common.APIResponse;

import com.eum.bank.common.dto.request.DealRequestDTO;
import com.eum.bank.common.enums.SuccessCode;
import com.eum.bank.domain.account.entity.Account;
Expand All @@ -25,6 +26,21 @@ public class DealService {
private final PasswordEncoder passwordEncoder;


// 거래 생성
public void createDeal(Account accountNumber, Long deposit, Long maxPeople, Long postId){
// 거래 생성
// 거래상태 a -> WAITING
Deal deal = Deal.builder()
.senderAccount(accountNumber)
.status("WAITING")
.deposit(deposit)
.numberOfPeople(maxPeople)
.postId(postId)
.build();

dealRepository.save(deal);
}

// 거래 성사
// 1. 거래상태 a인지 확인
// 2. 수신계좌들 검증
Expand Down

0 comments on commit 0822ff6

Please sign in to comment.