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

Improve stash api #337

Merged
merged 8 commits into from
Nov 8, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public NetworkModificationController(NetworkModificationService networkModificat
this.lineTypesCatalogService = lineTypesCatalogService;
}

@GetMapping(value = "/groups/{groupUuid}/modifications", produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/groups/{groupUuid}/network-modifications", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Get modifications list of a group")
@ApiResponse(responseCode = "200", description = "List of modifications of the group")
public ResponseEntity<List<ModificationInfos>> getNetworkModifications(@Parameter(description = "Group UUID") @PathVariable("groupUuid") UUID groupUuid,
@Parameter(description = "Only metadata") @RequestParam(name = "onlyMetadata", required = false, defaultValue = "false") Boolean onlyMetadata,
@Parameter(description = "Stashed modifications") @RequestParam(name = "stashed", required = false, defaultValue = "false") Boolean stashed,
@Parameter(description = "Stashed modifications") @RequestParam(name = "onlyStashed", required = false, defaultValue = "false") Boolean onlyStashed,
@Parameter(description = "Return 404 if group is not found or an empty list") @RequestParam(name = "errorOnGroupNotFound", required = false, defaultValue = "true") Boolean errorOnGroupNotFound) {
return ResponseEntity.ok().body(networkModificationService.getNetworkModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, stashed));
return ResponseEntity.ok().body(networkModificationService.getNetworkModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, onlyStashed));
}

@PostMapping(value = "/groups")
Expand Down Expand Up @@ -148,9 +148,15 @@ public ResponseEntity<ModificationInfos> getNetworkModification(
@Operation(summary = "Delete network modifications")
@ApiResponse(responseCode = "200", description = "The network modifications were deleted")
public ResponseEntity<Void> deleteNetworkModifications(
@Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List<UUID> networkModificationUuids,
@Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid) {
networkModificationService.deleteNetworkModifications(groupUuid, networkModificationUuids);
@Parameter(description = "Network modification UUIDs") @RequestParam(name = "uuids", required = false) List<UUID> networkModificationUuids,
@Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid,
@Parameter(description = "delete only stashed modifications") @RequestParam(name = "onlyStashed", required = false, defaultValue = "false") Boolean onlyStashed) {

if (networkModificationUuids == null || networkModificationUuids.isEmpty()) {
networkModificationService.deleteNetworkModifications(groupUuid, onlyStashed);
} else {
networkModificationService.deleteNetworkModifications(groupUuid, networkModificationUuids);
}
return ResponseEntity.ok().build();
}

Expand Down Expand Up @@ -202,23 +208,18 @@ public ResponseEntity<UUID> createModificationInGroup(@RequestBody ModificationI
return ResponseEntity.ok().body(networkModificationService.createModificationInGroup(modificationsInfos));
}

@PostMapping(value = "/network-modifications/stash", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "stash network modifications")
@PutMapping(value = "/network-modifications", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "stash or unstash network modifications")
@ApiResponse(responseCode = "200", description = "The network modifications were stashed")
public ResponseEntity<Void> stashNetworkModifications(
@Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List<UUID> networkModificationUuids,
@Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid) {
networkModificationService.stashNetworkModifications(networkModificationUuids);
return ResponseEntity.ok().build();
}

@PostMapping(value = "/network-modifications/restore", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "restore network modifications")
@ApiResponse(responseCode = "200", description = "The network modifications were restored")
public ResponseEntity<Void> restoreNetworkModifications(
@Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List<UUID> networkModificationUuids,
@Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid) {
networkModificationService.restoreNetworkModifications(networkModificationUuids);
@Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid,
@Parameter(description = "stash or unstash network modifications") @RequestParam(name = "stashed", defaultValue = "true") Boolean stashed) {
if (stashed) {
networkModificationService.stashNetworkModifications(networkModificationUuids);
} else {
networkModificationService.restoreNetworkModifications(networkModificationUuids);
}
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum Type {
GROOVY_SCRIPT_ERROR(HttpStatus.BAD_REQUEST),
NETWORK_NOT_FOUND(HttpStatus.NOT_FOUND),
VARIANT_NOT_FOUND(HttpStatus.NOT_FOUND),
NOTHING_TO_DELETE(HttpStatus.BAD_REQUEST),
MODIFICATION_GROUP_NOT_FOUND(HttpStatus.NOT_FOUND),
MODIFICATION_NOT_FOUND(HttpStatus.NOT_FOUND),
SWITCH_NOT_FOUND(HttpStatus.NOT_FOUND),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public List<ModificationInfos> getModifications(UUID groupUuid, boolean onlyMeta
}

@Transactional(readOnly = true)
public List<ModificationInfos> getModifications(UUID groupUuid, boolean onlyMetadata, boolean errorOnGroupNotFound, boolean stashedModifications) {
public List<ModificationInfos> getModifications(UUID groupUuid, boolean onlyMetadata, boolean errorOnGroupNotFound, boolean onlyStashed) {
try {
return onlyMetadata ? getModificationsMetadata(groupUuid, stashedModifications) : getModificationsInfos(List.of(groupUuid), stashedModifications);
return onlyMetadata ? getModificationsMetadata(groupUuid, onlyStashed) : getModificationsInfos(List.of(groupUuid), onlyStashed);
} catch (NetworkModificationException e) {
if (e.getType() == MODIFICATION_GROUP_NOT_FOUND && !errorOnGroupNotFound) {
return List.of();
Expand All @@ -136,13 +136,19 @@ public List<ModificationInfos> getModifications(UUID groupUuid, boolean onlyMeta
}
}

public List<ModificationInfos> getModificationsMetadata(UUID groupUuid, boolean stashedModifications) {
return modificationRepository
public List<ModificationInfos> getModificationsMetadata(UUID groupUuid, boolean onlyStashed) {
Stream<ModificationEntity> modificationEntitySteam = modificationRepository
.findAllBaseByGroupId(getModificationGroup(groupUuid).getId())
.stream()
.filter(m -> m.getStashed() == stashedModifications)
.map(this::getModificationInfos)
.collect(Collectors.toList());
.stream();
if (onlyStashed) {
return modificationEntitySteam.filter(m -> m.getStashed())
.map(this::getModificationInfos)
.collect(Collectors.toList());
} else {
return modificationEntitySteam
.map(this::getModificationInfos)
.collect(Collectors.toList());
}
}

public TabularModificationEntity loadTabularModificationSubEntities(ModificationEntity modificationEntity) {
Expand All @@ -167,11 +173,16 @@ public ModificationInfos getModificationInfos(ModificationEntity modificationEnt
return modificationEntity.toModificationInfos();
}

public List<ModificationInfos> getModificationsInfos(List<UUID> groupUuids, boolean stashedModifications) {
return groupUuids.stream().flatMap(this::getModificationEntityStream)
.filter(m -> m.getStashed() == stashedModifications)
.map(this::getModificationInfos)
public List<ModificationInfos> getModificationsInfos(List<UUID> groupUuids, boolean onlyStashed) {
Stream<ModificationEntity> modificationEntity = groupUuids.stream().flatMap(this::getModificationEntityStream);
if (onlyStashed) {
return modificationEntity.filter(m -> m.getStashed() == onlyStashed)
.map(ModificationEntity::toModificationInfos)
.collect(Collectors.toList());
} else {
return modificationEntity.map(ModificationEntity::toModificationInfos)
.collect(Collectors.toList());
}
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -199,6 +210,23 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound
}
}

@Transactional
public int deleteModifications(UUID groupUuid, boolean onlyStashed) {
ModificationGroupEntity groupEntity = getModificationGroup(groupUuid);
List<ModificationEntity> modifications;
if (onlyStashed) {
modifications = getModificationEntityStream(groupUuid)
.filter(ModificationEntity::getStashed)
.collect(Collectors.toList());
} else {
modifications = getModificationEntityStream(groupUuid).collect(Collectors.toList());
}
modifications.forEach(groupEntity::removeModification);
int count = modifications.size();
this.modificationRepository.deleteAll(modifications);
return count;
}

@Transactional // To have the find and delete in the same transaction (atomic)
public int deleteModifications(UUID groupUuid, List<UUID> uuids) {
ModificationGroupEntity groupEntity = getModificationGroup(groupUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ public void stopBuildRequest(String receiver) {
notificationService.emitCancelBuildMessage(receiver);
}

public void deleteNetworkModifications(UUID groupUuid, boolean onlyStashed) {
if (networkModificationRepository.deleteModifications(groupUuid, onlyStashed) == 0) {
throw new NetworkModificationException(NOTHING_TO_DELETE);
}
}

public void deleteNetworkModifications(UUID groupUuid, List<UUID> modificationsUuids) {
if (networkModificationRepository.deleteModifications(groupUuid, modificationsUuids) == 0) {
throw new NetworkModificationException(MODIFICATION_NOT_FOUND);
Expand Down Expand Up @@ -293,4 +299,5 @@ public Optional<NetworkModificationResult> duplicateModificationsInGroup(UUID ta
public void deleteStashedModificationInGroup(UUID groupUuid, boolean errorOnGroupNotFound) {
networkModificationRepository.deleteStashedModificationInGroup(groupUuid, errorOnGroupNotFound);
}

}
Loading
Loading