diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java index bb4db64..8aeaa2e 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java @@ -54,7 +54,9 @@ public final class GrantConverter implements Block { grant.containsKey("created") ? grant.getDate("created").toInstant() : Instant.now(), null, null, - null + null, + -1, + -1 ); BlockingCallback callback = new BlockingCallback<>(); diff --git a/src/main/java/net/frozenorb/apiv3/model/Grant.java b/src/main/java/net/frozenorb/apiv3/model/Grant.java index 9d457a0..9ba66f2 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -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> 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 ranks, SingleResultCallback> callback) { Collection 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> 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 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> callback) { @@ -58,11 +62,11 @@ public final class Grant { } public static void findByUser(UUID user, SingleResultCallback> 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 users, SingleResultCallback>> 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 scopes, Rank rank, Instant expiresAt, User addedBy) { + this(user, reason, scopes, rank, expiresAt, addedBy, -1, -1); + } + + public Grant(User user, String reason, Set 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 callback) { - grantsCollection.insertOne(this, callback); + grantsCollection.insertOne(this, SyncUtils.vertxWrap(callback)); } public void delete(User removedBy, String reason, SingleResultCallback 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)); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java b/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java index fc43da8..e2bd0e4 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java @@ -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 { public void handle(RoutingContext ctx) { @@ -15,8 +17,9 @@ public final class GETGrants implements Handler { 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); } diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java b/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java index efe652e..5e20e89 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java @@ -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 { } } - 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 callback = new BlockingCallback<>(); grant.insert(callback); callback.get();