Skip to content

Instantly share code, notes, and snippets.

@alvarosanchez
Created June 2, 2014 01:20
Show Gist options
  • Save alvarosanchez/c74054d9422d24ddb256 to your computer and use it in GitHub Desktop.
Save alvarosanchez/c74054d9422d24ddb256 to your computer and use it in GitHub Desktop.
package com.odobo.gr8conf
import grails.rest.RestfulController
import grails.transaction.Transactional
@Transactional(readOnly = true)
class CategoryController extends RestfulController {
static responseFormats = ['json', 'xml']
CategoryController() {
super(Category)
}
@Override
protected List<Category> listAllResources(Map params) {
if (params.gameId) {
Game.get(params.gameId).categories as List
} else {
Category.list(params)
}
}
@Override
protected Integer countResources() {
if (params.gameId) {
Game.get(params.gameId).categories.size()
} else {
Category.count()
}
}
}
package com.odobo.gr8conf
import spock.lang.Shared
import spock.lang.Specification
import wslite.rest.ContentType
import wslite.rest.RESTClient
import wslite.rest.Response
class CategorySpec extends Specification {
@Shared
RESTClient restClient = new RESTClient("http://localhost:8080/restful-gr8conf-2014")
def "it returns all the cateogories"() {
when:
Response response = restClient.get(path: "/categories", accept: ContentType.JSON)
then:
response.json.size() == 5
}
def "it returns just the categories of a game when is a nested resource"() {
when:
Response response = restClient.get(path: "/games/5/categories", accept: ContentType.JSON)
then:
response.json.size() == 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment