Refactoring on classes and modelling

This commit is contained in:
Sambo Chea 2020-08-21 10:59:20 +07:00
parent 4547fb366e
commit a85390ffb3
4 changed files with 111 additions and 100 deletions

View File

@ -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
@ -42,82 +22,3 @@ class DefaultController {
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
)

View File

@ -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)
}
}

View File

@ -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
)

View 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