Allow managing clientsettings per collective

This commit is contained in:
eikek
2021-12-23 22:54:14 +01:00
parent 706cfaeb05
commit 05229a7e9c
3 changed files with 156 additions and 10 deletions

View File

@ -19,6 +19,7 @@ import org.http4s.HttpRoutes
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe.CirceEntityEncoder._
import org.http4s.dsl.Http4sDsl
import cats.kernel.Semigroup
object ClientSettingsRoutes {
@ -27,14 +28,27 @@ object ClientSettingsRoutes {
import dsl._
HttpRoutes.of {
case req @ PUT -> Root / Ident(clientId) =>
case GET -> Root / Ident(clientId) =>
for {
collData <- backend.clientSettings.loadCollective(clientId, user.account)
userData <- backend.clientSettings.loadUser(clientId, user.account)
mergedData = collData.map(_.settingsData) |+| userData.map(_.settingsData)
res <- mergedData match {
case Some(j) => Ok(j)
case None => NotFound()
}
} yield res
case req @ PUT -> Root / "user" / Ident(clientId) =>
for {
data <- req.as[Json]
_ <- backend.clientSettings.saveUser(clientId, user.account, data)
res <- Ok(BasicResult(true, "Settings stored"))
} yield res
case GET -> Root / Ident(clientId) =>
case GET -> Root / "user" / Ident(clientId) =>
for {
data <- backend.clientSettings.loadUser(clientId, user.account)
res <- data match {
@ -43,7 +57,7 @@ object ClientSettingsRoutes {
}
} yield res
case DELETE -> Root / Ident(clientId) =>
case DELETE -> Root / "user" / Ident(clientId) =>
for {
flag <- backend.clientSettings.deleteUser(clientId, user.account)
res <- Ok(
@ -53,6 +67,36 @@ object ClientSettingsRoutes {
)
)
} yield res
case req @ PUT -> Root / "collective" / Ident(clientId) =>
for {
data <- req.as[Json]
_ <- backend.clientSettings.saveCollective(clientId, user.account, data)
res <- Ok(BasicResult(true, "Settings stored"))
} yield res
case GET -> Root / "collective" / Ident(clientId) =>
for {
data <- backend.clientSettings.loadCollective(clientId, user.account)
res <- data match {
case Some(d) => Ok(d.settingsData)
case None => NotFound()
}
} yield res
case DELETE -> Root / "collective" / Ident(clientId) =>
for {
flag <- backend.clientSettings.deleteCollective(clientId, user.account)
res <- Ok(
BasicResult(
flag,
if (flag) "Settings deleted" else "Deleting settings failed"
)
)
} yield res
}
}
implicit def jsonSemigroup: Semigroup[Json] =
Semigroup.instance((a1, a2) => a1.deepMerge(a2))
}