Skip to content

Commit

Permalink
Fixed a bug in event sourced implementation where the first event was…
Browse files Browse the repository at this point in the history
… materialized before task execution
  • Loading branch information
debasishg committed Aug 22, 2016
1 parent ac202a5 commit 48664c3
Showing 1 changed file with 48 additions and 32 deletions.
80 changes: 48 additions & 32 deletions src/main/scala/frdomain/ch8/cqrs/service/AccountService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,38 +109,46 @@ object RepositoryBackedAccountInterpreter extends RepositoryBackedInterpreter {

private def handleCommand[A](e: Event[A]): Task[A] = e match {

case o @ Opened(no, name, odate, _) => validateOpen(no).fold(
err => fail(new RuntimeException(err)),
_ => now {
val a = Account(no, name, odate.get)
eventLog.put(no, o)
a
}
)

case c @ Closed(no, cdate, _) => validateClose(no, cdate).fold(
err => fail(new RuntimeException(err)),
currentState => now {
eventLog.put(no, c)
updateState(c, currentState)(no)
}
)

case d @ Debited(no, amount, _) => validateDebit(no, amount).fold(
err => fail(new RuntimeException(err)),
currentState => now {
eventLog.put(no, d)
updateState(d, currentState)(no)
}
)

case r @ Credited(no, amount, _) => validateCredit(no).fold(
err => fail(new RuntimeException(err)),
currentState => now {
eventLog.put(no, r)
updateState(r, currentState)(no)
}
)
case o @ Opened(no, name, odate, _) => Task {
validateOpen(no).fold(
err => throw new RuntimeException(err),
_ => {
val a = Account(no, name, odate.get)
eventLog.put(no, o)
a
}
)
}

case c @ Closed(no, cdate, _) => Task {
validateClose(no, cdate).fold(
err => throw new RuntimeException(err),
currentState => {
eventLog.put(no, c)
updateState(c, currentState)(no)
}
)
}

case d @ Debited(no, amount, _) => Task {
validateDebit(no, amount).fold(
err => throw new RuntimeException(err),
currentState => {
eventLog.put(no, d)
updateState(d, currentState)(no)
}
)
}

case r @ Credited(no, amount, _) => Task {
validateCredit(no).fold(
err => throw new RuntimeException(err),
currentState => {
eventLog.put(no, r)
updateState(r, currentState)(no)
}
)
}
}
}

Expand All @@ -166,4 +174,12 @@ object Scripts extends AccountCommands {
_ <- credit(a.no, 30000)
d <- debit(a.no, 50000)
} yield d

val comp =
for {
a <- open("a1", "debasish ghosh", Some(today))
_ <- credit(a.no, 10000)
_ <- credit(a.no, 30000)
d <- debit(a.no, 23000)
} yield d
}

0 comments on commit 48664c3

Please sign in to comment.