Skip to content

Commit

Permalink
#20 Change format of existence API output and fix parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jun 29, 2017
1 parent 5fccc8e commit 53a6957
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/org/elastic4play/controllers/DBListCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)))
}
}
6 changes: 5 additions & 1 deletion app/org/elastic4play/services/DBList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 53a6957

Please sign in to comment.