Skip to content

Commit

Permalink
feat(api): add update and delete resource REST API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
cmark committed Aug 11, 2022
1 parent e93433c commit b79a13c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
*/
package com.b2international.snowowl.core.rest.resource;

import java.util.concurrent.TimeUnit;

import org.elasticsearch.common.Strings;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import com.b2international.commons.exceptions.NotFoundException;
import com.b2international.snowowl.core.Resource;
import com.b2international.snowowl.core.Resources;
import com.b2international.snowowl.core.events.util.Promise;
import com.b2international.snowowl.core.request.ResourceRequests;
import com.b2international.snowowl.core.rest.AbstractRestService;
import com.b2international.snowowl.core.rest.domain.ResourceRequest;
import com.b2international.snowowl.core.rest.domain.ResourceSelectors;

import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -116,4 +122,64 @@ public Promise<Resource> get(
.buildAsync(timestamp)
.execute(getBus());
}

@Operation(
summary = "Update a resource by its unique identifier",
description = "Updates a resource definition in the system using its identifier and the given patch update."
)
@ApiResponses({
@ApiResponse(responseCode = "204", description = "No Content"),
@ApiResponse(responseCode = "400", description = "Bad Request"),
})
@PutMapping(value = "/{resourceId}", consumes = { AbstractRestService.JSON_MEDIA_TYPE })
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(
@Parameter(description = "The resource identifier")
@PathVariable(value = "resourceId")
final String resourceId,

@RequestBody
final ResourceRequest<ResourceUpdateRestInput> body,

@RequestHeader(value = X_AUTHOR, required = false)
final String author) {
final String commitComment = Strings.isNullOrEmpty(body.getCommitComment()) ? String.format("Updated Resource %s", resourceId) : body.getCommitComment();
body.getChange().toUpdateRequest(resourceId)
.build(author, commitComment)
.execute(getBus())
.getSync(COMMIT_TIMEOUT, TimeUnit.MINUTES);
}

@Operation(
summary="Delete a Resource",
description="Deletes a Resource permanently from the server")
@ApiResponses({
@ApiResponse(responseCode = "204", description = "No content"),
@ApiResponse(responseCode = "400", description = "Bad Request"),
@ApiResponse(responseCode = "409", description = "Resource cannot be deleted")
})
@DeleteMapping(value = "/{resourceId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(
@Parameter(description = "The resource identifier")
@PathVariable(value="resourceId")
final String resourceId,

@RequestHeader(value = X_AUTHOR, required = false)
final String author) {
try {
final Resource codeSystem = ResourceRequests.prepareGet(resourceId)
.buildAsync()
.execute(getBus())
.getSync(1, TimeUnit.MINUTES);

ResourceRequests.prepareDelete(resourceId)
.build(author, String.format("Deleted resource %s", codeSystem.getTitle()))
.execute(getBus())
.getSync(COMMIT_TIMEOUT, TimeUnit.MINUTES);
} catch(NotFoundException e) {
// already deleted, ignore error
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2022 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.b2international.snowowl.core.rest.resource;

import com.b2international.snowowl.core.request.ResourceRequests;
import com.b2international.snowowl.core.request.ResourceUpdateRequestBuilder;
import com.b2international.snowowl.core.rest.BaseResourceUpdateRestInput;

/**
* @since 8.5.1
*/
public class ResourceUpdateRestInput extends BaseResourceUpdateRestInput {

public ResourceUpdateRequestBuilder toUpdateRequest(String resourceId) {
return ResourceRequests.prepareUpdate(resourceId)
.setUrl(getUrl())
.setTitle(getTitle())
.setLanguage(getLanguage())
.setDescription(getDescription())
.setStatus(getStatus())
.setCopyright(getCopyright())
.setOwner(getOwner())
.setContact(getContact())
.setUsage(getUsage())
.setPurpose(getPurpose())
.setBundleId(getBundleId())
.setSettings(getSettings());
}

}

0 comments on commit b79a13c

Please sign in to comment.