Skip to content

Commit

Permalink
#19 add API to update a dblist item
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jun 22, 2017
1 parent 6b18788 commit 33287a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
21 changes: 16 additions & 5 deletions app/org/elastic4play/controllers/DBListCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package org.elastic4play.controllers

import javax.inject.{ Inject, Singleton }

import scala.concurrent.ExecutionContext
import scala.reflect.runtime.universe
import org.elastic4play.{ MissingAttributeError, Timed }
import org.elastic4play.services.{ DBLists, Role }
import play.api.libs.json.JsValue
import play.api.mvc.{ Action, AnyContent, Controller }
import org.elastic4play.Timed
import org.elastic4play.services.{ DBLists, Role }

import scala.concurrent.Future
import scala.concurrent.{ ExecutionContext, Future }

@Singleton
class DBListCtrl @Inject() (
Expand Down Expand Up @@ -49,4 +47,17 @@ class DBListCtrl @Inject() (
NoContent
}
}

@Timed("controllers.DBListCtrl.udpateItem")
def updateItem(itemId: String) = authenticated(Role.admin).async(fieldsBodyParser) { implicit request
request.body.getValue("value")
.map { value
for {
item dblists.getItem(itemId)
_ dblists.deleteItem(item)
newItem dblists(item.dblist).addItem(value)
} yield renderer.toOutput(OK, newItem.id)
}
.getOrElse(Future.failed(MissingAttributeError("value")))
}
}
22 changes: 12 additions & 10 deletions app/org/elastic4play/services/DBList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ package org.elastic4play.services

import javax.inject.{ Inject, Provider, Singleton }

import scala.annotation.implicitNotFound
import scala.concurrent.{ ExecutionContext, Future }
import scala.concurrent.duration.DurationInt
import akka.NotUsed
import akka.stream.Materializer
import akka.stream.scaladsl.{ Sink, Source }
import org.elastic4play.database.DBCreate
import org.elastic4play.models.{ Attribute, EntityDef, ModelDef, AttributeFormat F }
import org.elastic4play.utils.{ Hasher, RichFuture }
import play.api.Configuration
import play.api.cache.CacheApi
import play.api.libs.json.{ JsObject, JsString, JsValue }
import play.api.libs.json.{ Reads, Writes }
import play.api.libs.json.JsValue.jsValueToJsLookup
import play.api.libs.json.Json
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import org.elastic4play.database.DBCreate
import org.elastic4play.models.{ Attribute, EntityDef, ModelDef, AttributeFormat F }
import org.elastic4play.utils.{ Hasher, RichFuture }
import play.api.libs.json._

import scala.collection.immutable
import scala.concurrent.duration.DurationInt
import scala.concurrent.{ ExecutionContext, Future }

@Singleton
class DBListModel(dblistName: String) extends ModelDef[DBListModel, DBListItemEntity](dblistName) { model
Expand Down Expand Up @@ -69,13 +66,18 @@ class DBLists @Inject() (
}

def deleteItem(itemId: String)(implicit authContext: AuthContext): Future[Unit] = {
getItem(itemId).flatMap(deleteItem)
}

def deleteItem(item: DBListItemEntity)(implicit authContext: AuthContext): Future[Unit] = {
for {
item getSrv[DBListModel, DBListItemEntity](dblistModel, itemId)
_ deleteSrv.get.realDelete[DBListModel, DBListItemEntity](dblistModel, item)
_ = cache.remove(dblistModel.name + "_" + item.dblist)
} yield ()
}

def getItem(itemId: String): Future[DBListItemEntity] = getSrv[DBListModel, DBListItemEntity](dblistModel, itemId)

def apply(name: String): DBList = new DBList {
def cachedItems: immutable.Seq[DBListItem] = cache.getOrElse(dblistModel.name + "_" + name, 10.seconds) {
val (src, total) = getItems()
Expand Down

0 comments on commit 33287a6

Please sign in to comment.