Streamline query parameters

This commit is contained in:
Eike Kettner 2020-01-12 01:13:03 +01:00
parent d535130c9e
commit c6f3920351
7 changed files with 28 additions and 39 deletions

View File

@ -17,6 +17,12 @@ object QueryParam {
QueryParamDecoder[String].map(s => QueryString(s.trim.toLowerCase))
// implicit val booleanDecoder: QueryParamDecoder[Boolean] =
// QueryParamDecoder.fromUnsafeCast(qp => Option(qp.value).exists(_.equalsIgnoreCase("true")))(
// "Boolean"
// )
object FullOpt extends OptionalQueryParamDecoderMatcher[Boolean]("full")
object ContactKindOpt extends OptionalQueryParamDecoderMatcher[ContactKind]("kind")

View File

@ -2,15 +2,17 @@ package docspell.restserver.routes
import cats.effect._
import cats.implicits._
import org.http4s.HttpRoutes
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe.CirceEntityEncoder._
import org.http4s.dsl.Http4sDsl
import docspell.backend.BackendApp
import docspell.backend.auth.AuthToken
import docspell.common.Ident
import docspell.restapi.model._
import docspell.restserver.conv.Conversions._
import org.http4s.HttpRoutes
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe.CirceEntityEncoder._
import org.http4s.dsl.Http4sDsl
import docspell.restserver.http4s.QueryParam
object EquipmentRoutes {
@ -19,10 +21,9 @@ object EquipmentRoutes {
import dsl._
HttpRoutes.of {
case req @ GET -> Root =>
val q = req.params.get("q").map(_.trim).filter(_.nonEmpty)
case GET -> Root :? QueryParam.QueryOpt(q) =>
for {
data <- backend.equipment.findAll(user.account, q)
data <- backend.equipment.findAll(user.account, q.map(_.q))
resp <- Ok(EquipmentList(data.map(mkEquipment).toList))
} yield resp

View File

@ -17,6 +17,7 @@ import docspell.restapi.model._
import docspell.store.records.RUserEmail
import docspell.store.EmilUtil
import docspell.restserver.conv.Conversions
import docspell.restserver.http4s.QueryParam
object MailSettingsRoutes {
@ -25,10 +26,9 @@ object MailSettingsRoutes {
import dsl._
HttpRoutes.of {
case req @ GET -> Root =>
val q = req.params.get("q").map(_.trim).filter(_.nonEmpty)
case GET -> Root :? QueryParam.QueryOpt(q) =>
for {
list <- backend.mail.getSettings(user.account, q)
list <- backend.mail.getSettings(user.account, q.map(_.q))
res = list.map(convert)
resp <- Ok(EmailSettingsList(res.toList))
} yield resp

View File

@ -7,7 +7,7 @@ import docspell.backend.auth.AuthToken
import docspell.common.Ident
import docspell.restapi.model._
import docspell.restserver.conv.Conversions._
import docspell.restserver.routes.ParamDecoder._
import docspell.restserver.http4s.QueryParam
import org.http4s.HttpRoutes
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe.CirceEntityEncoder._
@ -20,16 +20,15 @@ object OrganizationRoutes {
import dsl._
HttpRoutes.of {
case req @ GET -> Root :? FullQueryParamMatcher(full) =>
val q = req.params.get("q").map(_.trim).filter(_.nonEmpty)
case GET -> Root :? QueryParam.FullOpt(full) +& QueryParam.QueryOpt(q) =>
if (full.getOrElse(false)) {
for {
data <- backend.organization.findAllOrg(user.account, q)
data <- backend.organization.findAllOrg(user.account, q.map(_.q))
resp <- Ok(OrganizationList(data.map(mkOrg).toList))
} yield resp
} else {
for {
data <- backend.organization.findAllOrgRefs(user.account, q)
data <- backend.organization.findAllOrgRefs(user.account, q.map(_.q))
resp <- Ok(ReferenceList(data.map(mkIdName).toList))
} yield resp
}

View File

@ -1,15 +0,0 @@
package docspell.restserver.routes
import org.http4s.QueryParamDecoder
import org.http4s.dsl.impl.OptionalQueryParamDecoderMatcher
object ParamDecoder {
implicit val booleanDecoder: QueryParamDecoder[Boolean] =
QueryParamDecoder.fromUnsafeCast(qp => Option(qp.value).exists(_.equalsIgnoreCase("true")))(
"Boolean"
)
object FullQueryParamMatcher extends OptionalQueryParamDecoderMatcher[Boolean]("full")
}

View File

@ -8,7 +8,7 @@ import docspell.common.Ident
import docspell.common.syntax.all._
import docspell.restapi.model._
import docspell.restserver.conv.Conversions._
import docspell.restserver.routes.ParamDecoder._
import docspell.restserver.http4s.QueryParam
import org.http4s.HttpRoutes
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe.CirceEntityEncoder._
@ -23,16 +23,15 @@ object PersonRoutes {
import dsl._
HttpRoutes.of {
case req @ GET -> Root :? FullQueryParamMatcher(full) =>
val q = req.params.get("q").map(_.trim).filter(_.nonEmpty)
case GET -> Root :? QueryParam.FullOpt(full) +& QueryParam.QueryOpt(q) =>
if (full.getOrElse(false)) {
for {
data <- backend.organization.findAllPerson(user.account, q)
data <- backend.organization.findAllPerson(user.account, q.map(_.q))
resp <- Ok(PersonList(data.map(mkPerson).toList))
} yield resp
} else {
for {
data <- backend.organization.findAllPersonRefs(user.account, q)
data <- backend.organization.findAllPersonRefs(user.account, q.map(_.q))
resp <- Ok(ReferenceList(data.map(mkIdName).toList))
} yield resp
}

View File

@ -7,7 +7,7 @@ import docspell.backend.auth.AuthToken
import docspell.common.Ident
import docspell.restapi.model._
import docspell.restserver.conv.Conversions._
import docspell.restserver.http4s.ResponseGenerator
import docspell.restserver.http4s._
import org.http4s.HttpRoutes
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe.CirceEntityEncoder._
@ -20,10 +20,9 @@ object TagRoutes {
import dsl._
HttpRoutes.of {
case req @ GET -> Root =>
val q = req.params.get("q").map(_.trim).filter(_.nonEmpty)
case GET -> Root :? QueryParam.QueryOpt(q) =>
for {
all <- backend.tag.findAll(user.account, q)
all <- backend.tag.findAll(user.account, q.map(_.q))
resp <- Ok(TagList(all.size, all.map(mkTag).toList))
} yield resp