Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update to work with GSON plugin style controllers
now have an alternate ng-resource setup that maps to Grails resource style URL mappings
- Loading branch information
1 parent
6ec32c0
commit 7af0ab3
Showing
9 changed files
with
347 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 145 additions & 103 deletions
248
test/apps/grails-ng/grails-app/controllers/grails/plugin/angular/test/AlbumController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,154 @@ | ||
package grails.plugin.angular.test | ||
|
||
import grails.plugin.gson.converters.GSON | ||
import org.springframework.dao.DataIntegrityViolationException | ||
import grails.converters.JSON | ||
import static javax.servlet.http.HttpServletResponse.* | ||
import static org.codehaus.groovy.grails.web.servlet.HttpHeaders.* | ||
import static grails.plugin.gson.http.HttpConstants.* | ||
|
||
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 index() { } | ||
|
||
def list(Integer max) { | ||
params.max = Math.min(max ?: 10, 100) | ||
response.addIntHeader X_PAGINATION_TOTAL, Album.count() | ||
render Album.list(params) as GSON | ||
} | ||
|
||
def save() { | ||
if (!requestIsJson()) { | ||
respondNotAcceptable() | ||
return | ||
} | ||
|
||
def albumInstance = new Album(request.GSON) | ||
if (albumInstance.save(flush: true)) { | ||
respondCreated albumInstance | ||
} else { | ||
respondUnprocessableEntity albumInstance | ||
} | ||
} | ||
|
||
def show(long id) { | ||
def albumInstance = Album.get(id) | ||
if (albumInstance) { | ||
respondFound albumInstance | ||
} else { | ||
respondNotFound id | ||
} | ||
} | ||
|
||
def update(long id) { | ||
if (!requestIsJson()) { | ||
respondNotAcceptable() | ||
return | ||
} | ||
|
||
def albumInstance = Album.get(id) | ||
if (!albumInstance) { | ||
respondNotFound id | ||
return | ||
} | ||
|
||
if (params.version != null) { | ||
if (albumInstance.version > params.long('version')) { | ||
respondConflict(albumInstance) | ||
return | ||
} | ||
} | ||
} | ||
|
||
def update() { | ||
println request.JSON | ||
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) { | ||
response.status = SC_CONFLICT | ||
responseJson.message = message(code: 'default.optimistic.locking.failure', | ||
args: [message(code: 'album.label', default: 'Album')], | ||
default: 'Another user has updated this Album while you were editing') | ||
cache false | ||
render responseJson as JSON | ||
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 | ||
} | ||
|
||
albumInstance.properties = request.GSON | ||
|
||
if (albumInstance.save(flush: true)) { | ||
respondUpdated albumInstance | ||
} else { | ||
respondUnprocessableEntity albumInstance | ||
} | ||
} | ||
|
||
def delete(long id) { | ||
def albumInstance = Album.get(id) | ||
if (!albumInstance) { | ||
respondNotFound id | ||
return | ||
} | ||
|
||
try { | ||
albumInstance.delete(flush: true) | ||
respondDeleted id | ||
} catch (DataIntegrityViolationException e) { | ||
respondNotDeleted id | ||
} | ||
} | ||
|
||
private boolean requestIsJson() { | ||
GSON.isJson(request) | ||
} | ||
|
||
private void respondFound(Album albumInstance) { | ||
response.status = SC_OK | ||
render albumInstance as GSON | ||
} | ||
|
||
private void respondUpdated(Album albumInstance) { | ||
response.status = SC_OK | ||
render albumInstance as GSON | ||
} | ||
|
||
private void respondDeleted(id) { | ||
def responseBody = [:] | ||
responseBody.message = message(code: 'default.deleted.message', args: [message(code: 'album.label', default: 'Album'), id]) | ||
response.status = SC_OK | ||
render responseBody as GSON | ||
} | ||
|
||
private void respondCreated(Album albumInstance) { | ||
response.status = SC_CREATED | ||
response.addHeader LOCATION, createLink(action: 'show', id: albumInstance.id) | ||
render albumInstance as GSON | ||
} | ||
|
||
private void respondNotFound(id) { | ||
def responseBody = [:] | ||
responseBody.message = message(code: 'default.not.found.message', args: [message(code: 'album.label', default: 'Album'), id]) | ||
response.status = SC_NOT_FOUND | ||
render responseBody as GSON | ||
} | ||
|
||
private void respondNotAcceptable() { | ||
response.status = SC_NOT_ACCEPTABLE | ||
response.contentLength = 0 | ||
response.outputStream.flush() | ||
response.outputStream.close() | ||
} | ||
|
||
private void respondConflict(Album albumInstance) { | ||
albumInstance.errors.rejectValue('version', 'default.optimistic.locking.failure', | ||
[message(code: 'album.label', default: 'Album')] as Object[], | ||
'Another user has updated this Album while you were editing') | ||
def responseBody = [:] | ||
responseBody.errors = albumInstance.errors.allErrors.collect { | ||
message(error: it) | ||
} | ||
response.status = SC_CONFLICT | ||
render responseBody as GSON | ||
} | ||
|
||
private void respondUnprocessableEntity(Album albumInstance) { | ||
def responseBody = [:] | ||
responseBody.errors = albumInstance.errors.allErrors.collect { | ||
message(error: it) | ||
} | ||
response.status = SC_UNPROCESSABLE_ENTITY | ||
render responseBody as GSON | ||
} | ||
|
||
private void respondNotDeleted(id) { | ||
def responseBody = [:] | ||
responseBody.message = message(code: 'default.not.deleted.message', args: [message(code: 'album.label', default: 'Album'), id]) | ||
response.status = SC_INTERNAL_SERVER_ERROR | ||
render responseBody as GSON | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.