Skip to content

Instantly share code, notes, and snippets.

@urbansky urbansky/spa.groovy
Created Aug 27, 2018

Embed
What would you like to do?
// ------------------------
// Server API
// ------------------------
package de.beispiel.api
import de.beispiel.SuperController
import de.beispiel.crm.CrmProject
import de.beispiel.crm.CrmProjectService
import grails.converters.JSON
class ApiCrmProjectsController extends SuperController {
CrmProjectService crmProjectService
def index(String product) {
log.info("get project list in product ${product}")
List<CrmProject> projects = CrmProject.findAllByProduct(product)
Map<String,?> result = [:]
result.projects = projects.collect { CrmProject project -> encodeProject(project) }
render result as JSON
}
def create(String product) {
log.info("create project in product $product")
CrmProject project = crmProjectService.create(product, [
title: request.JSON.title,
description: request.JSON.description
])
Map result = [ok: true, projectId: project.id, project: encodeProject(project)]
render result as JSON
}
def update(String product, long projectId) {
log.info("update project in product $product")
CrmProject project = crmProjectService.update(CrmProject.get(projectId), [
title: request.JSON.title,
description: request.JSON.description
])
Map result = [ok: true, project: encodeProject(project)]
render result as JSON
}
def delete(String product, long projectId) {
log.info("delete project in product $product")
crmProjectService.delete(CrmProject.get(projectId))
Map result = [ok: true]
render result as JSON
}
private Map encodeProject(CrmProject project) {
[
id: project.id,
title: project.title,
description: project.description
]
}
}
// ------------------------
// URL mapping
// ------------------------
"/api/crm/$product/projects" (controller: "apiCrmProjects", action: "index", method: "GET")
"/api/crm/$product/projects" (controller: "apiCrmProjects", action: "create", method: "POST")
"/api/crm/$product/projects/$projectId" (controller: "apiCrmProjects", action: "delete", method: "DELETE")
"/api/crm/$product/projects/$projectId" (controller: "apiCrmProjects", action: "update", method: "PUT")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.