Skip to content

Commit

Permalink
Add TestController as front-end application that uses RestController
Browse files Browse the repository at this point in the history
  • Loading branch information
HSalaila committed Jun 6, 2016
1 parent 0dfd407 commit 182030e
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import play.api.mvc.{Action, Controller}
class Application extends Controller {

def index = Action {
Ok(views.html.default("hello"))
Redirect(routes.TestController.index())
}
}
16 changes: 11 additions & 5 deletions app/controllers/RestController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ class RestController @Inject() (pinboardRepo: PinboardRepo,
*/
def createPinboard = Action {
request => {
val map = request.body.asFormUrlEncoded.get
val seqName = map.get("name")
val pinboard = seqName match {
case Some(seqName) => pinboardRepo.addPinboard(seqName.head)
case None => pinboardRepo.addPinboard("no name")
val map = request.body.asFormUrlEncoded
val name = map match {
case Some(map) => map.get("name") match {
case Some(name) => name.head
case None => ""
}
case None => ""
}
val pinboard = name match {
case name if name != "" => pinboardRepo.addPinboard(name)
case _ => pinboardRepo.addPinboard("no name")
}
Ok(Json.toJson(Await.result(pinboard, Duration.Inf))).as("application/json")
}
Expand Down
60 changes: 60 additions & 0 deletions app/controllers/TestController.scala
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))
}
}
}
7 changes: 7 additions & 0 deletions app/models/Pinboard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import javax.inject.Inject

import _root_.util.BaseRepo
import play.api.db.slick.DatabaseConfigProvider
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._
import slick.driver.H2Driver.api._
import slick.lifted.{TableQuery, Tag}
Expand All @@ -24,6 +26,11 @@ object Pinboard {
"name" -> pinboard.name
)
}

implicit val pinboardReads = {
(__ \ "id").read[Long] and
(__ \ "name").read[String]
}.apply(Pinboard.apply _)
}
class PinboardTable(tag: Tag) extends Table[Pinboard](tag, "pinboard") {
def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
Expand Down
2 changes: 0 additions & 2 deletions app/views/default.scala.html

This file was deleted.

23 changes: 23 additions & 0 deletions app/views/test.scala.html
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>
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ scalaVersion := "2.11.8"
lazy val root = (project in file(".")).enablePlugins(PlayScala)

libraryDependencies ++= Seq(
ws,
"com.h2database" % "h2" % "1.4.192",
"com.github.tototoshi" %% "slick-joda-mapper" % "2.2.0",
"com.typesafe.play" %% "play-slick" % "2.0.0",
Expand Down
6 changes: 5 additions & 1 deletion conf/routes
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
GET / controllers.Application.index
GET /generateDDL controllers.DatabaseController.generateDDL

GET /test controllers.TestController.index
POST /test/create controllers.TestController.createPinboard
POST /test/delete controllers.TestController.deletePinboard
GET /pinboards controllers.RestController.index

# rest stuff
POST /pinboards controllers.RestController.createPinboard
GET /pinboards/:id controllers.RestController.listArticles(id: Long)
DELETE /pinboards/:id controllers.RestController.deletePinboard(id: Long)
Expand Down

0 comments on commit 182030e

Please sign in to comment.