-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestController as front-end application that uses RestController
- Loading branch information
Showing
8 changed files
with
108 additions
and
9 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
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,60 @@ | ||
package controllers | ||
|
||
import javax.inject.Inject | ||
|
||
import models.Pinboard | ||
import play.api.data.Form | ||
import play.api.data.Forms._ | ||
import play.api.data.format.Formats.longFormat | ||
import play.api.libs.ws.WSClient | ||
import play.api.mvc.{Action, Controller} | ||
|
||
import scala.concurrent.Await | ||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
/** | ||
* Created by Harold on 2016-06-06. | ||
* Simple front-end that allows to create and delete pinboards (will only be used internally) | ||
*/ | ||
case class PinboardForm(name: Option[String]) | ||
case class DeletePinboardForm(id: Long) | ||
object PinboardForm { | ||
val pinboardForm = Form( | ||
mapping( | ||
"name" -> optional(text) | ||
)(PinboardForm.apply)(PinboardForm.unapply) | ||
) | ||
val deletePinboardForm = Form( | ||
mapping( | ||
"id" -> of[Long] | ||
)(DeletePinboardForm.apply)(DeletePinboardForm.unapply) | ||
) | ||
} | ||
class TestController @Inject() (ws: WSClient) extends Controller { | ||
def index = Action { | ||
Ok{views.html.test(PinboardForm.pinboardForm, PinboardForm.deletePinboardForm)} | ||
} | ||
|
||
def createPinboard = Action { | ||
implicit request => { | ||
val wsrequest = ws.url("http://" + request.host + "/pinboards") | ||
val nameOpt = PinboardForm.pinboardForm.bindFromRequest().get.name | ||
|
||
val requestNameOpt = nameOpt match { | ||
case Some(name) => wsrequest.withBody(Map("name" -> Seq(name))) | ||
case None => wsrequest | ||
} | ||
|
||
val resultAsPinboard = requestNameOpt.execute("POST").map(response => response.json.as[Pinboard]) | ||
val result = resultAsPinboard.map(pinboard => "created new pinboard with id: " + pinboard.id + " name: " + pinboard.name) | ||
Ok(Await.result(result, Duration.Inf)) | ||
} | ||
} | ||
def deletePinboard = Action { | ||
implicit request => { | ||
val wsrequest = ws.url("http://" + request.host + "/pinboards/" + PinboardForm.deletePinboardForm.bindFromRequest().get.id) | ||
val result = wsrequest.execute("DELETE").map(response => response.body) | ||
Ok(Await.result(result, Duration.Inf)) | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,23 @@ | ||
@import controllers.PinboardForm | ||
@(pinboardForm: Form[PinboardForm], deletePinboardForm: Form[DeletePinboardForm]) | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>test controller</title> | ||
</head> | ||
<body> | ||
<h1>rest controller</h1> | ||
<p>uses the RestController to fetch the results </p> | ||
<h2>create pinboard</h2> | ||
<form action="@routes.TestController.createPinboard()" method="post"> | ||
<input type="text" name="@pinboardForm("name").name" placeholder="Enter name (Optional)" value=""/> | ||
<input type="submit" value="Create"> | ||
</form> | ||
<h2>delete pinboard</h2> | ||
<form action="@routes.TestController.deletePinboard()" method="post"> | ||
<input type="number" name="@deletePinboardForm("id").name" placeholder="Enter id" value=""/> | ||
<input type="submit" value="Delete"> | ||
</form> | ||
</body> | ||
</html> |
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