Skip to content

Commit

Permalink
Delete multiple elements (#76)
Browse files Browse the repository at this point in the history
Signed-off-by: Seddik Yengui <[email protected]>
  • Loading branch information
YenguiSeddik authored Feb 26, 2024
1 parent 3b9d35a commit 5e625cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
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) {
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);
}
}

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

0 comments on commit 5e625cf

Please sign in to comment.