Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete multiple elements #76

Merged
merged 7 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/gridsuite/explore/server/ExploreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ public ResponseEntity<Void> deleteElement(@PathVariable("elementUuid") UUID elem
return ResponseEntity.ok().build();
}

@DeleteMapping(value = "/explore/elements/{directoryUuid}/delete-stashed")
@Operation(summary = "Remove directories/elements")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "directories/elements was successfully removed")})
public ResponseEntity<Void> deleteElements(@RequestParam("ids") List<UUID> elementsUuid,
@RequestHeader("userId") String userId,
@PathVariable String directoryUuid) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we must have an uuid at this position or the gateway will reject our fetch with 403 status

exploreService.deleteElements(elementsUuid, userId);
return ResponseEntity.ok().build();
}

@GetMapping(value = "/explore/elements/metadata", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "get element infos from ids given as parameters")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The elements information")})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ public void deleteDirectoryElement(UUID elementUuid, String userId) {
restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class);
}

public void deleteDirectoryElements(List<UUID> elementUuids, String userId) {
var ids = elementUuids.stream().map(UUID::toString).collect(Collectors.joining(","));
String path = UriComponentsBuilder
.fromPath(ELEMENTS_SERVER_ROOT_PATH + "?ids=" + ids)
.buildAndExpand()
.toUriString();
HttpHeaders headers = new HttpHeaders();
headers.add(HEADER_USER_ID, userId);
restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class);
}

public ElementAttributes getElementInfos(UUID elementUuid) {
String path = UriComponentsBuilder
.fromPath(ELEMENTS_SERVER_ROOT_PATH + "/{elementUuid}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ public void deleteElement(UUID id, String userId) {
}
}

public void deleteElements(List<UUID> uuids, String userId) {
try {
uuids.forEach(id -> directoryService.deleteElement(id, userId));
// FIXME dirty fix to ignore errors and still delete the elements in the directory-server. To delete when handled properly.
} catch (Exception e) {
LOGGER.error(e.toString(), e);
} finally {
directoryService.deleteDirectoryElements(uuids, userId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this method is called in both try and catch !?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same logic of this PR: #27. we ignore the exception and do the deletion anyway

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider this suggestion
public void deleteElements(List uuids, String userId) {
try {
uuids.forEach(id -> directoryService.deleteElement(id, userId));
// FIXME dirty fix to ignore errors and still delete the elements in the directory-server. To delete when handled properly.
} catch (Exception e) {
LOGGER.error(e.toString(), e);
} finally {
directoryService.deleteDirectoryElements(uuids, userId);
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}

public void updateFilter(UUID id, String filter, String userId, String name) {
filterService.updateFilter(id, filter, userId);
updateElementName(id, name, userId);
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/gridsuite/explore/server/ExploreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -297,6 +298,8 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/parameters/" + PARAMETERS_UUID)) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("\\/v1\\/elements\\?ids=([^,]+,){2,}[^,]+$")) {
return new MockResponse().setResponseCode(200);
}
return new MockResponse().setResponseCode(404);
}
Expand Down Expand Up @@ -460,6 +463,13 @@ public void deleteElement(UUID elementUUid) throws Exception {
.andExpect(status().isOk());
}

public void deleteElements(List<UUID> elementUuids, UUID parentUuid) throws Exception {
var ids = elementUuids.stream().map(UUID::toString).collect(Collectors.joining(","));
mockMvc.perform(delete("/v1/explore/elements/{parentUuid}/delete-stashed?ids=" + ids, parentUuid)
.header("userId", USER1))
.andExpect(status().isOk());
}

public void deleteElementInvalidType(UUID elementUUid) throws Exception {
mockMvc.perform(delete("/v1/explore/elements/{elementUuid}", elementUUid)
.header("userId", USER1))
Expand All @@ -468,6 +478,7 @@ public void deleteElementInvalidType(UUID elementUUid) throws Exception {

@Test
public void testDeleteElement() throws Exception {
deleteElements(List.of(FILTER_UUID, PRIVATE_STUDY_UUID, CONTINGENCY_LIST_UUID, CASE_UUID), PARENT_DIRECTORY_UUID);
deleteElement(FILTER_UUID);
deleteElement(PRIVATE_STUDY_UUID);
deleteElement(CONTINGENCY_LIST_UUID);
Expand Down
Loading