diff --git a/core/shared/src/main/scala/works/iterative/core/service/Factory.scala b/core/shared/src/main/scala/works/iterative/core/service/Factory.scala new file mode 100644 index 0000000..c3174e4 --- /dev/null +++ b/core/shared/src/main/scala/works/iterative/core/service/Factory.scala @@ -0,0 +1,17 @@ +package works.iterative.core +package service + +import zio.* + +trait EntityFactory[Entity, Seed]: + def make(seed: Seed): IO[UserMessage, Entity] + +class GeneratingFactory[Entity, Seed, Id]( + constructor: Seed => Validated[Id => Entity], + idGenerator: IdGenerator[Id] +) extends EntityFactory[Entity, Seed]: + override def make(seed: Seed): IO[UserMessage, Entity] = + for + construct <- constructor(seed).toZIO + id <- idGenerator.nextId + yield construct(id) diff --git a/core/shared/src/main/scala/works/iterative/core/service/Factory.scala b/core/shared/src/main/scala/works/iterative/core/service/Factory.scala new file mode 100644 index 0000000..c3174e4 --- /dev/null +++ b/core/shared/src/main/scala/works/iterative/core/service/Factory.scala @@ -0,0 +1,17 @@ +package works.iterative.core +package service + +import zio.* + +trait EntityFactory[Entity, Seed]: + def make(seed: Seed): IO[UserMessage, Entity] + +class GeneratingFactory[Entity, Seed, Id]( + constructor: Seed => Validated[Id => Entity], + idGenerator: IdGenerator[Id] +) extends EntityFactory[Entity, Seed]: + override def make(seed: Seed): IO[UserMessage, Entity] = + for + construct <- constructor(seed).toZIO + id <- idGenerator.nextId + yield construct(id) diff --git a/core/shared/src/main/scala/works/iterative/core/service/IdGenerator.scala b/core/shared/src/main/scala/works/iterative/core/service/IdGenerator.scala new file mode 100644 index 0000000..5174e05 --- /dev/null +++ b/core/shared/src/main/scala/works/iterative/core/service/IdGenerator.scala @@ -0,0 +1,6 @@ +package works.iterative.core.service + +import zio.* + +trait IdGenerator[A]: + def nextId: UIO[A]