Implement clients API

This commit is contained in:
Paul-Henri Froidmont 2025-03-03 00:24:13 +01:00
parent 91584c18d5
commit 31014d1a0c
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
14 changed files with 474 additions and 228 deletions

View file

@ -2,25 +2,42 @@ package lu.foyer
import zio.*
import java.util.UUID
import zio.schema.Schema
final case class Entity[T](entityId: UUID, data: T, version: Long)
final case class Event[T](entityId: UUID, data: T, eventId: UUID)
trait StateRepository[State] extends Repository[Entity[State], UUID]
trait EventRepository[State] extends Repository[Event[State], UUID]
trait StateRepository[Data] extends Repository[Entity[Data], UUID]
trait EventRepository[Data] extends Repository[Event[Data], UUID]:
def fetchOne(entityId: UUID, eventId: UUID): Task[Option[Event[Data]]]
def fetchMany(entityId: UUID, page: Page): Task[Paged[Event[Data]]]
trait Reducer[Event, State]:
def reduce(event: Event): Option[State]
def reduce(state: State, event: Event): Option[State]
def fromEmpty: PartialFunction[Event, State]
def fromState: PartialFunction[(State, Event), State]
trait CommandHandler[Command, Event, State]:
def reduce(event: Event): Option[State] =
fromEmpty.lift(event)
def reduce(state: State, event: Event): Option[State] =
fromState.lift((state, event))
trait CommandHandler[+Command, +Event, +State]:
def name: String
def isCreate: Boolean
inline def isUpdate: Boolean = !isCreate
def commandSchema: Schema[?]
trait CommandHandlerCreate[Command, Event, State] extends CommandHandler[Command, Event, State]:
trait CommandHandlerCreate[Command: Schema, Event] extends CommandHandler[Command, Event, Nothing]:
def onCommand(entityId: UUID, command: Command): Task[Event]
val isCreate = true
val commandSchema = summon[Schema[Command]]
trait CommandHandlerUpdate[Command, Event, State] extends CommandHandler[Command, Event, State]:
trait CommandHandlerUpdate[Command: Schema, Event, State]
extends CommandHandler[Command, Event, State]:
def onCommand(entityId: UUID, state: State, command: Command): Task[Event]
val isCreate = false
val commandSchema = summon[Schema[Command]]
class CommandEngine[Command, Event, State](
val handlers: List[CommandHandler[Command, Event, State]],
@ -28,7 +45,8 @@ class CommandEngine[Command, Event, State](
val eventRepo: EventRepository[Event],
val stateRepo: StateRepository[State]):
def handleCommand(command: Command, name: String, entityId: UUID): Task[(Event, State)] =
def handleCommand(command: Command, name: String, entityId: UUID)
: Task[(lu.foyer.Event[Event], lu.foyer.Entity[State])] =
for
handler <- ZIO
.succeed(handlers.find(_.name == name))
@ -39,13 +57,12 @@ class CommandEngine[Command, Event, State](
ZIO
.succeed(newStateOption)
.someOrFail(new IllegalArgumentException("Reducer cannot resolve state transition"))
newEntity <-
Random.nextUUID.map(Entity(_, newState, entityOption.map(_.version).getOrElse(1)))
_ <- if entityOption.isEmpty then stateRepo.insert(newEntity)
newEntity = Entity(entityId, newState, entityOption.map(_.version).getOrElse(1))
_ <- if entityOption.isEmpty then stateRepo.insert(newEntity.entityId, newEntity)
else stateRepo.update(newEntity.entityId, newEntity)
eventEntity <- Random.nextUUID.map(Event(newEntity.entityId, event, _))
_ <- eventRepo.insert(eventEntity)
yield (event, newState)
_ <- eventRepo.insert(eventEntity.eventId, eventEntity)
yield (eventEntity, newEntity)
private def transition(
command: Command,
@ -54,21 +71,23 @@ class CommandEngine[Command, Event, State](
entityOption: Option[Entity[State]],
handler: CommandHandler[Command, Event, State]
): Task[(Event, Option[State])] = (entityOption, handler) match
case (None, h: CommandHandlerCreate[Command, Event, State]) =>
h.onCommand(entityId, command)
case (None, h) if !h.isUpdate =>
h.asInstanceOf[CommandHandlerCreate[Command, Event]]
.onCommand(entityId, command)
.map(event => (event, reducer.reduce(event)))
case (Some(entity), h: CommandHandlerUpdate[Command, Event, State]) =>
h.onCommand(entityId, entity.data, command)
case (Some(entity), h) if h.isUpdate =>
h.asInstanceOf[CommandHandlerUpdate[Command, Event, State]]
.onCommand(entityId, entity.data, command)
.map(event => (event, reducer.reduce(entity.data, event)))
case (Some(_), _: CommandHandlerCreate[Command, Event, State]) =>
case (Some(_), h) if !h.isUpdate =>
ZIO.fail(
new IllegalArgumentException(s"State already exists when applying create command $name")
)
case (None, _: CommandHandlerUpdate[Command, Event, State]) =>
case (None, h) if h.isUpdate =>
ZIO.fail(new IllegalArgumentException(s"No state found to apply the update command $name"))
case _ => ZIO.fail(new IllegalArgumentException("Impossible state"))
end CommandEngine
object CommandEngine:
def layer[Command, Event, State](using Tag[Command], Tag[Event], Tag[State]) =
def layer[Command: Tag, Event: Tag, State: Tag] =
ZLayer.fromFunction(CommandEngine[Command, Event, State](_, _, _, _))