From 88efe13209526f5770ba04a284f2209bf8fbe8b7 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Sat, 11 Jan 2020 01:51:05 +0100 Subject: [PATCH] Fix item route responses Also avoid storing empty strings in a nullable field. --- .../src/main/scala/docspell/common/Ident.scala | 2 +- .../docspell/restserver/routes/ItemRoutes.scala | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/common/src/main/scala/docspell/common/Ident.scala b/modules/common/src/main/scala/docspell/common/Ident.scala index e17d54f0..6314dffc 100644 --- a/modules/common/src/main/scala/docspell/common/Ident.scala +++ b/modules/common/src/main/scala/docspell/common/Ident.scala @@ -32,7 +32,7 @@ object Ident { def fromString(s: String): Either[String, Ident] = if (s.forall(chars.contains)) Right(new Ident(s)) - else Left(s"Invalid identifier: $s. Allowed chars: ${chars.mkString}") + else Left(s"Invalid identifier: '$s'. Allowed chars: ${chars.toList.sorted.mkString}") def fromBytes(bytes: ByteVector): Ident = unsafe(bytes.toBase58) diff --git a/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala index a35a69cc..7aee0ea2 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala @@ -96,15 +96,15 @@ object ItemRoutes { case req @ POST -> Root / Ident(id) / "notes" => for { text <- req.as[OptionalText] - res <- backend.item.setNotes(id, text.text, user.account.collective) - resp <- Ok(Conversions.basicResult(res, "Concerned equipment updated")) + res <- backend.item.setNotes(id, text.text.notEmpty, user.account.collective) + resp <- Ok(Conversions.basicResult(res, "Notes updated")) } yield resp case req @ POST -> Root / Ident(id) / "name" => for { text <- req.as[OptionalText] - res <- backend.item.setName(id, text.text.getOrElse(""), user.account.collective) - resp <- Ok(Conversions.basicResult(res, "Concerned equipment updated")) + res <- backend.item.setName(id, text.text.notEmpty.getOrElse(""), user.account.collective) + resp <- Ok(Conversions.basicResult(res, "Name updated")) } yield resp case req @ POST -> Root / Ident(id) / "duedate" => @@ -138,4 +138,10 @@ object ItemRoutes { } yield resp } } + + + final implicit class OptionString(opt: Option[String]) { + def notEmpty: Option[String] = + opt.map(_.trim).filter(_.nonEmpty) + } }