From 3b3251e3415a896ecfce6ad61316a166a34896de Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Thu, 29 Nov 2018 10:19:11 +0700 Subject: [PATCH] Example JSon transform and get configuration from Controller. --- app/application/API.scala | 16 +++++++++++++++- conf/routes | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/application/API.scala b/app/application/API.scala index db4114a..c9f90e5 100644 --- a/app/application/API.scala +++ b/app/application/API.scala @@ -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) @@ -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] @@ -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) @@ -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")) + } } diff --git a/conf/routes b/conf/routes index 8be6657..609a452 100644 --- a/conf/routes +++ b/conf/routes @@ -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)