Created
November 14, 2008 17:18
-
-
Save kitplummer/25000 to your computer and use it in GitHub Desktop.
color
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
class ProductController { | |
def index = { redirect(action:list,params:params) } | |
// the delete, save and update actions only accept POST requests | |
def allowedMethods = [delete:'POST', save:'POST', update:'POST'] | |
def list = { | |
if(!params.max) params.max = 10 | |
[ productList: Product.list( params ) ] | |
} | |
def show = { | |
def product = Product.get( params.id ) | |
if(!product) { | |
flash.message = "Product not found with id ${params.id}" | |
redirect(action:list) | |
} | |
else { return [ product : product ] } | |
} | |
def delete = { | |
def product = Product.get( params.id ) | |
if(product) { | |
product.delete() | |
flash.message = "Product ${params.id} deleted" | |
redirect(action:list) | |
} | |
else { | |
flash.message = "Product not found with id ${params.id}" | |
redirect(action:list) | |
} | |
} | |
def edit = { | |
def product = Product.get( params.id ) | |
if(!product) { | |
flash.message = "Product not found with id ${params.id}" | |
redirect(action:list) | |
} | |
else { | |
return [ product : product ] | |
} | |
} | |
def update = { | |
def product = Product.get( params.id ) | |
if(product) { | |
product.properties = params | |
if(!product.hasErrors() && product.save()) { | |
flash.message = "Product ${params.id} updated" | |
redirect(action:show,id:product.id) | |
} | |
else { | |
render(view:'edit',model:[product:product]) | |
} | |
} | |
else { | |
flash.message = "Product not found with id ${params.id}" | |
redirect(action:edit,id:params.id) | |
} | |
} | |
def create = { | |
def product = new Product() | |
product.properties = params | |
return ['product':product] | |
} | |
def save = { | |
def product = new Product(params) | |
if(!product.hasErrors() && product.save()) { | |
flash.message = "Product ${product.id} created" | |
redirect(action:show,id:product.id) | |
} | |
else { | |
render(view:'create',model:[product:product]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment