OSDN Git Service

1cc14b1592e1ac5a207f7825ffe3212aeb67567b
[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 models.services._
9 import org.squeryl._
10 import org.squeryl.PrimitiveTypeMode._
11 object UserController extends Controller{
12   val Home = Redirect(routes.UserController.list(0, ""))
13   val userForm = Form(
14       mapping(
15           "name" -> nonEmptyText,
16           "email" -> nonEmptyText
17        )(User.apply)(User.unapply)
18   )
19   
20   def createUser = Action { implicit request =>
21     Ok(views.html.createUserForm(userForm))
22   }
23   
24   def userRegistration() = Action { implicit request =>
25     userForm.bindFromRequest.fold(
26         formWithErrors => BadRequest(views.html.createUserForm(formWithErrors)),
27         user => {
28           inTransaction {
29             UserManager().insert(user)
30             Home.flashing("success" -> "User %s has been created".format(user.name))
31           }
32         }
33      )
34   }
35   
36   def updateUser(id: Long) = Action{ implicit request =>
37     inTransaction {
38       val user = UserManager().getById(id)
39       val updUser = User(user.name, user.email)
40       Ok(views.html.updateUserForm(userForm.fill(updUser), id))
41     }
42   }
43   
44   def userModification(id: Long) = Action{ implicit request =>
45     userForm.bindFromRequest.fold(
46         formWithErrors => BadRequest(views.html.updateUserForm(formWithErrors, id)),
47         user => {
48           inTransaction {
49             UserManager().update(user, id)
50             Home.flashing("success" -> "User %s has been updated".format(user.name))
51           }
52         }
53      )
54   }
55   
56   def list(page: Int, key: String) = Action{ implicit reauest =>
57     inTransaction {
58       val row = Integer.decode(Messages("list.row"))
59       val buff = UserManager().list(key)
60       Ok(views.html.userlist(buff.page(page*row, row), buff.size, key, page))
61     }
62   }
63   
64 }