This repository has been archived by the owner on Jan 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Showing
11 changed files
with
153 additions
and
29 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
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,26 @@ | ||
package controllers | ||
|
||
import models._ | ||
import play.api.mvc._ | ||
import play.api.libs.json._ | ||
|
||
object Actions extends Results with BodyParsers { | ||
|
||
/** | ||
* Simplifies handling incoming json by wrapping validation and returning BadRequest if it fails | ||
* @param action the underlying action | ||
* @tparam A a class that has an implicit Read available | ||
* @return a response | ||
*/ | ||
def JsonAction[A](action: A => Result)(implicit reader: Reads[A]): EssentialAction = { | ||
Action(parse.json) { implicit request => | ||
request.body.validate[A].fold( | ||
valid = { json => | ||
action(json) | ||
}, | ||
invalid = (e => BadRequest(JsError.toFlatJson(e)).as("application/json")) | ||
) | ||
} | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package controllers.api | ||
|
||
import play.api.mvc._ | ||
import play.api.libs.json._ | ||
import models._ | ||
import controllers.Actions._ | ||
|
||
import com.mongodb.casbah.WriteConcern | ||
import se.radley.plugin.salat._ | ||
import se.radley.plugin.salat.Binders._ | ||
import mongoContext._ | ||
|
||
object Users extends Controller { | ||
|
||
def index() = Action { | ||
val users = User.findAll().toList | ||
Ok(Json.toJson(users)) | ||
} | ||
|
||
def create = JsonAction[User] { user => | ||
User.save(user, WriteConcern.Safe) | ||
Ok(Json.toJson(user)) | ||
} | ||
|
||
def view(id: ObjectId) = Action { | ||
User.findOneById(id).map { user => | ||
Ok(Json.toJson(user)) | ||
} getOrElse { | ||
NotFound | ||
} | ||
} | ||
|
||
def update(id: ObjectId) = JsonAction[User] { requestUser => | ||
val user = requestUser.copy(id) | ||
User.save(user, WriteConcern.Safe) | ||
Ok(Json.toJson(user)) | ||
} | ||
|
||
def delete(id: ObjectId) = Action { | ||
User.removeById(id) | ||
Ok("") | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,31 @@ | ||
package models | ||
|
||
import com.mongodb.casbah.Imports._ | ||
import java.util.Date | ||
import play.api.libs.json._ | ||
import play.api.libs.functional.syntax._ | ||
|
||
case class Address( | ||
street: String, | ||
zip: String, | ||
country: String | ||
) | ||
) | ||
|
||
object Address { | ||
// Conversions | ||
implicit val addressJsonWrite = new Writes[Address] { | ||
def writes(a: Address): JsValue = { | ||
Json.obj( | ||
"street" -> a.street, | ||
"zip" -> a.zip, | ||
"country" -> a.country | ||
) | ||
} | ||
} | ||
|
||
implicit val addressJsonRead = ( | ||
(__ \ 'street).read[String] ~ | ||
(__ \ 'zip).read[String] ~ | ||
(__ \ 'country).read[String] | ||
)(Address.apply _) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
# User | ||
GET /user controllers.api.Users.index() | ||
POST /user controllers.api.Users.create() | ||
GET /user/:id controllers.api.Users.view(id: ObjectId) | ||
PUT /user/:id controllers.api.Users.update(id: ObjectId) | ||
DELETE /user/:id controllers.api.Users.delete(id: ObjectId) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import sbt._ | ||
import Keys._ | ||
import PlayProject._ | ||
import play.Project._ | ||
|
||
object ApplicationBuild extends Build { | ||
|
||
val appName = "sample" | ||
val appVersion = "1.0" | ||
|
||
val appDependencies = Seq( | ||
"se.radley" %% "play-plugins-salat" % "1.1" | ||
"se.radley" %% "play-plugins-salat" % "1.2" | ||
) | ||
|
||
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( | ||
val main = play.Project(appName, appVersion, appDependencies).settings( | ||
routesImport += "se.radley.plugin.salat.Binders._", | ||
templatesImport += "org.bson.types.ObjectId" | ||
templatesImport += "org.bson.types.ObjectId", | ||
resolvers += Resolver.sonatypeRepo("snapshots") | ||
) | ||
|
||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=0.11.3 | ||
sbt.version=0.12.2 |
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