Allow specifying the user to search for in GET /auditLog, /punishments, /grants, and /ipBans

This commit is contained in:
Colin McDonald 2016-06-23 00:54:40 -04:00
parent 2eeabf7ef8
commit c00b424fe8
7 changed files with 28 additions and 25 deletions

View File

@ -38,8 +38,8 @@ public final class Grant {
@Getter private Instant removedAt;
@Getter private String removalReason;
public static List<Grant> findAllSync() {
return SyncUtils.blockMulti(grantsCollection.find().sort(new Document("addedAt", -1)));
public static void findAll(SingleResultCallback<List<Grant>> callback) {
grantsCollection.find().sort(new Document("addedAt", -1)).into(new LinkedList<>(), callback);
}
public static List<Grant> findByRankSync(Collection<Rank> ranks) {
@ -59,8 +59,8 @@ public final class Grant {
return SyncUtils.blockMulti(grantsCollection.find(new Document("user", user)));
}
public static void findAllPaginated(int skip, int pageSize, SingleResultCallback<List<Grant>> callback) {
grantsCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), 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);
}
public static void findById(String id, SingleResultCallback<Grant> callback) {
@ -72,18 +72,18 @@ public final class Grant {
}
public static void findByUser(UUID user, SingleResultCallback<List<Grant>> callback) {
grantsCollection.find(new Document("user", user)).into(new ArrayList<>(), callback);
grantsCollection.find(new Document("user", user)).into(new LinkedList<>(), 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 ArrayList<>(), (grants, error) -> {
grantsCollection.find(new Document("user", new Document("$in", users))).into(new LinkedList<>(), (grants, error) -> {
if (error != null) {
callback.onResult(null, error);
} else {
Map<UUID, List<Grant>> result = new HashMap<>();
for (UUID user : users) {
result.put(user, new ArrayList<>());
result.put(user, new LinkedList<>());
}
for (Grant grant : grants) {

View File

@ -43,8 +43,8 @@ public final class IpBan {
return SyncUtils.blockOne(ipBansCollection.find(new Document("_id", id)));
}
public static void findAllPaginated(int skip, int pageSize, SingleResultCallback<List<IpBan>> callback) {
ipBansCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback);
public static void findPaginated(Document query, int skip, int pageSize, SingleResultCallback<List<IpBan>> callback) {
ipBansCollection.find(query).sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new LinkedList<>(), callback);
}
public static void findById(String id, SingleResultCallback<IpBan> callback) {
@ -52,18 +52,18 @@ public final class IpBan {
}
public static void findByIp(String userIp, SingleResultCallback<List<IpBan>> callback) {
ipBansCollection.find(new Document("userIp", userIp)).into(new ArrayList<>(), callback);
ipBansCollection.find(new Document("userIp", userIp)).into(new LinkedList<>(), callback);
}
public static void findByIpGrouped(Iterable<String> userIps, SingleResultCallback<Map<String, List<IpBan>>> callback) {
ipBansCollection.find(new Document("userIp", new Document("$in", userIps))).into(new ArrayList<>(), (ipBans, error) -> {
ipBansCollection.find(new Document("userIp", new Document("$in", userIps))).into(new LinkedList<>(), (ipBans, error) -> {
if (error != null) {
callback.onResult(null, error);
} else {
Map<String, List<IpBan>> result = new HashMap<>();
for (String userIp : userIps) {
result.put(userIp, new ArrayList<>());
result.put(userIp, new LinkedList<>());
}
for (IpBan ipBan : ipBans) {

View File

@ -43,9 +43,9 @@ public final class Punishment {
@Getter private Instant removedAt;
@Getter private String removalReason;
public static List<Punishment> findByTypeSync(Collection<PunishmentType> types) {
public static void findByType(Collection<PunishmentType> types, SingleResultCallback<List<Punishment>> callback) {
Collection<String> convertedTypes = types.stream().map(PunishmentType::name).collect(Collectors.toList());
return SyncUtils.blockMulti(punishmentsCollection.find(new Document("type", new Document("$in", convertedTypes))));
punishmentsCollection.find(new Document("type", new Document("$in", convertedTypes))).into(new LinkedList<>(), callback);
}
public static Punishment findByIdSync(String id) {
@ -69,8 +69,8 @@ public final class Punishment {
return SyncUtils.blockMulti(punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", convertedTypes))));
}
public static void findAllPaginated(int skip, int pageSize, SingleResultCallback<List<Punishment>> callback) {
punishmentsCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback);
public static void findPaginated(Document query, int skip, int pageSize, SingleResultCallback<List<Punishment>> callback) {
punishmentsCollection.find(query).sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new LinkedList<>(), callback);
}
public static void findById(String id, SingleResultCallback<Punishment> callback) {
@ -86,18 +86,18 @@ public final class Punishment {
}
public static void findByUser(UUID user, SingleResultCallback<List<Punishment>> callback) {
punishmentsCollection.find(new Document("user", user)).into(new ArrayList<>(), callback);
punishmentsCollection.find(new Document("user", user)).into(new LinkedList<>(), callback);
}
public static void findByUserGrouped(Iterable<UUID> users, SingleResultCallback<Map<UUID, List<Punishment>>> callback) {
punishmentsCollection.find(new Document("user", new Document("$in", users))).into(new ArrayList<>(), (punishments, error) -> {
punishmentsCollection.find(new Document("user", new Document("$in", users))).into(new LinkedList<>(), (punishments, error) -> {
if (error != null) {
callback.onResult(null, error);
} else {
Map<UUID, List<Punishment>> result = new HashMap<>();
for (UUID user : users) {
result.put(user, new ArrayList<>());
result.put(user, new LinkedList<>());
}
for (Punishment punishment : punishments) {
@ -115,7 +115,7 @@ public final class Punishment {
public static void findByUserAndType(UUID user, Collection<PunishmentType> types, SingleResultCallback<List<Punishment>> callback) {
Collection<String> convertedTypes = types.stream().map(PunishmentType::name).collect(Collectors.toList());
punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", convertedTypes))).into(new ArrayList<>(), callback);
punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", convertedTypes))).into(new LinkedList<>(), callback);
}
public Punishment() {} // For Jackson

View File

@ -14,7 +14,8 @@ public final class GETAuditLog implements Handler<RoutingContext> {
int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip"));
int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize"));
AuditLogEntry.findPaginated(new Document(), skip, pageSize, (auditLog, error) -> {
// TODO: DO THIS BETTER (SAME FOR PUNISHMENTS, IP BANS, AND GRANTS
AuditLogEntry.findPaginated(ctx.request().getParam("user") == null ? new Document() : new Document("user", ctx.request().getParam("user")), skip, pageSize, (auditLog, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {

View File

@ -5,6 +5,7 @@ import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.util.ErrorUtils;
import org.bson.Document;
public final class GETGrants implements Handler<RoutingContext> {
@ -13,7 +14,7 @@ public final class GETGrants implements Handler<RoutingContext> {
int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip"));
int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize"));
Grant.findAllPaginated(skip, pageSize, (grants, error) -> {
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);
} else {

View File

@ -5,6 +5,7 @@ import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.IpBan;
import net.frozenorb.apiv3.util.ErrorUtils;
import org.bson.Document;
public final class GETIpBans implements Handler<RoutingContext> {
@ -13,7 +14,7 @@ public final class GETIpBans implements Handler<RoutingContext> {
int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip"));
int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize"));
IpBan.findAllPaginated(skip, pageSize, (grants, error) -> {
IpBan.findPaginated(ctx.request().getParam("userIp") == null ? new Document() : new Document("userIp", ctx.request().getParam("userIp")), skip, pageSize, (grants, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {

View File

@ -5,16 +5,16 @@ import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.util.ErrorUtils;
import org.bson.Document;
public final class GETPunishments implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
// USER PARAM
try {
int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip"));
int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize"));
Punishment.findAllPaginated(skip, pageSize, (grants, error) -> {
Punishment.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);
} else {