Support JWT

This commit is contained in:
Paul-Henri Froidmont 2025-11-04 13:42:19 +01:00
parent 25318cd6de
commit aa9d60e4d1
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
9 changed files with 96 additions and 19 deletions

View file

@ -3,6 +3,8 @@ package lu.foyer
import zio.*
import zio.schema.Schema
import lu.foyer.auth.UserInfo
final case class Entity[T](entityId: String, data: T, version: Long)
final case class Event[T](entityId: String, data: T, eventId: String)
@ -28,13 +30,14 @@ trait CommandHandler[+Command, +Event, +State]:
def commandSchema: Schema[?]
trait CommandHandlerCreate[Command: Schema, Event] extends CommandHandler[Command, Event, Nothing]:
def onCommand(entityId: String, command: Command): IO[JsonApiError | Throwable, Event]
def onCommand(entityId: String, command: Command): ZIO[UserInfo, JsonApiError | Throwable, Event]
val isCreate = true
val commandSchema = summon[Schema[Command]]
trait CommandHandlerUpdate[Command: Schema, Event, State]
extends CommandHandler[Command, Event, State]:
def onCommand(entityId: String, state: State, command: Command) : IO[JsonApiError | Throwable, Event]
def onCommand(entityId: String, state: State, command: Command)
: ZIO[UserInfo, JsonApiError | Throwable, Event]
val isCreate = false
val commandSchema = summon[Schema[Command]]
@ -45,7 +48,7 @@ class CommandEngine[Command, Event, State](
val stateRepo: StateRepository[State]):
def handleCommand(command: Command, name: String, entityId: String)
: IO[JsonApiError | Throwable, (lu.foyer.Event[Event], lu.foyer.Entity[State])] =
: ZIO[UserInfo, JsonApiError | Throwable, (lu.foyer.Event[Event], lu.foyer.Entity[State])] =
for
handler <- ZIO
.succeed(handlers.find(_.name == name))
@ -70,7 +73,7 @@ class CommandEngine[Command, Event, State](
entityId: String,
entityOption: Option[Entity[State]],
handler: CommandHandler[Command, Event, State]
): IO[JsonApiError | Throwable, (Event, Option[State])] = (entityOption, handler) match
): ZIO[UserInfo, JsonApiError | Throwable, (Event, Option[State])] = (entityOption, handler) match
case (None, h) if !h.isUpdate =>
h.asInstanceOf[CommandHandlerCreate[Command, Event]]
.onCommand(entityId, command)