From 1823fd80d2e46f24940ca4b56548f7107c46d8f0 Mon Sep 17 00:00:00 2001 From: amgohan Date: Sat, 13 Jul 2013 12:52:20 +0100 Subject: [PATCH 1/3] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81f7341..2549127 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Play Framework 2.0 + AngularJS + Squeryl + lift-json Sample +# Play Framework 2.1.1 + AngularJS + Squeryl + lift-json Sample -Play Framework 2.0.2,AngularJS,Squeryl,lift-jsonを組み合わせたTODOアプリのサンプルです。 +Play Framework 2.1.1,AngularJS,Squeryl,lift-json From df47f768b1118de3cfcec973c0a3f66a4777add5 Mon Sep 17 00:00:00 2001 From: amgohan Date: Sat, 13 Jul 2013 12:55:04 +0100 Subject: [PATCH 2/3] Migration to play 2.1.1 Using LiftJsonPlayModule from https://github.com/tototoshi/lift-json-play-module --- app/controllers/Application.scala | 2 +- .../utils/LiftJsonPlayModule.scala | 57 +++++++++++++++++++ conf/application.conf | 2 +- project/Build.scala | 8 +-- project/build.properties | 2 +- project/plugins.sbt | 2 +- 6 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 app/controllers/utils/LiftJsonPlayModule.scala diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 73bb6a2..009c2b8 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -3,7 +3,7 @@ package controllers import play.api._ import play.api.mvc._ -import com.github.tototoshi.play2.json.LiftJson +import controllers.utils.LiftJson import net.liftweb.json._ import org.squeryl.PrimitiveTypeMode._ import models.AppDB diff --git a/app/controllers/utils/LiftJsonPlayModule.scala b/app/controllers/utils/LiftJsonPlayModule.scala new file mode 100644 index 0000000..3bff41a --- /dev/null +++ b/app/controllers/utils/LiftJsonPlayModule.scala @@ -0,0 +1,57 @@ +package controllers.utils + +import play.api._ +import play.api.http._ +import play.api.mvc._ +import play.api.libs.iteratee._ +import net.liftweb.json.{ JValue => LiftJValue, _ } +import scala.language.reflectiveCalls + +trait LiftJsonWriteable { self: LiftJsonContentTypeOf => + + implicit def writeableOf_LiftJValue(implicit codec: Codec): Writeable[LiftJValue] = { + Writeable((jval: LiftJValue) => codec.encode((pretty(render(jval))))) + } + +} + +trait LiftJsonContentTypeOf { self: LiftJsonWriteable => + + implicit def contentTypeOf_JsValue(implicit codec: Codec): ContentTypeOf[LiftJValue] = { + ContentTypeOf(Some(ContentTypes.JSON)) + } + +} + +trait LiftJsonParser { + + def tolerantJson(maxLength: Int): BodyParser[LiftJValue] = BodyParser("json, maxLength=" + maxLength) { request => + play.api.libs.iteratee.Traversable.takeUpTo[Array[Byte]](maxLength).apply(Iteratee.consume[Array[Byte]]().map { bytes => + scala.util.control.Exception.allCatch[LiftJValue].either { + JsonParser.parse(new String(bytes, request.charset.getOrElse("utf-8"))) + }.left.map { e => + (Play.maybeApplication.map(_.global.onBadRequest(request, "Invalid Json")).getOrElse(Results.BadRequest), bytes) + } + }).flatMap(Iteratee.eofOrElse(Results.EntityTooLarge)) + .flatMap { + case Left(b) => Done(Left(b), Input.Empty) + case Right(it) => it.flatMap { + case Left((r, in)) => Done(Left(r), Input.El(in)) + case Right(json) => Done(Right(json), Input.Empty) + } + } + } + + def tolerantJson: BodyParser[LiftJValue] = tolerantJson(BodyParsers.parse.DEFAULT_MAX_TEXT_LENGTH) + + def liftJson(maxLength: Int): BodyParser[LiftJValue] = BodyParsers.parse.when( + _.contentType.exists(m => m == "text/json" || m == "application/json"), + tolerantJson(maxLength), + request => Play.maybeApplication.map(_.global.onBadRequest(request, "Expecting text/json or application/json body")).getOrElse(Results.BadRequest) + ) + + def liftJson: BodyParser[LiftJValue] = liftJson(BodyParsers.parse.DEFAULT_MAX_TEXT_LENGTH) + +} + +trait LiftJson extends LiftJsonParser with LiftJsonWriteable with LiftJsonContentTypeOf \ No newline at end of file diff --git a/conf/application.conf b/conf/application.conf index 5878cdb..ba22fa8 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -15,7 +15,7 @@ application.langs="en" # ~~~~~ # Define the Global object class for this application. # Default to Global in the root package. -global=Global +application.global=Global # Database configuration # ~~~~~ diff --git a/project/Build.scala b/project/Build.scala index e6519ec..d1c2394 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -8,15 +8,13 @@ object ApplicationBuild extends Build { val appVersion = "1.0-SNAPSHOT" val appDependencies = Seq( - "org.squeryl" %% "squeryl" % "0.9.5-2" + "org.squeryl" %% "squeryl" % "0.9.5-6" ) val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( - // lift-json-play-module - resolvers += "tototoshi.github.com maven-repo/releases" at "http://tototoshi.github.com/maven-repo/releases", libraryDependencies ++= Seq( - "com.github.tototoshi" %% "lift-json-play-module" % "0.1", - "org.squeryl" %% "squeryl" % "0.9.5-2" + "org.squeryl" %% "squeryl" % "0.9.5-6", + "net.liftweb" %% "lift-json" % "2.5-M4" exclude("org.specs2","specs2_2.10") ) ) diff --git a/project/build.properties b/project/build.properties index 390c1af..21e2c5d 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.11.3 \ No newline at end of file +sbt.version=0.12.2 \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index 526b146..df6b94f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -5,7 +5,7 @@ logLevel := Level.Warn resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/" // Use the Play sbt plugin for Play projects -addSbtPlugin("play" % "sbt-plugin" % "2.0.2") +addSbtPlugin("play" % "sbt-plugin" % Option(System.getProperty("play.version")).getOrElse("2.0")) // Use the sbteclipse plugin addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0") From d198f94c9df7a20efc247826ea81e1ac545c6ac7 Mon Sep 17 00:00:00 2001 From: amgohan Date: Sat, 13 Jul 2013 17:44:34 +0100 Subject: [PATCH 3/3] add play field validation and handle error from angularjs --- app/controllers/Application.scala | 21 ++++++++++++++++++--- app/views/index.scala.html | 3 +++ public/javascripts/todo.js | 25 +++++++++++++++++-------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 009c2b8..cce9d9d 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -2,6 +2,7 @@ package controllers import play.api._ import play.api.mvc._ +import i18n.Lang import controllers.utils.LiftJson import net.liftweb.json._ @@ -14,6 +15,7 @@ import play.api.data._ import play.api.data.Form import play.api.data.Forms._ import views._ +import play.api.libs.json._ object Application extends Controller with LiftJson { @@ -22,7 +24,7 @@ object Application extends Controller with LiftJson { val taskForm = Form( mapping( "id" -> ignored(0L), - "label" -> nonEmptyText, + "label" -> text(minLength = 4), "done" ->ignored(false) )(Task.apply)(Task.unapply) ) @@ -41,11 +43,24 @@ object Application extends Controller with LiftJson { } def newTask = Action { implicit request => - val t = taskForm.bindFromRequest.value map { task => + /*val t = taskForm.bindFromRequest.value map { task => val t = inTransaction(AppDB.taskTable insert task ) t } - Ok(Extraction.decompose(t)).as("application/json") + Ok(Extraction.decompose(t)).as("application/json")*/ + + taskForm.bindFromRequest.fold( + // Form has errors, redisplay it + errors => { + Ok(Json.toJson(Map("status" -> "KO", "message" -> errors.errorsAsJson(Lang.get("us").get).toString))) + }, + + // We got a valid User value, display the summary + task => { + val t = inTransaction(AppDB.taskTable insert task ) + Ok(Extraction.decompose(t)).as("application/json") + } + ) } def doneTask(id: Long) = Action { implicit request => diff --git a/app/views/index.scala.html b/app/views/index.scala.html index 096c3d7..7fafc84 100644 --- a/app/views/index.scala.html +++ b/app/views/index.scala.html @@ -15,6 +15,9 @@

TODO List

+
+ {{todoTextErrorMessage}} +
} \ No newline at end of file diff --git a/public/javascripts/todo.js b/public/javascripts/todo.js index 7122ad1..a27db28 100644 --- a/public/javascripts/todo.js +++ b/public/javascripts/todo.js @@ -1,4 +1,5 @@ function TodoCtrl($scope) { +$scope.todoTextErrorHide = true; jsRouter.controllers.Application.tasks().ajax({ async: false, success: function(data) { @@ -9,16 +10,24 @@ function TodoCtrl($scope) { } }); $scope.addTodo = function() { - jsRouter.controllers.Application.newTask().ajax({ + newTask = jsRouter.controllers.Application.newTask().ajax({ data: { label: $scope.todoText }, + dataType : "json", success: function(data) { - $scope.$apply(function(){ - $scope.todos.push(data); - $scope.todoText = ''; - }); - }, - error: function() { - alert("error:addTask"); + if(data.status == "KO") { + $scope.$apply(function(){ + $scope.todoTextErrorHide = false; + $scope.todoTextErrorMessage = jQuery.parseJSON(data.message).label[0]; + }); + } + else { + + $scope.$apply(function(){ + $scope.todoTextErrorHide = true; + $scope.todos.push(data); + $scope.todoText = ''; + }); + } } }); };