Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Scaffold annotation for Controllers and Services #118

Merged
merged 13 commits into from
Sep 10, 2024
Merged
Prev Previous commit
Next Next commit
GenericController implementation that utilizes services
codeconsole committed Sep 7, 2024
commit 69bf0e36204fb7554a90377e4c6534e60860a7a1
43 changes: 43 additions & 0 deletions src/main/groovy/grails/plugin/scaffolding/GenericController.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package grails.plugin.scaffolding

import grails.artefact.Artefact
import grails.gorm.transactions.ReadOnly
import grails.rest.RestfulController
import grails.util.Holders

@Artefact("Controller")
@ReadOnly
class GenericController<T> extends RestfulController<T> {

GenericController(Class<T> resource, boolean readOnly) {
super(resource, readOnly)
}

protected def getService() {
Holders.grailsApplication.getMainContext().getBean(resourceName + 'Service')
}

protected T queryForResource(Serializable id) {
getService().get(id)
}

protected List<T> listAllResources(Map params) {
getService().list(params)
}

protected Integer countResources() {
getService().count()
}

protected T saveResource(T resource) {
getService().save(resource)
}

protected T updateResource(T resource) {
getService().save(resource)
}

protected void deleteResource(T resource) {
getService().delete(resource)
}
}