/
RoleHandler.scala
147 lines (135 loc) · 4.61 KB
/
RoleHandler.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* 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.handler
import dsp.errors.NotFoundException
import dsp.errors.RequestRejectedException
import dsp.role.api.RoleRepo
import dsp.role.domain.Role
import dsp.role.domain.RoleUser
import dsp.valueobjects.Id.RoleId
import dsp.valueobjects.Permission
import dsp.valueobjects.Role._
import zio.ZIO
import zio._
/**
* The role handler.
*
* @param repo the role repository
*/
final case class RoleHandler(repo: RoleRepo) {
/**
* Retrieves all roles (sorted by IRI).
*/
def getRoles(): UIO[List[Role]] =
repo
.getRoles()
.map(_.sorted)
.tap(_ => ZIO.logInfo("Retrieved all roles."))
/**
* Retrieves the role by ID.
*
* @param id of the role to be retrieved
*/
def getRoleById(id: RoleId): IO[NotFoundException, Role] =
for {
role <- repo
.getRoleById(id)
.mapError(_ => NotFoundException(s"Not found the role with ID: $id"))
.tap(_ => ZIO.logInfo(s"Found the role by ID: $id"))
} yield role
/**
* Creates a new role.
*
* @param name the role's name
* @param description the role's description
* @param users the role's users
* @param permission the role's permission
* @return the [[RoleId]] if succeed, [[Throwable]] if fail
*/
def createRole(
name: LangString,
description: LangString,
users: List[RoleUser],
permission: Permission
): IO[Throwable, RoleId] =
(for {
id <- RoleId.make().toZIO
role <- Role.make(id, name, description, users, permission).toZIO
storedRole <- repo.storeRole(role)
} yield storedRole).tap(id => ZIO.logInfo(s"Created role with ID: $id"))
/**
* Updates the name of a role.
*
* @param id the roles's ID
* @param newValue the new name value
* @return the [[RoleId]] if succeed, [[RequestRejectedException]] if fail
*/
def updateName(id: RoleId, newValue: LangString): IO[RequestRejectedException, RoleId] =
(for {
role <- getRoleById(id)
updatedRole <- role.updateName(newValue).toZIO
_ <- repo.storeRole(updatedRole)
} yield id).tap(_ => ZIO.logInfo(s"Updated name with new value: ${newValue.value}"))
/**
* Updates the description of a role.
*
* @param id the roles's ID
* @param newValue the new description value
* @return the [[RoleId]] if succeed, [[RequestRejectedException]] if fail
*/
def updateDescription(id: RoleId, newValue: LangString): IO[RequestRejectedException, RoleId] =
(for {
role <- getRoleById(id)
updatedRole <- role.updateDescription(newValue).toZIO
_ <- repo.storeRole(updatedRole)
} yield id).tap(_ => ZIO.logInfo(s"Updated description with new value: $newValue"))
/**
* Updates the users of a role.
*
* @param id the roles's ID
* @param newValue the new users value
* @return the [[RoleId]] if succeed, [[RequestRejectedException]] if fail
*/
def updateUsers(id: RoleId, newValue: List[RoleUser]): IO[RequestRejectedException, RoleId] =
(for {
role <- getRoleById(id)
updatedRole <- role.updateUsers(newValue).toZIO
_ <- repo.storeRole(updatedRole)
} yield id).tap(_ => ZIO.logInfo(s"Updated users with new value: $newValue"))
/**
* Updates the permission of a role.
*
* @param id the roles's ID
* @param newValue the new permission value
* @return the [[RoleId]] if succeed, [[RequestRejectedException]] if fail
*/
def updatePermission(id: RoleId, newValue: Permission): IO[RequestRejectedException, RoleId] =
(for {
role <- getRoleById(id)
updatedRole <- role.updatePermission(newValue).toZIO
_ <- repo.storeRole(updatedRole)
} yield id).tap(_ => ZIO.logInfo(s"Updated permission with new value: $newValue"))
/**
* Deletes the role.
*
* @param id the roles's ID
* @return the [[RoleId]] if succeed, [[NotFoundException]] if fail
*/
def deleteRole(id: RoleId): IO[NotFoundException, RoleId] =
(for {
_ <- repo.deleteRole(id).mapError(_ => NotFoundException(s"Not found the role with ID: $id"))
} yield id).tap(_ => ZIO.logInfo(s"Deleted role with ID: $id"))
}
/**
* Companion object providing the layer with an initialized implementation
*/
object RoleHandler {
val layer: ZLayer[RoleRepo, Nothing, RoleHandler] =
ZLayer {
for {
repo <- ZIO.service[RoleRepo]
} yield RoleHandler(repo)
}.tap(_ => ZIO.logInfo(">>> Role Handler initilaized <<<"))
}