Skip to content

Commit

Permalink
feat deal create
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesecrust committed Jan 31, 2024
1 parent c7030ef commit 3f755c4
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/eum/bank/common/dto/request/DealRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.eum.bank.common.dto.request;

import lombok.Builder;
import lombok.Getter;

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;
}

}
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 @@ -85,13 +85,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
30 changes: 30 additions & 0 deletions src/main/java/com/eum/bank/service/DealService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.eum.bank.service;

import com.eum.bank.common.APIResponse;
import com.eum.bank.domain.account.entity.Account;
import com.eum.bank.domain.deal.entity.Deal;
import com.eum.bank.repository.DealRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DealService {

private final DealRepository dealRepository;

// 거래 생성
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);
}
}

0 comments on commit 3f755c4

Please sign in to comment.