/
AlbumController.groovy
108 lines (91 loc) · 3.56 KB
/
AlbumController.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
100
101
102
103
104
105
106
107
108
package grails.plugin.angular.test
import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON
import static javax.servlet.http.HttpServletResponse.*
class AlbumController {
static final int SC_UNPROCESSABLE_ENTITY = 422
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index() { }
def list() {
cache false
params.max = Math.min(params.max ? params.int('max') : 10, 100)
response.setIntHeader('X-Pagination-Total', Album.count())
render Album.list(params) as JSON
}
def save() {
def albumInstance = new Album(request.JSON)
def responseJson = [:]
if (albumInstance.save(flush: true)) {
response.status = SC_CREATED
responseJson.id = albumInstance.id
responseJson.message = message(code: 'default.created.message', args: [message(code: 'album.label', default: 'Album'), albumInstance.id])
} else {
response.status = SC_UNPROCESSABLE_ENTITY
responseJson.errors = albumInstance.errors.fieldErrors.collectEntries {
[(it.field): message(error: it)]
}
}
cache false
render responseJson as JSON
}
def get() {
def albumInstance = Album.get(params.id)
if (albumInstance) {
cache false
render albumInstance as JSON
} else {
notFound params.id
}
}
def update() {
def albumInstance = Album.get(params.id)
if (!albumInstance) {
notFound params.id
return
}
def responseJson = [:]
if (request.JSON.version != null) {
if (albumInstance.version > request.JSON.version) {
render status: SC_CONFLICT, text: message(code: 'default.optimistic.locking.failure',
args: [message(code: 'album.label', default: 'Album')],
default: 'Another user has updated this Album while you were editing')
return
}
}
albumInstance.properties = request.JSON
if (albumInstance.save(flush: true)) {
response.status = SC_OK
responseJson.id = albumInstance.id
responseJson.message = message(code: 'default.updated.message', args: [message(code: 'album.label', default: 'Album'), albumInstance.id])
} else {
response.status = SC_UNPROCESSABLE_ENTITY
responseJson.errors = albumInstance.errors.fieldErrors.collectEntries {
[(it.field): message(error: it)]
}
}
cache false
render responseJson as JSON
}
def delete() {
def albumInstance = Album.get(params.id)
if (!albumInstance) {
notFound params.id
return
}
def responseJson = [:]
try {
albumInstance.delete(flush: true)
responseJson.message = message(code: 'default.deleted.message', args: [message(code: 'album.label', default: 'Album'), params.id])
} catch (DataIntegrityViolationException e) {
response.status = SC_CONFLICT
responseJson.message = message(code: 'default.not.deleted.message', args: [message(code: 'album.label', default: 'Album'), params.id])
}
cache false
render responseJson as JSON
}
private void notFound(id) {
response.status = SC_NOT_FOUND
def responseJson = [message: message(code: 'default.not.found.message', args: [message(code: 'album.label', default: 'Album'), params.id])]
render responseJson as JSON
}
}