/
RoleRepoLive.scala
70 lines (60 loc) · 1.66 KB
/
RoleRepoLive.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package dsp.role.repo.impl
import zio._
import zio.stm.TMap
import java.util.UUID
import dsp.role.api.RoleRepo
import dsp.role.domain.Role
import dsp.valueobjects.Id.RoleId
/**
* Role repo live implementation.
*
* @param roles a map of roles
*/
final case class RoleRepoLive(
roles: TMap[UUID, Role]
) extends RoleRepo {
/**
* @inheritDoc
*/
override def storeRole(r: Role): UIO[RoleId] =
(for {
_ <- roles.put(r.id.uuid, r)
} yield r.id).commit.tap(_ => ZIO.logInfo(s"Stored role with ID: ${r.id.uuid}"))
/**
* @inheritDoc
*/
override def getRoles(): UIO[List[Role]] =
roles.values.commit.tap(rolesList => ZIO.logInfo(s"Number of roles found: ${rolesList.size}"))
/**
* @inheritDoc
*/
override def getRoleById(id: RoleId): IO[Option[Nothing], Role] =
roles
.get(id.uuid)
.commit
.some
.tapBoth(
_ => ZIO.logInfo(s"Not found the role with ID: ${id.uuid}"),
_ => ZIO.logInfo(s"Found role by ID: ${id.uuid}")
)
/**
* @inheritDoc
*/
override def deleteRole(id: RoleId): IO[Option[Nothing], RoleId] =
(for {
_ <- roles.get(id.uuid).some
_ <- roles.delete(id.uuid)
} yield id).commit.tap(_ => ZIO.logInfo(s"Deleted role: ${id.uuid}"))
}
object RoleRepoLive {
val layer: ZLayer[Any, Nothing, RoleRepo] =
ZLayer {
for {
roles <- TMap.empty[UUID, Role].commit
} yield RoleRepoLive(roles)
}.tap(_ => ZIO.logInfo(">>> Role repository initialized <<<"))
}