Skip to content

Commit

Permalink
separate version validation into reusable methods
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Ceni <[email protected]>
  • Loading branch information
lceni committed Oct 18, 2024
1 parent 11049bd commit cf489bc
Showing 1 changed file with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ private IterateChangesetsEvent buildIterateChangesetsEvent(final RoutingContext
if (useChangesetCollection) {
startVersion = Query.getLong(context, Query.START_VERSION, 0L);
endVersion = Query.getLong(context, Query.END_VERSION, null);

validateVersion(startVersion, true);
validateVersion(endVersion, false);
validateVersions(startVersion, endVersion);
} else {
final String version = context.pathParam(Path.VERSION);
startVersion = Long.parseLong(version);
endVersion = startVersion;
}
final Long version = Query.getLong(context, Query.VERSION, null);
validateVersion(version, true);

validateGetChangesetsQueryParams(startVersion, endVersion, useChangesetCollection);
startVersion = version;
endVersion = version;
}

return new IterateChangesetsEvent()
.withSpace(getSpaceId(context))
Expand All @@ -123,21 +127,17 @@ private IterateChangesetsEvent buildIterateChangesetsEvent(final RoutingContext
.withLimit(limit);
}

private void validateGetChangesetsQueryParams(Long startVersion, Long endVersion, boolean useChangesetCollection)
throws HttpException {
if (useChangesetCollection) {
if (startVersion != null && endVersion != null) {
if (startVersion < 0 || endVersion < 0)
throw new HttpException(HttpResponseStatus.BAD_REQUEST, "Invalid version specified.");
if (startVersion > endVersion)
throw new HttpException(HttpResponseStatus.BAD_REQUEST, "The parameter startVersion needs to be smaller as endVersion.");
}
} else {
if (startVersion == null)
throw new HttpException(HttpResponseStatus.BAD_REQUEST, "The parameter version is required.");
}
private void validateVersion(Long version, boolean required) throws HttpException {
if (required && version == null)
throw new HttpException(HttpResponseStatus.BAD_REQUEST, "The parameter version is required.");
if (version != null && version < 0)
throw new HttpException(HttpResponseStatus.BAD_REQUEST, "Invalid version specified.");
}

private void validateVersions(Long startVersion, Long endVersion) throws HttpException {
if (endVersion != null && startVersion > endVersion)
throw new HttpException(HttpResponseStatus.BAD_REQUEST, "The parameter startVersion needs to be smaller as endVersion.");
}

/**
* Delete changesets by version number
Expand Down

0 comments on commit cf489bc

Please sign in to comment.