Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
feat(zio-http): add TodoRepository with in memory implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMtnez committed Sep 5, 2023
1 parent 081593f commit 1ade0fa
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ lazy val `todobackend-zio-http` = project
"dev.zio" %% "zio-http" % zioHttpV
)
)
.dependsOn(common)
.enablePlugins(BuildInfoPlugin)

lazy val root = project
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package es.jesusmtnez.todobackend

import zio.*
import es.jesusmtnez.todobackend.domain.TodoItem

import java.util.UUID

trait TodoRepository:
def create(title: String, order: Option[Int]): Task[Option[TodoItem]]
def delete(id: UUID): Task[Unit]
def deleteAll(): Task[Unit]
def getAll(): Task[List[TodoItem]]
def getById(id: UUID): Task[Option[TodoItem]]
def update(
id: UUID,
title: Option[String],
completed: Option[Boolean],
order: Option[Int]
): Task[Option[TodoItem]]

final case class InMemoryRepository(
private val store: Ref[Map[UUID, TodoItem]]
) extends TodoRepository:

override def create(
title: String,
order: Option[Int]
): UIO[Option[TodoItem]] = {
for {
id <- ZIO.succeedUnsafe(_ => UUID.randomUUID())
result <- store
.updateAndGet { state =>
state ++ Map(
id -> TodoItem.uncompleted(id, title, order.getOrElse(0))
)
}
.map(_.get(id))
} yield result
}

override def delete(id: UUID): UIO[Unit] =
store.update(_.removed(id))

override def deleteAll(): UIO[Unit] =
store.set(Map.empty[UUID, TodoItem])

override def getById(id: UUID): UIO[Option[TodoItem]] =
store.get.map(_.get(id))

override def getAll(): UIO[List[TodoItem]] =
store.get.map(_.values.toList)

override def update(
id: UUID,
title: Option[String],
completed: Option[Boolean],
order: Option[Int]
): UIO[Option[TodoItem]] =
store
.updateAndGet { items =>
items
.get(id)
.map { todo =>
val newTitle = title.getOrElse(todo.title)
val newCompleted = completed.getOrElse(todo.completed)
val newOrder = order.getOrElse(todo.order)
todo.copy(
title = newTitle,
completed = newCompleted,
order = newOrder
)
}
.fold(items)(item => items.removed(id) + (id -> item))
}
.map(_.get(id))

object InMemoryRepository:
val layer: ZLayer[Any, Nothing, TodoRepository] =
ZLayer.fromZIO(
Ref.make(Map.empty[UUID, TodoItem]).map(InMemoryRepository(_))
)

0 comments on commit 1ade0fa

Please sign in to comment.