Skip to content

Commit

Permalink
Example JSon transform and get configuration from Controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyentoanit committed Nov 29, 2018
1 parent 21b9b60 commit 3b3251e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 15 additions & 1 deletion app/application/API.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import play.api.mvc._
import play.api.libs.json._
import play.api.libs.functional.syntax._
import javax.inject._
import play.api.Configuration

case class Location(lat: Double, long: Double)

Expand All @@ -25,7 +26,7 @@ object Place {
list = list ::: List(place)
}
}
class ApiApplication @Inject() (cc: ControllerComponents) extends AbstractController(cc) {
class ApiApplication @Inject() (cc: ControllerComponents, config: Configuration) extends AbstractController(cc) {
implicit val locationWrites: Writes[Location] = (
(__ \ "lat").write[Double] and
(__ \ "long").write[Double]
Expand All @@ -46,6 +47,9 @@ class ApiApplication @Inject() (cc: ControllerComponents) extends AbstractContro
(__ \ "location").read[Location]
)(Place.apply _)

// implicit val placeWrites = Json.writes[Place]
// implicit val placeReads = Json.reads[Place]

def listPlaces = Action {
val json = Json.toJson(Place.list)
Ok(json)
Expand All @@ -63,4 +67,14 @@ class ApiApplication @Inject() (cc: ControllerComponents) extends AbstractContro
}
)
}

def playTransform = Action { request =>
val json = Json.obj("key1" -> "value1", "key2" -> Json.obj("key21" -> "value21", "key22" -> "value22"))
val jsonTransformer = (__ \ "key2").json.pick
Ok(json.transform(jsonTransformer).toString)
}

def getConfiguration = Action { request =>
Ok(config.get[String]("db.default.username"))
}
}
2 changes: 2 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ POST /login application.AccountApplication.login

GET /places application.ApiApplication.listPlaces
POST /places application.ApiApplication.savePlace
GET /transform application.ApiApplication.playTransform
GET /getconfig application.ApiApplication.getConfiguration

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)

0 comments on commit 3b3251e

Please sign in to comment.