Skip to content

Commit

Permalink
Merge pull request #118 from vlingo/feature/query_version_by_ref
Browse files Browse the repository at this point in the history
Support pulling schema versions by reference
  • Loading branch information
VaughnVernon authored Jan 12, 2020
2 parents 519f0c8 + 12c62d0 commit ecbaf59
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ private Completes<SchemaVersionData> queryGreatestByNames(final String organizat
private Completes<SchemaVersionData> queryOne(final String query, final Map<String,String> parameters) {
final QueryExpression expression = MapQueryExpression.using(SchemaVersionState.class, query, parameters);

return queryObject(SchemaVersionState.class, expression, (SchemaVersionState state) -> SchemaVersionData.from(state));
return queryObject(SchemaVersionState.class, expression, (SchemaVersionState state) -> state == null
? SchemaVersionData.none()
: SchemaVersionData.from(state));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,82 @@ public Completes<Response> pushSchemaVersion(final String reference, final Schem
));
}

public Completes<Response> retrieveSchemaVersion(final String reference) {
FullyQualifiedReference fqr;
try {
fqr = FullyQualifiedReference.from(reference);
} catch (IllegalArgumentException ex) {
return Completes.withSuccess(Response.of(
BadRequest,
Headers.of(of(ContentLength, ex.getMessage().length())),
ex.getMessage()));
}

if (!fqr.isSchemaVersionReference()) {
final String msg = "Include the version of the schema to retrieve";
return Completes.withSuccess(Response.of(
BadRequest,
Headers.of(of(ContentLength, msg.length())),
msg));
}

return Queries.forSchemaVersions().schemaVersionOf(
fqr.organization,
fqr.unit,
fqr.context,
fqr.schema,
fqr.schemaVersion)
.andThenTo(schemaVersionData -> schemaVersionData.isNone()
? Completes.withSuccess(
Response.of(
NotFound,
"Schema version not found"))
: Completes.withSuccess(
Response.of(
Ok,
Headers.of(of(ContentType, "application/json; charset=UTF-8")),
serialized(schemaVersionData)))
);
}

public Completes<Response> retrieveSchemaVersionStatus(final String reference) {
FullyQualifiedReference fqr;
try {
fqr = FullyQualifiedReference.from(reference);
} catch (IllegalArgumentException ex) {
return Completes.withSuccess(Response.of(
BadRequest,
Headers.of(of(ContentLength, ex.getMessage().length())),
ex.getMessage()));
}

if (!fqr.isSchemaVersionReference()) {
final String msg = "Include the version of the schema to retrieve";
return Completes.withSuccess(Response.of(
BadRequest,
Headers.of(of(ContentLength, msg.length())),
msg));
}

return Queries.forSchemaVersions().schemaVersionOf(
fqr.organization,
fqr.unit,
fqr.context,
fqr.schema,
fqr.schemaVersion)
.andThenTo(schemaVersionData -> schemaVersionData.isNone()
? Completes.withSuccess(
Response.of(
NotFound,
"Schema version not found"))
: Completes.withSuccess(
Response.of(
Ok,
Headers.of(of(ContentType, "text/plain; charset=UTF-8")),
schemaVersionData.status))
);
}

@Override
public Resource<?> routes() {
return resource("SchemaVersion Resource", 1,
Expand Down Expand Up @@ -246,7 +322,13 @@ public Resource<?> routes() {
post("/versions/{reference}")
.param(String.class)
.body(SchemaVersionData.class)
.handle(this::pushSchemaVersion));
.handle(this::pushSchemaVersion),
get("/versions/{reference}")
.param(String.class)
.handle(this::retrieveSchemaVersion),
get("/versions/{reference}/status")
.param(String.class)
.handle(this::retrieveSchemaVersionStatus));
}

private String schemaVersionLocation(final SchemaVersionId schemaVersionId) {
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/rest-api-calls.http
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ Accept: application/json
GET http://localhost:9019/versions/search?organization=Org3&unit=Unit3&context=io.vlingo.schemata5&schema=SchemaDefinedFoo
Accept: application/json

### Retrieve schema versions by reference
GET http://localhost:9019/versions/Org3:Unit3:io.vlingo.schemata5:SchemaDefinedFoo:1.0.0
Accept: application/json


### Search for all schema versions by names


Expand Down

0 comments on commit ecbaf59

Please sign in to comment.