Refactoring on classes and modelling
This commit is contained in:
parent
4547fb366e
commit
a85390ffb3
@ -1,31 +1,11 @@
|
|||||||
package com.cubetiqs.demo.axon
|
package com.cubetiqs.demo.axon
|
||||||
|
|
||||||
import com.cubetiqs.demo.axon.command.CreditMoneyCommand
|
|
||||||
import com.cubetiqs.demo.axon.command.DebitMoneyCommand
|
|
||||||
import com.cubetiqs.demo.axon.event.AccountCreatedEvent
|
|
||||||
import com.cubetiqs.demo.axon.event.MoneyCreditedEvent
|
|
||||||
import com.cubetiqs.demo.axon.event.MoneyDebitedEvent
|
|
||||||
import com.cubetiqs.demo.axon.exception.InsufficientBalanceException
|
|
||||||
import org.axonframework.commandhandling.CommandHandler
|
|
||||||
import org.axonframework.eventsourcing.EventSourcingHandler
|
|
||||||
import org.axonframework.modelling.command.AggregateIdentifier
|
|
||||||
import org.axonframework.modelling.command.AggregateLifecycle
|
|
||||||
import org.axonframework.modelling.command.TargetAggregateIdentifier
|
|
||||||
import org.axonframework.spring.stereotype.Aggregate
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||||
import org.springframework.boot.runApplication
|
import org.springframework.boot.runApplication
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
import org.springframework.web.bind.annotation.RequestMapping
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import java.io.Serializable
|
|
||||||
import java.math.BigDecimal
|
|
||||||
import java.util.UUID
|
|
||||||
import javax.persistence.Entity
|
|
||||||
import javax.persistence.GeneratedValue
|
|
||||||
import javax.persistence.GenerationType
|
|
||||||
import javax.persistence.Id
|
|
||||||
import javax.persistence.Table
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
class AxonApplication
|
class AxonApplication
|
||||||
@ -41,83 +21,4 @@ class DefaultController {
|
|||||||
fun index(): ResponseEntity<Any> {
|
fun index(): ResponseEntity<Any> {
|
||||||
return ResponseEntity.ok("ok")
|
return ResponseEntity.ok("ok")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "bank_accounts")
|
|
||||||
data class BankAccount(
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
var id: UUID? = null,
|
|
||||||
|
|
||||||
var owner: String? = null,
|
|
||||||
|
|
||||||
var balance: BigDecimal? = null
|
|
||||||
) : Serializable
|
|
||||||
|
|
||||||
@Aggregate
|
|
||||||
class BankAccountAggregate(
|
|
||||||
@AggregateIdentifier
|
|
||||||
private var id: UUID? = null,
|
|
||||||
private var balance: BigDecimal? = null,
|
|
||||||
private var owner: String? = null
|
|
||||||
) {
|
|
||||||
@CommandHandler
|
|
||||||
constructor(command: CreateAccountCommand) {
|
|
||||||
AggregateLifecycle.apply(
|
|
||||||
AccountCreatedEvent(
|
|
||||||
command.accountId,
|
|
||||||
command.initialBalance,
|
|
||||||
command.owner
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventSourcingHandler
|
|
||||||
fun on(event: AccountCreatedEvent) {
|
|
||||||
id = event.id
|
|
||||||
owner = event.owner
|
|
||||||
balance = event.initialBalance
|
|
||||||
}
|
|
||||||
|
|
||||||
@CommandHandler
|
|
||||||
fun handles(command: CreditMoneyCommand) {
|
|
||||||
AggregateLifecycle.apply(
|
|
||||||
MoneyCreditedEvent(
|
|
||||||
command.accountId,
|
|
||||||
command.creditAmount
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventSourcingHandler
|
|
||||||
fun on(event: MoneyCreditedEvent) {
|
|
||||||
balance = balance!!.add(event.creditAmount)
|
|
||||||
}
|
|
||||||
|
|
||||||
@CommandHandler
|
|
||||||
fun handle(command: DebitMoneyCommand) {
|
|
||||||
AggregateLifecycle.apply(
|
|
||||||
MoneyDebitedEvent(
|
|
||||||
command.accountId,
|
|
||||||
command.debitAmount
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventSourcingHandler
|
|
||||||
@Throws(InsufficientBalanceException::class)
|
|
||||||
fun on(event: MoneyDebitedEvent) {
|
|
||||||
if (balance!! < event.debitAmount) {
|
|
||||||
throw InsufficientBalanceException(event.accountId!!, event.debitAmount!!)
|
|
||||||
}
|
|
||||||
balance = balance!!.subtract(event.debitAmount)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class CreateAccountCommand(
|
|
||||||
@TargetAggregateIdentifier
|
|
||||||
val accountId: UUID? = null,
|
|
||||||
val initialBalance: BigDecimal? = null,
|
|
||||||
val owner: String? = null
|
|
||||||
)
|
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.cubetiqs.demo.axon.aggregate
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.axon.command.CreateAccountCommand
|
||||||
|
import com.cubetiqs.demo.axon.command.CreditMoneyCommand
|
||||||
|
import com.cubetiqs.demo.axon.command.DebitMoneyCommand
|
||||||
|
import com.cubetiqs.demo.axon.event.AccountCreatedEvent
|
||||||
|
import com.cubetiqs.demo.axon.event.MoneyCreditedEvent
|
||||||
|
import com.cubetiqs.demo.axon.event.MoneyDebitedEvent
|
||||||
|
import com.cubetiqs.demo.axon.exception.InsufficientBalanceException
|
||||||
|
import org.axonframework.commandhandling.CommandHandler
|
||||||
|
import org.axonframework.eventsourcing.EventSourcingHandler
|
||||||
|
import org.axonframework.modelling.command.AggregateIdentifier
|
||||||
|
import org.axonframework.modelling.command.AggregateLifecycle
|
||||||
|
import org.axonframework.spring.stereotype.Aggregate
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
@Aggregate
|
||||||
|
class BankAccountAggregate(
|
||||||
|
@AggregateIdentifier
|
||||||
|
private var id: UUID? = null,
|
||||||
|
private var balance: BigDecimal? = null,
|
||||||
|
private var owner: String? = null
|
||||||
|
) {
|
||||||
|
@CommandHandler
|
||||||
|
constructor(command: CreateAccountCommand) {
|
||||||
|
AggregateLifecycle.apply(
|
||||||
|
AccountCreatedEvent(
|
||||||
|
command.accountId,
|
||||||
|
command.initialBalance,
|
||||||
|
command.owner
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventSourcingHandler
|
||||||
|
fun on(event: AccountCreatedEvent) {
|
||||||
|
id = event.id
|
||||||
|
owner = event.owner
|
||||||
|
balance = event.initialBalance
|
||||||
|
}
|
||||||
|
|
||||||
|
@CommandHandler
|
||||||
|
fun handles(command: CreditMoneyCommand) {
|
||||||
|
AggregateLifecycle.apply(
|
||||||
|
MoneyCreditedEvent(
|
||||||
|
command.accountId,
|
||||||
|
command.creditAmount
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventSourcingHandler
|
||||||
|
fun on(event: MoneyCreditedEvent) {
|
||||||
|
balance = balance!!.add(event.creditAmount)
|
||||||
|
}
|
||||||
|
|
||||||
|
@CommandHandler
|
||||||
|
fun handle(command: DebitMoneyCommand) {
|
||||||
|
AggregateLifecycle.apply(
|
||||||
|
MoneyDebitedEvent(
|
||||||
|
command.accountId,
|
||||||
|
command.debitAmount
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventSourcingHandler
|
||||||
|
@Throws(InsufficientBalanceException::class)
|
||||||
|
fun on(event: MoneyDebitedEvent) {
|
||||||
|
if (balance!! < event.debitAmount) {
|
||||||
|
throw InsufficientBalanceException(event.accountId!!, event.debitAmount!!)
|
||||||
|
}
|
||||||
|
balance = balance!!.subtract(event.debitAmount)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.cubetiqs.demo.axon.command
|
||||||
|
|
||||||
|
import org.axonframework.modelling.command.TargetAggregateIdentifier
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
data class CreateAccountCommand(
|
||||||
|
@TargetAggregateIdentifier
|
||||||
|
val accountId: UUID? = null,
|
||||||
|
val initialBalance: BigDecimal? = null,
|
||||||
|
val owner: String? = null
|
||||||
|
)
|
22
src/main/kotlin/com/cubetiqs/demo/axon/entity/BankAccount.kt
Normal file
22
src/main/kotlin/com/cubetiqs/demo/axon/entity/BankAccount.kt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package com.cubetiqs.demo.axon.entity
|
||||||
|
|
||||||
|
import java.io.Serializable
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import java.util.UUID
|
||||||
|
import javax.persistence.Entity
|
||||||
|
import javax.persistence.GeneratedValue
|
||||||
|
import javax.persistence.GenerationType
|
||||||
|
import javax.persistence.Id
|
||||||
|
import javax.persistence.Table
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "bank_accounts")
|
||||||
|
data class BankAccount(
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
var id: UUID? = null,
|
||||||
|
|
||||||
|
var owner: String? = null,
|
||||||
|
|
||||||
|
var balance: BigDecimal? = null
|
||||||
|
) : Serializable
|
Loading…
Reference in New Issue
Block a user