Skip to content

Instantly share code, notes, and snippets.

@sergiomichels
Created August 29, 2013 16:42
Show Gist options
  • Select an option

  • Save sergiomichels/6380504 to your computer and use it in GitHub Desktop.

Select an option

Save sergiomichels/6380504 to your computer and use it in GitHub Desktop.
Params is playing with me?
class Category {
String name
String description
Category parent
static hasMany = [subcategories : Category]
static mappedBy = [children: 'parent']
static constraints = {
name blank: false, maxSize: 30
description blank: false, maxSize: 100
parent nullable: true
}
static mapping = {
subcategories sort: "id"
}
static Category getRoot() {
def cat = Category.executeQuery(" from Category where parent is null ")
return cat ? cat[0] : null
}
boolean isLeaf() {
return subcategories ? (subcategories.size() > 0 ? false : true) : true
}
@Override
public String toString() {
"[category: $name, description: $description, leaf: ${isLeaf()}]"
}
}
class CategoryController extends RestfulController<Category> {
//def categoryService
static responseFormats = ['json']
CategoryController() {
super(Category)
}
@Override
protected Category createResource(Map params) {
Category instance = super.createResource(params);
println "params: $params"
if(params.parent?.id) {
instance.parent = Category.get(params.parent.id)
}
return instance
}
@Override
protected Map getParametersToBind() {
println "PARAMS II: $params"
return super.getParametersToBind();
}
//def tree() {
// render (categoryService.getCategoryTree() as JSON)
//}
}
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}
"/category/tree" (controller: "category", action: "tree")
"/category"(resources: "category")
"/"(controller: 'app', action: 'redir')
"500"(view:'/error')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment