-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathController.groovy
99 lines (81 loc) · 2.92 KB
/
Controller.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<%=packageName ? "package ${packageName}" : ''%>
import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*
class ${className}Controller {
${className}Service ${propertyName}Service
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond ${propertyName}Service.list(params), model:[${propertyName}Count: ${propertyName}Service.count()]
}
def show(Long id) {
respond ${propertyName}Service.get(id)
}
def create() {
respond new ${className}(params)
}
def save(${className} ${propertyName}) {
if (${propertyName} == null) {
notFound()
return
}
try {
${propertyName}Service.save(${propertyName})
} catch (ValidationException e) {
respond ${propertyName}.errors, view:'create'
return
}
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: '${propertyName}.label', default: '${className}'), ${propertyName}.id])
redirect ${propertyName}
}
'*' { respond ${propertyName}, [status: CREATED] }
}
}
def edit(Long id) {
respond ${propertyName}Service.get(id)
}
def update(${className} ${propertyName}) {
if (${propertyName} == null) {
notFound()
return
}
try {
${propertyName}Service.save(${propertyName})
} catch (ValidationException e) {
respond ${propertyName}.errors, view:'edit'
return
}
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message', args: [message(code: '${propertyName}.label', default: '${className}'), ${propertyName}.id])
redirect ${propertyName}
}
'*'{ respond ${propertyName}, [status: OK] }
}
}
def delete(Long id) {
if (id == null) {
notFound()
return
}
${propertyName}Service.delete(id)
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.deleted.message', args: [message(code: '${propertyName}.label', default: '${className}'), id])
redirect action:"index", method:"GET"
}
'*'{ render status: NO_CONTENT }
}
}
protected void notFound() {
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.not.found.message', args: [message(code: '${propertyName}.label', default: '${className}'), params.id])
redirect action: "index", method: "GET"
}
'*'{ render status: NOT_FOUND }
}
}
}