Skip to content

Commit

Permalink
Added a delete data set operation #934
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed May 2, 2024
1 parent 460cfc2 commit f3a59ff
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
4 changes: 4 additions & 0 deletions grails-app/controllers/au/org/ala/ecodata/UrlMappings.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
18 changes: 17 additions & 1 deletion grails-app/services/au/org/ala/ecodata/ProjectService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
}
Expand All @@ -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)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
}
22 changes: 22 additions & 0 deletions src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -806,4 +806,26 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest<ProjectSer
actual3.custom.dataSets == [dataSet, dataSet2]
}
void "The deleteDataSet method will delete a dataSet from a Project"() {
setup:
Map dataSet = [name: 'Test Data Set', description: 'Test Description', dataSetId:'d1']
Project project = new Project(projectId: '345', name: "Project 345", isMERIT: true, hubId:"12345", custom:[dataSets:[dataSet]])
project.save(flush: true, failOnError: true)
when:
Map resp = service.deleteDataSet(project.projectId, 'd1')
then:
resp.status == 'ok'
Project actual = Project.findByProjectId(project.projectId)
actual.projectId == project.projectId
actual.name == project.name
actual.isMERIT == project.isMERIT
actual.hubId == project.hubId
actual.custom.dataSets == []
}
}

0 comments on commit f3a59ff

Please sign in to comment.