mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 10:28:27 +00:00
Add routes for storing/retrieving client settings
This commit is contained in:
@ -38,6 +38,7 @@ trait BackendApp[F[_]] {
|
||||
def folder: OFolder[F]
|
||||
def customFields: OCustomFields[F]
|
||||
def simpleSearch: OSimpleSearch[F]
|
||||
def clientSettings: OClientSettings[F]
|
||||
}
|
||||
|
||||
object BackendApp {
|
||||
@ -73,26 +74,28 @@ object BackendApp {
|
||||
folderImpl <- OFolder(store)
|
||||
customFieldsImpl <- OCustomFields(store)
|
||||
simpleSearchImpl = OSimpleSearch(fulltextImpl, itemSearchImpl)
|
||||
clientSettingsImpl <- OClientSettings(store)
|
||||
} yield new BackendApp[F] {
|
||||
val login = loginImpl
|
||||
val signup = signupImpl
|
||||
val collective = collImpl
|
||||
val source = sourceImpl
|
||||
val tag = tagImpl
|
||||
val equipment = equipImpl
|
||||
val organization = orgImpl
|
||||
val upload = uploadImpl
|
||||
val node = nodeImpl
|
||||
val job = jobImpl
|
||||
val item = itemImpl
|
||||
val itemSearch = itemSearchImpl
|
||||
val fulltext = fulltextImpl
|
||||
val mail = mailImpl
|
||||
val joex = joexImpl
|
||||
val userTask = userTaskImpl
|
||||
val folder = folderImpl
|
||||
val customFields = customFieldsImpl
|
||||
val simpleSearch = simpleSearchImpl
|
||||
val login = loginImpl
|
||||
val signup = signupImpl
|
||||
val collective = collImpl
|
||||
val source = sourceImpl
|
||||
val tag = tagImpl
|
||||
val equipment = equipImpl
|
||||
val organization = orgImpl
|
||||
val upload = uploadImpl
|
||||
val node = nodeImpl
|
||||
val job = jobImpl
|
||||
val item = itemImpl
|
||||
val itemSearch = itemSearchImpl
|
||||
val fulltext = fulltextImpl
|
||||
val mail = mailImpl
|
||||
val joex = joexImpl
|
||||
val userTask = userTaskImpl
|
||||
val folder = folderImpl
|
||||
val customFields = customFieldsImpl
|
||||
val simpleSearch = simpleSearchImpl
|
||||
val clientSettings = clientSettingsImpl
|
||||
}
|
||||
|
||||
def apply[F[_]: ConcurrentEffect: ContextShift](
|
||||
|
@ -0,0 +1,78 @@
|
||||
package docspell.backend.ops
|
||||
|
||||
import cats.data.OptionT
|
||||
import cats.effect.{Effect, Resource}
|
||||
import cats.implicits._
|
||||
|
||||
import docspell.common.AccountId
|
||||
import docspell.common._
|
||||
import docspell.common.syntax.all._
|
||||
import docspell.store.Store
|
||||
import docspell.store.records.RClientSettings
|
||||
import docspell.store.records.RUser
|
||||
|
||||
import io.circe.Json
|
||||
import org.log4s._
|
||||
|
||||
trait OClientSettings[F[_]] {
|
||||
|
||||
def delete(clientId: Ident, account: AccountId): F[Boolean]
|
||||
def save(clientId: Ident, account: AccountId, data: Json): F[Unit]
|
||||
def load(clientId: Ident, account: AccountId): F[Option[RClientSettings]]
|
||||
|
||||
}
|
||||
|
||||
object OClientSettings {
|
||||
private[this] val logger = getLogger
|
||||
|
||||
def apply[F[_]: Effect](store: Store[F]): Resource[F, OClientSettings[F]] =
|
||||
Resource.pure[F, OClientSettings[F]](new OClientSettings[F] {
|
||||
|
||||
private def getUserId(account: AccountId): OptionT[F, Ident] =
|
||||
OptionT(store.transact(RUser.findByAccount(account))).map(_.uid)
|
||||
|
||||
def delete(clientId: Ident, account: AccountId): F[Boolean] =
|
||||
(for {
|
||||
_ <- OptionT.liftF(
|
||||
logger.fdebug(
|
||||
s"Deleting client settings for client ${clientId.id} and account $account"
|
||||
)
|
||||
)
|
||||
userId <- getUserId(account)
|
||||
n <- OptionT.liftF(
|
||||
store.transact(
|
||||
RClientSettings.delete(clientId, userId)
|
||||
)
|
||||
)
|
||||
} yield n > 0).getOrElse(false)
|
||||
|
||||
def save(clientId: Ident, account: AccountId, data: Json): F[Unit] =
|
||||
(for {
|
||||
_ <- OptionT.liftF(
|
||||
logger.fdebug(
|
||||
s"Storing client settings for client ${clientId.id} and account $account"
|
||||
)
|
||||
)
|
||||
userId <- getUserId(account)
|
||||
n <- OptionT.liftF(
|
||||
store.transact(RClientSettings.upsert(clientId, userId, data))
|
||||
)
|
||||
_ <- OptionT.liftF(
|
||||
if (n <= 0) Effect[F].raiseError(new Exception("No rows updated!"))
|
||||
else ().pure[F]
|
||||
)
|
||||
} yield ()).getOrElse(())
|
||||
|
||||
def load(clientId: Ident, account: AccountId): F[Option[RClientSettings]] =
|
||||
(for {
|
||||
_ <- OptionT.liftF(
|
||||
logger.fdebug(
|
||||
s"Loading client settings for client ${clientId.id} and account $account"
|
||||
)
|
||||
)
|
||||
userId <- getUserId(account)
|
||||
data <- OptionT(store.transact(RClientSettings.find(clientId, userId)))
|
||||
} yield data).value
|
||||
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user