Skip to content

Commit

Permalink
Add put endpoint for depositing money.
Browse files Browse the repository at this point in the history
Refactor inAdapter to be in single file
Refactor Account out adapter.
Add DepositAccount in port and moneyDeposited event.
  • Loading branch information
rafaelcp94 committed Mar 22, 2024
1 parent 796ba8d commit eb147dc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.Acco
import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.account.mapper.toDto
import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.dto.AccountDto
import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.dto.CreateAccountRequestDto
import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.dto.DepositRequestDto
import io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.inbound.account.CreateAccountInPort
import io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.inbound.account.DepositAccountInPort
import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.customer.CustomerNumber
import io.holixon.cqrshexagonaldemo.demoparent.transactions.framework.InAdapter
import mu.KLogging
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
import java.math.BigDecimal

@RestController
@InAdapter
class CreateAccountRestInAdapter(
private val createAccountInPort: CreateAccountInPort
class AccountRestInAdapter(
private val createAccountInPort: CreateAccountInPort,
private val depositAccountInPort: DepositAccountInPort
) : AccountApiDelegate {

companion object : KLogging()
Expand All @@ -23,4 +27,9 @@ class CreateAccountRestInAdapter(
val createdAccount = createAccountInPort.createAccount(CustomerNumber(createAccountRequestDto.customerNumber))
return ResponseEntity.ok(createdAccount.toDto())
}
}

override fun deposit(depositRequestDto: DepositRequestDto): ResponseEntity<Void> {
depositAccountInPort.deposit(depositRequestDto.accountNumber, BigDecimal.valueOf(depositRequestDto.amount))
return ResponseEntity.noContent().build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.outbound.ac

import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.outbound.account.jpa.mapper.AccountEntityMapper
import io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.outbound.account.AccountOutPort
import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.exception.AccountNotFoundException
import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Account
import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Iban
import io.holixon.cqrshexagonaldemo.demoparent.transactions.framework.OutAdapter
import jakarta.transaction.Transactional
import mu.KLogging
import java.math.BigDecimal

@OutAdapter
Expand All @@ -14,11 +16,14 @@ open class AccountOutAdapter(
private val accountEntityMapper: AccountEntityMapper
) : AccountOutPort {

companion object : KLogging()

override fun findAccount(iban: Iban): Account? {
val entity = jpaAccountOutAdapter.findById(iban.value)
if (!entity.isPresent) return null
val entity = jpaAccountOutAdapter.findById(iban.value).orElseThrow {
AccountNotFoundException("Account not found for Iban: ${iban.value}")
}

return accountEntityMapper.toDomain(entity.get())
return accountEntityMapper.toDomain(entity)
}

override fun createAccount(account: Account): Account {
Expand All @@ -29,16 +34,11 @@ open class AccountOutAdapter(
return accountEntityMapper.toDomain(savedAccount)
}

//Refactor code similar to withdraw...
@Transactional
override fun deposit(iban: Iban, amount: BigDecimal): Account {
require(amount > BigDecimal.ZERO) {
"Amount to deposit must be above zero"
}

val entity = jpaAccountOutAdapter.findById(iban.value)
.orElseThrow {
IllegalArgumentException("Account not found for Iban: ${iban.value}")
AccountNotFoundException("Account not found for Iban: ${iban.value}")
}

val account = accountEntityMapper.toDomain(entity)
Expand All @@ -56,7 +56,7 @@ open class AccountOutAdapter(

val entity = jpaAccountOutAdapter.findById(iban.value)
.orElseThrow {
IllegalArgumentException("Account not found for Iban: ${iban.value}")
AccountNotFoundException("Account not found for Iban: ${iban.value}")
}

val account = accountEntityMapper.toDomain(entity)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.inbound.account

import java.math.BigDecimal

interface DepositAccountInPort {
fun deposit(accountNumber: String, amount: BigDecimal)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.event

import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Iban
import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Money

data class MoneyDepositedEvent(val iban: Iban, val balance: Money, val timeStamp: Long) : Event
28 changes: 28 additions & 0 deletions demo-transactions/src/main/resources/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Account'
/api/v1/account/deposit:
put:
tags:
- account
summary: >
Deposit money
description: >
Deposit money for a specified account
operationId: deposit
requestBody:
required: true
content:
application/json:
schema:
properties:
accountNumber:
type: string
amount:
type: number
required:
- accountNumber
- amount
responses:
'204':
description: Money Deposited
'404':
description: No Account found

components:
schemas:
Account:
Expand Down

0 comments on commit eb147dc

Please sign in to comment.