From 1bbde4ac978d32309a62187a5a35c3a5516e1981 Mon Sep 17 00:00:00 2001 From: To-om Date: Mon, 26 Jun 2017 15:40:25 +0200 Subject: [PATCH] #20 Add API to check existence of dblist item according to simple criterion --- .../elastic4play/controllers/DBListCtrl.scala | 7 +++++- app/org/elastic4play/services/DBList.scala | 22 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/org/elastic4play/controllers/DBListCtrl.scala b/app/org/elastic4play/controllers/DBListCtrl.scala index 771bb64..2a3e27b 100644 --- a/app/org/elastic4play/controllers/DBListCtrl.scala +++ b/app/org/elastic4play/controllers/DBListCtrl.scala @@ -4,7 +4,7 @@ import javax.inject.{ Inject, Singleton } import org.elastic4play.{ MissingAttributeError, Timed } import org.elastic4play.services.{ DBLists, Role } -import play.api.libs.json.JsValue +import play.api.libs.json.{ JsBoolean, JsValue } import play.api.mvc.{ Action, AnyContent, Controller } import scala.concurrent.{ ExecutionContext, Future } @@ -60,4 +60,9 @@ class DBListCtrl @Inject() ( } .getOrElse(Future.failed(MissingAttributeError("value"))) } + + @Timed("controllers.DBListCtrl.itemExists") + def itemExists(listName: String, itemKey: String, itemValue: JsValue): Action[AnyContent] = authenticated(Role.read).async { implicit request ⇒ + dblists(listName).exists(itemKey, itemValue).map(r ⇒ Ok(JsBoolean(r))) + } } \ No newline at end of file diff --git a/app/org/elastic4play/services/DBList.scala b/app/org/elastic4play/services/DBList.scala index 0386f90..1927643 100644 --- a/app/org/elastic4play/services/DBList.scala +++ b/app/org/elastic4play/services/DBList.scala @@ -19,32 +19,43 @@ import scala.concurrent.duration.DurationInt import scala.concurrent.{ ExecutionContext, Future } @Singleton -class DBListModel(dblistName: String) extends ModelDef[DBListModel, DBListItemEntity](dblistName) { model ⇒ +class DBListModel(dblistName: String) extends ModelDef[DBListModel, DBListItemEntity](dblistName) { + model ⇒ @Inject def this(configuration: Configuration) = this(configuration.getString("dblist.name").get) val value: Attribute[String] = attribute("value", F.stringFmt, "Content of the dblist item") val dblist: Attribute[String] = attribute("dblist", F.stringFmt, "Name of the dblist") + override def apply(attributes: JsObject) = new DBListItemEntity(this, attributes) } class DBListItemEntity(model: DBListModel, attributes: JsObject) extends EntityDef[DBListModel, DBListItemEntity](model, attributes) with DBListItem { def mapTo[T](implicit reads: Reads[T]): T = Json.parse((attributes \ "value").as[String]).as[T] + def dblist: String = (attributes \ "dblist").as[String] + override def toJson: JsObject = super.toJson - "value" + ("value" → mapTo[JsValue]) } trait DBListItem { def id: String + def dblist: String + def mapTo[A](implicit reads: Reads[A]): A } trait DBList { def cachedItems: Seq[DBListItem] + def getItems(): (Source[DBListItem, NotUsed], Future[Long]) + def getItems[A](implicit reads: Reads[A]): (Source[(String, A), NotUsed], Future[Long]) + def addItem[A](item: A)(implicit writes: Writes[A]): Future[DBListItem] + + def exists(key: String, value: JsValue): Future[Boolean] } @Singleton @@ -104,6 +115,15 @@ class DBLists @Inject() ( dblistModel(newItem) } } + + def exists(key: String, value: JsValue): Future[Boolean] = { + getItems()._1 + .filter { item ⇒ + (item.mapTo[JsValue] \ key).asOpt[JsValue].contains(value) + } + .runWith(Sink.headOption) + .map(_.isDefined) + } } } \ No newline at end of file