-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
533f19e
commit 21b9b60
Showing
9 changed files
with
85 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package application | ||
import play.api.mvc._ | ||
import play.api.libs.json._ | ||
import play.api.libs.functional.syntax._ | ||
import javax.inject._ | ||
|
||
case class Location(lat: Double, long: Double) | ||
|
||
case class Place(name: String, location: Location) | ||
|
||
object Place { | ||
var list: List[Place] = { | ||
List( | ||
Place( | ||
"Sandleford", | ||
Location(51.377797, -1.318965) | ||
), | ||
Place( | ||
"Watership Down", | ||
Location(51.235685, -1.309197) | ||
) | ||
) | ||
} | ||
def save(place: Place) = { | ||
list = list ::: List(place) | ||
} | ||
} | ||
class ApiApplication @Inject() (cc: ControllerComponents) extends AbstractController(cc) { | ||
implicit val locationWrites: Writes[Location] = ( | ||
(__ \ "lat").write[Double] and | ||
(__ \ "long").write[Double] | ||
)(unlift(Location.unapply)) | ||
|
||
implicit val placeWrites: Writes[Place] = ( | ||
(__ \ "name").write[String] and | ||
(__ \ "location").write[Location] | ||
)(unlift(Place.unapply)) | ||
|
||
implicit val locationReads: Reads[Location] = ( | ||
(__ \ "lat").read[Double] and | ||
(__ \ "long").read[Double] | ||
)(Location.apply _) | ||
|
||
implicit val placeReads: Reads[Place] = ( | ||
(__ \ "name").read[String] and | ||
(__ \ "location").read[Location] | ||
)(Place.apply _) | ||
|
||
def listPlaces = Action { | ||
val json = Json.toJson(Place.list) | ||
Ok(json) | ||
} | ||
|
||
def savePlace = Action(parse.json) { request => | ||
val placeResult = request.body.validate[Place] | ||
placeResult.fold( | ||
errors => { | ||
BadRequest(Json.obj("status" -> "KO", "message" -> JsError.toJson(errors))) | ||
}, | ||
place => { | ||
Place.save(place) | ||
Ok(Json.obj("status" -> "OK", "message" -> ("Place '" + place.name + "' saved."))) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters