Created
August 27, 2018 18:11
-
-
Save urbansky/6e01c1649a5d91f3fb5218b2d2fc2cfe to your computer and use it in GitHub Desktop.
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
// ------------------------ | |
// 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