Add store order + item id for Ariel

This commit is contained in:
Colin McDonald 2016-07-09 17:21:24 -04:00
parent f999319666
commit 97b296a79d
4 changed files with 32 additions and 13 deletions

View File

@ -54,7 +54,9 @@ public final class GrantConverter implements Block<Document> {
grant.containsKey("created") ? grant.getDate("created").toInstant() : Instant.now(),
null,
null,
null
null,
-1,
-1
);
BlockingCallback<Void> callback = new BlockingCallback<>();

View File

@ -9,6 +9,7 @@ import fr.javatic.mongo.jacksonCodec.objectId.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document;
import org.bson.types.ObjectId;
@ -36,21 +37,24 @@ public final class Grant {
@Getter private Instant removedAt;
@Getter private String removalReason;
@Getter private int storeItemId;
@Getter private int storeOrderId;
public static void findAll(SingleResultCallback<List<Grant>> callback) {
grantsCollection.find().sort(new Document("addedAt", -1)).into(new LinkedList<>(), callback);
grantsCollection.find().sort(new Document("addedAt", -1)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback));
}
public static void findByRank(Collection<Rank> ranks, SingleResultCallback<List<Grant>> callback) {
Collection<String> convertedRanks = ranks.stream().map(Rank::getId).collect(Collectors.toList());
grantsCollection.find(new Document("rank", new Document("$in", convertedRanks))).into(new LinkedList<>(), callback);
grantsCollection.find(new Document("rank", new Document("$in", convertedRanks))).into(new LinkedList<>(), SyncUtils.vertxWrap(callback));
}
public static void findPaginated(Document query, int skip, int pageSize, SingleResultCallback<List<Grant>> callback) {
grantsCollection.find(query).sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new LinkedList<>(), callback);
grantsCollection.find(query).sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new LinkedList<>(), SyncUtils.vertxWrap(callback));
}
public static void findById(String id, SingleResultCallback<Grant> callback) {
grantsCollection.find(new Document("_id", id)).first(callback);
grantsCollection.find(new Document("_id", id)).first(SyncUtils.vertxWrap(callback));
}
public static void findByUser(User user, SingleResultCallback<List<Grant>> callback) {
@ -58,11 +62,11 @@ public final class Grant {
}
public static void findByUser(UUID user, SingleResultCallback<List<Grant>> callback) {
grantsCollection.find(new Document("user", user)).into(new LinkedList<>(), callback);
grantsCollection.find(new Document("user", user)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback));
}
public static void findByUserGrouped(Iterable<UUID> users, SingleResultCallback<Map<UUID, List<Grant>>> callback) {
grantsCollection.find(new Document("user", new Document("$in", users))).into(new LinkedList<>(), (grants, error) -> {
grantsCollection.find(new Document("user", new Document("$in", users))).into(new LinkedList<>(), SyncUtils.vertxWrap((grants, error) -> {
if (error != null) {
callback.onResult(null, error);
} else {
@ -78,12 +82,16 @@ public final class Grant {
callback.onResult(result, null);
}
});
}));
}
private Grant() {} // For Jackson
public Grant(User user, String reason, Set<ServerGroup> scopes, Rank rank, Instant expiresAt, User addedBy) {
this(user, reason, scopes, rank, expiresAt, addedBy, -1, -1);
}
public Grant(User user, String reason, Set<ServerGroup> scopes, Rank rank, Instant expiresAt, User addedBy, int storeItemId, int storeOrderId) {
this.id = new ObjectId().toString();
this.user = user.getId();
this.reason = reason;
@ -92,6 +100,8 @@ public final class Grant {
this.expiresAt = expiresAt;
this.addedBy = addedBy == null ? null : addedBy.getId();
this.addedAt = Instant.now();
this.storeItemId = storeItemId;
this.storeOrderId = storeOrderId;
}
public boolean isActive() {
@ -115,7 +125,7 @@ public final class Grant {
}
public void insert(SingleResultCallback<Void> callback) {
grantsCollection.insertOne(this, callback);
grantsCollection.insertOne(this, SyncUtils.vertxWrap(callback));
}
public void delete(User removedBy, String reason, SingleResultCallback<UpdateResult> callback) {
@ -123,7 +133,7 @@ public final class Grant {
this.removedAt = Instant.now();
this.removalReason = reason;
grantsCollection.replaceOne(new Document("_id", id), this, callback);
grantsCollection.replaceOne(new Document("_id", id), this, SyncUtils.vertxWrap(callback));
}
}

View File

@ -7,6 +7,8 @@ import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.util.ErrorUtils;
import org.bson.Document;
import java.util.stream.Collectors;
public final class GETGrants implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
@ -15,8 +17,9 @@ public final class GETGrants implements Handler<RoutingContext> {
int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize"));
Grant.findPaginated(ctx.request().getParam("user") == null ? new Document() : new Document("user", ctx.request().getParam("user")), skip, pageSize, (grants, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
if (ctx.request().getParam("active") != null) {
boolean requireActive = Boolean.parseBoolean(ctx.request().getParam("active"));
APIv3.respondJson(ctx, 200, grants.stream().filter(grant -> grant.isActive() == requireActive).collect(Collectors.toList()));
} else {
APIv3.respondJson(ctx, 200, grants);
}

View File

@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableMap;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.auditLog.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType;
@ -91,7 +92,10 @@ public final class POSTGrants implements Handler<RoutingContext> {
}
}
Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy);
int storeItemId = requestBody.getInteger("storeItemId", -1);
int storeOrderId = requestBody.getInteger("storeOrderId", -1);
Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy, storeItemId, storeOrderId);
BlockingCallback<Void> callback = new BlockingCallback<>();
grant.insert(callback);
callback.get();