OSDN Git Service

initial commit
[open-pdm-light/PartList.git] / PartsList / PartsList / app / controllers / UserController.scala
1 package controllers
2 import play.api._
3 import play.api.i18n._
4 import play.api.mvc._
5 import play.api.data._
6 import play.api.data.Forms._
7 import models._
8 import org.squeryl._
9 import org.squeryl.PrimitiveTypeMode._
10 object UserController extends Controller{
11   val Home = Redirect(routes.UserController.list(0, ""))
12   val userForm = Form(
13       mapping(
14           "name" -> nonEmptyText,
15           "email" -> nonEmptyText
16        )(User.apply)(User.unapply)
17   )
18   
19   def createUser = Action { implicit request =>
20     Ok(views.html.createUserForm(userForm))
21   }
22   
23   def userRegistration() = Action { implicit request =>
24     userForm.bindFromRequest.fold(
25         formWithErrors => BadRequest(views.html.createUserForm(formWithErrors)),
26         user => {
27           inTransaction {
28             PartsListDb.users.insert(User(user.name, user.email))
29             Home.flashing("success" -> "User %s has been created".format(user.name))
30           }
31         }
32      )
33   }
34   
35   def updateUser(id: Long) = Action{ implicit request =>
36     inTransaction {
37       val user = getUser(id)
38       val updUser = User(user.name, user.email)
39       Ok(views.html.updateUserForm(userForm.fill(updUser), id))
40     }
41   }
42   
43   def userModification(id: Long) = Action{ implicit request =>
44     userForm.bindFromRequest.fold(
45         formWithErrors => BadRequest(views.html.updateUserForm(formWithErrors, id)),
46         user => {
47           inTransaction {
48             val updUser = getUser(id)
49             updUser.name = user.name
50             updUser.email = user.email
51             PartsListDb.users.update(updUser)
52             Home.flashing("success" -> "User %s has been updated".format(user.name))
53           }
54         }
55      )
56   }
57   
58   def list(page: Int, key: String) = Action{ implicit reauest =>
59     inTransaction {
60       val row = Integer.decode(Messages("list.row"))
61       val buff = PartsListDb.users.where(u => u.name like key + "%")
62       Ok(views.html.userlist(buff.page(page*row, row), buff.size, key, page))
63     }
64   }
65   
66   def getUser(id: Long):User ={
67     PartsListDb.users.where(u => u.id === id ).head
68   }
69 }