From f3a59ff4bbe25b303115d95d70d341311ce97e31 Mon Sep 17 00:00:00 2001 From: chrisala Date: Fri, 3 May 2024 09:59:40 +1000 Subject: [PATCH] Added a delete data set operation #934 --- .../ecodata/DataSetSummaryController.groovy | 14 ++++++++---- .../au/org/ala/ecodata/UrlMappings.groovy | 4 ++++ .../au/org/ala/ecodata/ProjectService.groovy | 18 ++++++++++++++- .../DataSetSummaryControllerSpec.groovy | 14 ++++++++++++ .../org/ala/ecodata/ProjectServiceSpec.groovy | 22 +++++++++++++++++++ 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/grails-app/controllers/au/org/ala/ecodata/DataSetSummaryController.groovy b/grails-app/controllers/au/org/ala/ecodata/DataSetSummaryController.groovy index 666c8f3bb..7f5b629e2 100644 --- a/grails-app/controllers/au/org/ala/ecodata/DataSetSummaryController.groovy +++ b/grails-app/controllers/au/org/ala/ecodata/DataSetSummaryController.groovy @@ -5,17 +5,15 @@ import org.apache.http.HttpStatus class DataSetSummaryController { static responseFormats = ['json', 'xml'] - static allowedMethods = [update:['POST', 'PUT']] + static allowedMethods = [update:['POST', 'PUT'], delete:'DELETE'] ProjectService projectService /** Updates a single dataset for a project */ def update(String projectId) { Map dataSet = request.JSON + projectId = projectId ?: dataSet.projectId - if (!projectId) { - projectId = dataSet.projectId - } if (!projectId) { render status: HttpStatus.SC_BAD_REQUEST, text: "projectId is required" return @@ -28,4 +26,12 @@ class DataSetSummaryController { respond projectService.updateDataSet(projectId, dataSet) } + + def delete(String projectId, String dataSetId) { + if (!projectId || !dataSetId) { + render status: HttpStatus.SC_BAD_REQUEST, text: "projectId and dataSetId are required" + return + } + respond projectService.deleteDataSet(projectId, dataSetId) + } } diff --git a/grails-app/controllers/au/org/ala/ecodata/UrlMappings.groovy b/grails-app/controllers/au/org/ala/ecodata/UrlMappings.groovy index 7f85d0b43..4e2f62986 100644 --- a/grails-app/controllers/au/org/ala/ecodata/UrlMappings.groovy +++ b/grails-app/controllers/au/org/ala/ecodata/UrlMappings.groovy @@ -196,6 +196,10 @@ class UrlMappings { "/ws/project/getDefaultFacets"(controller: "project", action: "getDefaultFacets") "/ws/project/$projectId/dataSet/$dataSetId/records"(controller: "project", action: "fetchDataSetRecords") "/ws/admin/initiateSpeciesRematch"(controller: "admin", action: "initiateSpeciesRematch") + "/ws/dataSetSummary/$projectId/$dataSetId?"(controller :'dataSetSummary') { + + action = [POST:'update', PUT:'update', DELETE:'delete'] + } "/ws/document/download"(controller:"document", action:"download") diff --git a/grails-app/services/au/org/ala/ecodata/ProjectService.groovy b/grails-app/services/au/org/ala/ecodata/ProjectService.groovy index ec5467880..41b8b09a0 100644 --- a/grails-app/services/au/org/ala/ecodata/ProjectService.groovy +++ b/grails-app/services/au/org/ala/ecodata/ProjectService.groovy @@ -1077,7 +1077,9 @@ class ProjectService { Map updateDataSet(String projectId, Map dataSet) { synchronized (PROJECT_UPDATE_LOCKS.get(projectId)) { Project project = Project.findByProjectId(projectId) - + if (!project) { + return [status: 'error', error: "No project exists with projectId=${projectId}"] + } if (!dataSet.dataSetId) { dataSet.dataSetId = Identifiers.getNew(true, '') } @@ -1097,4 +1099,18 @@ class ProjectService { } } + Map deleteDataSet(String projectId, String dataSetId) { + synchronized (PROJECT_UPDATE_LOCKS.get(projectId)) { + Project project = Project.findByProjectId(projectId) + + boolean foundMatchingDataSet = project?.custom?.dataSets?.removeAll { it.dataSetId == dataSetId } + if (!foundMatchingDataSet) { + return [status: 'error', error: 'No such data set'] + } + else { + update([custom: project.custom], project.projectId, false) + } + } + } + } \ No newline at end of file diff --git a/src/test/groovy/au/org/ala/ecodata/DataSetSummaryControllerSpec.groovy b/src/test/groovy/au/org/ala/ecodata/DataSetSummaryControllerSpec.groovy index eeee899a8..46d7fc1e1 100644 --- a/src/test/groovy/au/org/ala/ecodata/DataSetSummaryControllerSpec.groovy +++ b/src/test/groovy/au/org/ala/ecodata/DataSetSummaryControllerSpec.groovy @@ -43,4 +43,18 @@ class DataSetSummaryControllerSpec extends Specification implements ControllerUn 0 * projectService.updateDataSet(_, _) response.status == HttpStatus.SC_BAD_REQUEST } + + void "The delete method delegates to the projectService"() { + setup: + String projectId = 'p1' + String dataSetSummaryId = 'd1' + + when: + request.method = 'DELETE' + controller.delete(projectId, dataSetSummaryId) + + then: + 1 * projectService.deleteDataSet(projectId, dataSetSummaryId) >> [status:'ok'] + response.json == ['status':'ok'] + } } diff --git a/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy b/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy index dd9b6d1fa..856ee7b16 100644 --- a/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy +++ b/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy @@ -806,4 +806,26 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest