yongchao li avatar yongchao li committed eb58bab

modify

Comments (0)

Files changed (3)

app/controllers/Application.scala

 
 import views._
 
+case class User(id:Pk[Long] = NotAssigned,uname:String,uage:Int)
 object Application extends Controller {
-	val saveUserForm = Form {
-		tuple(
-			"uname" -> nonEmptyText,
-			"uage" -> number(min=0,max=100)
-		)
-	}
-  
-  def index = Action {
+  //save user form defintion
+  val saveUserForm = Form {
+    mapping(
+      "id" -> ignored(NotAssigned:Pk[Long]),
+      "uname" -> nonEmptyText,
+      "uage" -> number(min=0,max=100)
+    )(User.apply)(User.unapply)
+  }
+
+  def index = Action {implicit request =>
     DB.withConnection{implicit conn =>
-      val users:List[(Long,String,Int)] = SQL("select *from User").as(long("id") ~ str("uname") ~ int("uage") map(flatten) *)
+      val users= SQL("select *from User").as(get[Pk[Long]]("id") ~ get[String]("uname") ~ get[Int]("uage") map {case id ~ uname ~ uage => User(id,uname,uage)} *)
       Ok(html.index(users))
     }
-  	
   }
 
   def add = Action {
-  	Ok(html.add(saveUserForm))
+    Ok(html.add(saveUserForm))
   }
 
   def save = Action { implicit request =>
-  	saveUserForm.bindFromRequest.fold(
-  		formWithErrors => BadRequest(html.add(formWithErrors)),
-      {case value => {
-          DB.withConnection{implicit conn =>
-            SQL("insert into User(uname,uage) values({uname},{uage})").on("uname" -> value._1,"uage" -> value._2).executeUpdate()
-          }
-          Redirect("/")
-      }}
-  	)
+    saveUserForm.bindFromRequest.fold(
+      formWithErrors => BadRequest(html.add(formWithErrors)),
+      user => {
+        DB.withConnection{implicit conn =>
+          SQL("insert into User(uname,uage) values({uname},{uage})").on('uname -> user.uname,'uage -> user.uage).executeUpdate()
+        }
+        Redirect("/")
+      }
+    )
   }
 
   def edit(id:Long) = TODO
       Redirect("/").flashing("success" -> "delete success")
     }
   }
+
 }

app/views/add.scala.html

-@(saveUserForm: Form[(String,Int)])
+@(saveUserForm: Form[User])
 @import helper._
 
 @layout("play-demo-add") {

app/views/index.scala.html

-@(users: List[(Long,String,Int)])
+@(users: List[User])(implicit flash: play.api.mvc.Flash)
 @layout("play-demo-index") {
 	<a href="/add">添加</a>
 	<hr/>
+	@flash.get("success").map { message =>
+        <div>
+            <strong>@message</strong> 
+        </div>
+    }
 	<table>
 		<tr><th>ID</th><th>姓名</th><th>年龄</th><th>操作</th></tr>
 		@users.map{ u =>
-		<tr><td>@u._1</td><td>@u._2</td><td>@u._3</td><td><a href="/edit/@u._1">编辑</a> | <a href="/delete/@u._1" onclick="return confirm('你确定?')">删除</a></tr>
+		<tr><td>@u.id</td><td>@u.uname</td><td>@u.uage</td><td><a href="/edit/@u.id">编辑</a> | <a href="/delete/@u.id" onclick="return confirm('你确定?')">删除</a></tr>
 		}
 	</table>
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.