From 53a6957ab217ee8d7b65e5b609dd2221e85846cd Mon Sep 17 00:00:00 2001 From: To-om Date: Thu, 29 Jun 2017 12:07:31 +0200 Subject: [PATCH] #20 Change format of existence API output and fix parsing error --- app/org/elastic4play/controllers/DBListCtrl.scala | 6 +++--- app/org/elastic4play/services/DBList.scala | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/org/elastic4play/controllers/DBListCtrl.scala b/app/org/elastic4play/controllers/DBListCtrl.scala index 8030a76..b13ae40 100644 --- a/app/org/elastic4play/controllers/DBListCtrl.scala +++ b/app/org/elastic4play/controllers/DBListCtrl.scala @@ -2,9 +2,9 @@ package org.elastic4play.controllers import javax.inject.{ Inject, Singleton } -import org.elastic4play.{ MissingAttributeError, Timed } import org.elastic4play.services.{ DBLists, Role } -import play.api.libs.json.{ JsBoolean, JsValue } +import org.elastic4play.{ MissingAttributeError, Timed } +import play.api.libs.json.{ JsValue, Json } import play.api.mvc.{ Action, AnyContent, Controller } import scala.concurrent.{ ExecutionContext, Future } @@ -65,6 +65,6 @@ class DBListCtrl @Inject() ( def itemExists(listName: String): Action[Fields] = authenticated(Role.read).async(fieldsBodyParser) { implicit request ⇒ val itemKey = request.body.getString("key").getOrElse(throw MissingAttributeError("Parameter key is missing")) val itemValue = request.body.getValue("value").getOrElse(throw MissingAttributeError("Parameter value is missing")) - dblists(listName).exists(itemKey, itemValue).map(r ⇒ Ok(JsBoolean(r))) + dblists(listName).exists(itemKey, itemValue).map(r ⇒ Ok(Json.obj("found" → r))) } } \ No newline at end of file diff --git a/app/org/elastic4play/services/DBList.scala b/app/org/elastic4play/services/DBList.scala index 1927643..ad8b5e8 100644 --- a/app/org/elastic4play/services/DBList.scala +++ b/app/org/elastic4play/services/DBList.scala @@ -119,7 +119,11 @@ class DBLists @Inject() ( def exists(key: String, value: JsValue): Future[Boolean] = { getItems()._1 .filter { item ⇒ - (item.mapTo[JsValue] \ key).asOpt[JsValue].contains(value) + item + .mapTo[JsValue] + .asOpt[JsObject] + .flatMap { obj ⇒ (obj \ key).asOpt[JsValue] } + .contains(value) } .runWith(Sink.headOption) .map(_.isDefined)