Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Oct 21, 2023
1 parent d8c2822 commit 10e2a3d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions modules/backend/src/main/scala/database.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ trait Database:
def create(key: Key, value: Option[Value]): IO[Unit]
def getAll(): IO[Stream[IO, Pair]]
def delete(key: Key): IO[Int]
def inc(key: Key): IO[Pair]
def dec(key: Key): IO[Pair]
def inc(key: Key): IO[Option[Pair]]
def dec(key: Key): IO[Option[Pair]]
def update(key: Key, value: Value): IO[Pair]

object Database:
Expand All @@ -26,15 +26,15 @@ object Database:
)
}

override def dec(key: Key): IO[Pair] = pool.use{
override def dec(key: Key): IO[Option[Pair]] = pool.use{
s => s.prepare(operations.decrementValue).flatMap(
_.unique(key)
_.option(key)
)
}

override def inc(key: Key): IO[Pair] = pool.use{
override def inc(key: Key): IO[Option[Pair]] = pool.use{
s => s.prepare(operations.incrementValue).flatMap(
_.unique(key)
_.option(key)
)
}

Expand Down
4 changes: 2 additions & 2 deletions modules/backend/src/main/scala/service.hello.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class HelloImplementation(logger: Scribe[IO], db: Database)
}

override def dec(key: Key): IO[Unit] =
db.dec(key).void
orNotFound(db.dec(key).map(_.map(_ => 1))).void
// (
// err =>
// logger.error(err) *>
Expand All @@ -25,7 +25,7 @@ class HelloImplementation(logger: Scribe[IO], db: Database)
// )

override def inc(key: Key): IO[Unit] =
db.inc(key).void
orNotFound(db.inc(key).map(_.map(_ => 1))).void
// .redeem(
// err =>
// IO.println("Inc error") *>
Expand Down
8 changes: 4 additions & 4 deletions modules/tests/src/test/scala/stub/InMemoryDatabase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class InMemoryDatabase private (rf: Ref[IO, Map[Key, Value]]) extends Database:
else throw KeyNotFound()
}

override def dec(key: Key): IO[Pair] = rf.modify[Pair] { mp =>
override def dec(key: Key): IO[Option[Pair]] = rf.modify[Option[Pair]] { mp =>
if mp.contains(key) then
val newMap = mp.updatedWith(key)(_.map(v => Value(v.value - 1)))
(newMap, Pair(key, newMap(key)))
(newMap, Some(Pair(key, newMap(key))))
else throw KeyNotFound()
}

override def inc(key: Key): IO[Pair] = rf.modify[Pair] { mp =>
override def inc(key: Key): IO[Option[Pair]] = rf.modify[Option[Pair]] { mp =>
if mp.contains(key) then
val newMap = mp.updatedWith(key)(_.map(v => Value(v.value + 1)))
(newMap, Pair(key, newMap(key)))
(newMap, Some(Pair(key, newMap(key))))
else
throw KeyNotFound()
}
Expand Down

0 comments on commit 10e2a3d

Please sign in to comment.