Last active
October 3, 2018 08:03
-
-
Save chiller/61142ae9c20894df3e90513565b97b16 to your computer and use it in GitHub Desktop.
CQRS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Command | |
case class CreatePotato(sort: String) extends Command | |
case class CookPotato() extends Command | |
case class EatPotato() extends Command | |
object Command { | |
import cats.data.State | |
def dispatch(c: Command): State[List[String], Unit] = State( s => { | |
c match { | |
case EatPotato() => (s.tail, ()) | |
case CreatePotato(sort) => (sort :: s, ()) | |
case CookPotato() => (s, ()) | |
} | |
}) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.data.State | |
object Main extends App { | |
// parse command from json | |
val command = CreatePotato("samsø") | |
val program = for { | |
_ <- Command.dispatch(command) | |
_ <- Command.dispatch(command) | |
_ <- Command.dispatch(command) | |
_ <- Command.dispatch(command) | |
_ <- Command.dispatch(command) | |
_ <- Command.dispatch(EatPotato()) | |
potatoes <- Query.list() | |
} yield potatoes | |
println(program.run(List()).value) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.data.State | |
object Query { | |
def list(): State[List[String], List[String]] = State(s => (s,s)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment