1 | class PersonController extends BaseController { |
---|
2 | |
---|
3 | def beforeInterceptor = [action:this.&auth,except:['login', 'logout']] |
---|
4 | |
---|
5 | def index = { redirect(action:list,params:params) } |
---|
6 | |
---|
7 | // the delete, save and update actions only accept POST requests |
---|
8 | def allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
9 | |
---|
10 | def list = { |
---|
11 | if(!params.max) params.max = 10 |
---|
12 | [ personInstanceList: Person.list( params ) ] |
---|
13 | } |
---|
14 | |
---|
15 | def show = { |
---|
16 | def personInstance = Person.get( params.id ) |
---|
17 | |
---|
18 | if(!personInstance) { |
---|
19 | flash.message = "Person not found with id ${params.id}" |
---|
20 | redirect(action:list) |
---|
21 | } |
---|
22 | else { return [ personInstance : personInstance ] } |
---|
23 | } |
---|
24 | |
---|
25 | def delete = { |
---|
26 | def personInstance = Person.get( params.id ) |
---|
27 | if(personInstance) { |
---|
28 | personInstance.delete() |
---|
29 | flash.message = "Person ${params.id} deleted" |
---|
30 | redirect(action:list) |
---|
31 | } |
---|
32 | else { |
---|
33 | flash.message = "Person not found with id ${params.id}" |
---|
34 | redirect(action:list) |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | def edit = { |
---|
39 | def personInstance = Person.get( params.id ) |
---|
40 | |
---|
41 | if(!personInstance) { |
---|
42 | flash.message = "Person not found with id ${params.id}" |
---|
43 | redirect(action:list) |
---|
44 | } |
---|
45 | else { |
---|
46 | return [ personInstance : personInstance ] |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | def update = { |
---|
51 | def personInstance = Person.get( params.id ) |
---|
52 | if(personInstance) { |
---|
53 | personInstance.properties = params |
---|
54 | if(!personInstance.hasErrors() && personInstance.save()) { |
---|
55 | flash.message = "Person ${params.id} updated" |
---|
56 | redirect(action:show,id:personInstance.id) |
---|
57 | } |
---|
58 | else { |
---|
59 | render(view:'edit',model:[personInstance:personInstance]) |
---|
60 | } |
---|
61 | } |
---|
62 | else { |
---|
63 | flash.message = "Person not found with id ${params.id}" |
---|
64 | redirect(action:edit,id:params.id) |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | def create = { |
---|
69 | def personInstance = new Person() |
---|
70 | personInstance.properties = params |
---|
71 | return ['personInstance':personInstance] |
---|
72 | } |
---|
73 | |
---|
74 | def save = { |
---|
75 | def personInstance = new Person(params) |
---|
76 | if(!personInstance.hasErrors() && personInstance.save()) { |
---|
77 | flash.message = "Person ${personInstance.id} created" |
---|
78 | redirect(action:show,id:personInstance.id) |
---|
79 | } |
---|
80 | else { |
---|
81 | render(view:'create',model:[personInstance:personInstance]) |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | def login = { |
---|
86 | if (request.method == "GET") { |
---|
87 | session.userId = null |
---|
88 | def person = new Person() |
---|
89 | } |
---|
90 | else { |
---|
91 | def person = Person.findByUserIdAndPassword(params.userId,params.password) |
---|
92 | if (person) { |
---|
93 | session.userId = person.userId |
---|
94 | def greeting = "Hi "+person.firstName+" "+person.lastName |
---|
95 | def redirectParams = |
---|
96 | session.originalRequestParams ? |
---|
97 | session.originalRequestParams : [controller:'task'] |
---|
98 | flash['message'] = "${greeting}" |
---|
99 | redirect(redirectParams) |
---|
100 | } |
---|
101 | else { |
---|
102 | flash['message'] = 'Please enter a valid user ID and password' |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | def logout = { |
---|
108 | session.userId = null |
---|
109 | flash['message'] = 'Successfully logged out' |
---|
110 | redirect(controller:'person', action:'login') |
---|
111 | } |
---|
112 | |
---|
113 | def admin = { |
---|
114 | render(view:'admin') |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|