Skip to content

Commit

Permalink
#20 Add API to check existence of dblist item according to simple cri…
Browse files Browse the repository at this point in the history
…terion
  • Loading branch information
To-om committed Jun 26, 2017
1 parent 33287a6 commit 1bbde4a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/org/elastic4play/controllers/DBListCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)))
}
}
22 changes: 21 additions & 1 deletion app/org/elastic4play/services/DBList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

}

0 comments on commit 1bbde4a

Please sign in to comment.