Skip to content

Commit

Permalink
Merge pull request #36 from coolcrowd/delete
Browse files Browse the repository at this point in the history
Delete
  • Loading branch information
LeanderK committed Mar 18, 2016
2 parents d6053fd + 9fe6c8f commit d0737bf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public void init() throws Exception {
ctx.getResponse().getHeaders().add("access-control-expose-headers", "Link,Location");
ctx.getResponse().getHeaders().add("access-control-max-age", "86400");
String jwtHeader = ctx.getRequest().getHeaders().get("Authorization");
if (jwtHeader == null) {
jwtHeader = ctx.getRequest().getQueryParams().get("Authorization");
}
if (jwtHeader != null) {
if (!jwtHeader.matches("Bearer .*")) {
throw new BadRequestException("Authorization header must contain Bearer token in the format <Bearer JWT>");
Expand All @@ -81,6 +84,7 @@ public void init() throws Exception {
.post("answers", doAuthorized(ctx -> ctx.render(commands.submitAnswer(ctx))))
.post("ratings", doAuthorized(ctx -> ctx.render(commands.submitRating(ctx))))
.post("calibrations", doAuthorized(ctx -> ctx.render(commands.submitCalibration(ctx))))
.post("delete", doAuthorized(ctx -> ctx.render(commands.deleteWorker(ctx))))
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.exec.Promise;
import ratpack.exec.Upstream;
import ratpack.handling.Context;
import ratpack.http.TypedData;

Expand All @@ -31,6 +32,7 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collector;

/**
Expand Down Expand Up @@ -194,6 +196,29 @@ public Promise<String> submitRating(Context context) {
});
}

/**
* deletes the worker from the database
* @param context the context to use
* @return return success or no success
*/
public Promise<String> deleteWorker(Context context) {
int workerID = context.get(WorkerID.class).get();
Upstream<Integer> objectUpstream = downstream -> downstream.accept(
communication.deleteWorker(workerID)
.handle((BiFunction<? super Integer, Throwable, Integer>) (t, throwable) ->
wrapExceptionOr201(t, throwable, context)));

return Promise.of(objectUpstream)
.map(status -> {
if (status == 201) {
return String.format("successfully deleted worker %d", workerID);
} else {
throw new NotFoundException(String.format(
"Unable to delete worker %d, object-service answered with %d.", workerID, status));
}
});
}

/**
* if the throwable is not null it wraps the throwable into an InternalServerErrorException, or else it sets the
* response to 201 Created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ public CompletableFuture<HttpResponse<JsonNode>> submitCalibration(int option, i
.thenApply(response -> throwOr(response, () -> null));
}

/**
* deletes a worker from the system
* @param id the primary key of the worker
* @return 204 if successfully, 404 if not
*/
public CompletableFuture<Integer> deleteWorker(int id) {
String route = String.format("/workers/%d", id);
return doAsync(Unirest.delete(url + route)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.basicAuth(username, password),
builder -> builder)
.thenApply(response -> throwOr(response, () -> response))
.thenApply(HttpResponse::getStatus);
}

/**
* tries to get the workerID from the request.
* Calls 'GET /get/:platform' from the Object-Service.
Expand Down

0 comments on commit d0737bf

Please sign in to comment.