OSDN Git Service

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