diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 0e19f09..83c5c2b 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -1,36 +1,20 @@ package net.frozenorb.apiv3; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; -import com.mongodb.async.client.MongoClient; -import com.mongodb.async.client.MongoClientSettings; -import com.mongodb.async.client.MongoClients; import com.mongodb.async.client.MongoDatabase; -import com.mongodb.connection.ClusterConnectionMode; -import com.mongodb.connection.ClusterSettings; import io.vertx.core.AbstractVerticle; -import io.vertx.core.json.JsonArray; import io.vertx.ext.web.Router; import lombok.Getter; -import net.frozenorb.apiv3.collections.AuditLog; -import net.frozenorb.apiv3.collections.Grant; -import net.frozenorb.apiv3.collections.Server; -import net.frozenorb.apiv3.mongoCodec.InstantCodec; -import net.frozenorb.apiv3.utils.ErrorUtils; -import org.bson.BsonType; +import net.frozenorb.apiv3.accessor.AuditLog; +import net.frozenorb.apiv3.accessor.Grants; +import net.frozenorb.apiv3.accessor.Servers; +import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.JsonUtils; +import net.frozenorb.apiv3.util.MongoUtils; import org.bson.Document; -import org.bson.codecs.BsonTypeClassMap; -import org.bson.codecs.BsonValueCodecProvider; -import org.bson.codecs.DocumentCodecProvider; -import org.bson.codecs.ValueCodecProvider; -import org.bson.codecs.configuration.CodecRegistries; -import org.bson.codecs.configuration.CodecRegistry; -import java.time.Instant; import java.util.UUID; -import java.util.stream.Collectors; public final class APIv3 extends AbstractVerticle { @@ -38,27 +22,7 @@ public final class APIv3 extends AbstractVerticle { public void start() { Router coreHttpRouter = Router.router(vertx); - BsonTypeClassMap codecMap = new BsonTypeClassMap(ImmutableMap.of( - BsonType.DATE_TIME, Instant.class - )); - CodecRegistry codecRegistry = CodecRegistries.fromRegistries( - CodecRegistries.fromCodecs(new InstantCodec()), - CodecRegistries.fromProviders(new DocumentCodecProvider(codecMap)), - CodecRegistries.fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider()) - ); - MongoClientSettings settings = - MongoClientSettings.builder() - .clusterSettings( - ClusterSettings.builder() - .mode(ClusterConnectionMode.SINGLE) - .hosts(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505))) - .build() - ) - .credentialList(ImmutableList.of(MongoCredential.createCredential("test", "minehqapi", "test".toCharArray()))) - .codecRegistry(codecRegistry) - .build(); - MongoClient mongoClient = MongoClients.create(settings); - mongo = mongoClient.getDatabase("minehqapi"); + mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray()); // We always reply in JSON. coreHttpRouter.route("/*").handler(ctx -> { @@ -67,25 +31,25 @@ public final class APIv3 extends AbstractVerticle { }); coreHttpRouter.get("/servers").handler(ctx -> { - Server.findAll(res -> { - if (res.succeeded()) { - JsonArray response = new JsonArray(); - res.result().forEach(server -> response.add(server.toLiteJson())); - - ctx.response().end(response.encode()); + Servers.findAll((servers, error) -> { + if (error != null) { + ctx.response().end(ErrorUtils.createResponse(error).toJson()); } else { - ctx.response().end(ErrorUtils.createResponse(res.cause()).toJson()); + ctx.response().end(JsonUtils.toLiteJsonString(servers)); } }); }); coreHttpRouter.get("/server/:server").handler(ctx -> { - Server.findById(ctx.request().getParam("server"), res -> { - if (res.succeeded()) { - res.result().update(); - ctx.response().end(res.result().toFullJson().toJson()); + String serverId = ctx.request().getParam("server"); + + Servers.findById(serverId, (server, error) -> { + if (error != null) { + ctx.response().end(ErrorUtils.createResponse(error).toJson()); + } else if (server != null) { + ctx.response().end(JsonUtils.toLiteJsonString(server)); } else { - ctx.response().end(ErrorUtils.createResponse(res.cause()).toJson()); + ctx.response().end(ErrorUtils.createResponse("Server '" + serverId + "' not found.").toJson()); } }); }); @@ -93,39 +57,30 @@ public final class APIv3 extends AbstractVerticle { coreHttpRouter.get("/user/:user/grants").handler(ctx -> { UUID target = UUID.fromString(ctx.request().getParam("user")); - Grant.findByTarget(target, res -> { - if (res.succeeded()) { - JsonArray response = new JsonArray(); - res.result().forEach(grant -> response.add(grant.toLiteJson())); - - ctx.response().end(response.encode()); + Grants.findByTarget(target, (grants, error) -> { + if (error != null) { + ctx.response().end(ErrorUtils.createResponse(error).toJson()); } else { - ctx.response().end(ErrorUtils.createResponse(res.cause()).toJson()); + ctx.response().end(JsonUtils.toLiteJsonString(grants)); } }); }); coreHttpRouter.get("/auditLog").handler(ctx -> { - AuditLog.findAll((auditLog, error) -> { + AuditLog.findAll((auditLogEntries, error) -> { if (error != null) { - auditLog.stream().map(AuditLog::toLiteJson).collect(Collectors.toList()); - JsonArray response = new JsonArray(); - - auditLog.result().forEach(server -> { - response.add(server.toLiteJson()); - }); - - ctx.response().end(response.encode()); - } else { ctx.response().end(ErrorUtils.createResponse(error).toJson()); + } else { + ctx.response().end(JsonUtils.toLiteJsonString(auditLogEntries)); } }); }); coreHttpRouter.get("/doAction/:actionType").handler(ctx -> { String actionType = ctx.request().getParam("actionType"); - AuditLog.log(UUID.randomUUID(), "192.168.1.1", actionType, new Document()); - ctx.response().end("its done"); + + AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document()); + ctx.response().end("{'logged': true}"); }); vertx.createHttpServer().requestHandler(coreHttpRouter::accept).listen(8080); diff --git a/src/main/java/net/frozenorb/apiv3/LiteFullJson.java b/src/main/java/net/frozenorb/apiv3/LiteFullJson.java index 5c5422d..d1c11be 100644 --- a/src/main/java/net/frozenorb/apiv3/LiteFullJson.java +++ b/src/main/java/net/frozenorb/apiv3/LiteFullJson.java @@ -2,8 +2,6 @@ package net.frozenorb.apiv3; import org.bson.Document; -import java.util.Collection; - public interface LiteFullJson { Document toLiteJson(); @@ -11,16 +9,4 @@ public interface LiteFullJson { return toLiteJson(); } - static String toLiteJson(Object value) { - if (value instanceof Collection) { - - } else { - - } - } - - static String toFullJson(Object value) { - - } - } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java b/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java new file mode 100644 index 0000000..673ae0a --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java @@ -0,0 +1,51 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.AuditLogEntry; +import net.frozenorb.apiv3.util.IPUtils; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; +import org.bson.types.ObjectId; + +import java.util.Collection; +import java.util.Date; +import java.util.UUID; + +@UtilityClass +public class AuditLog { + + public static final String COLLECTION_NAME = "auditLog"; + private static final Document TIME_BASED_SORT = new Document("performedAt", -1); + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, AuditLogEntry::new, callback); + } + + public static void findByPerformer(UUID performer, SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), TIME_BASED_SORT, AuditLogEntry::new, callback); + } + + public static void findByPerformerIp(long machineIp, SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", machineIp), TIME_BASED_SORT, AuditLogEntry::new, callback); + } + + public static void findByActionType(String actionType, SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback); + } + + public static void log(UUID user, String userIp, String actionType, Document data) { + Document insert = new Document(); + + insert.put("_id", new ObjectId().toString()); + insert.put("performedBy", user.toString()); + insert.put("performedAt", new Date()); + insert.put("performedFrom", IPUtils.humanToMachine(userIp)); + insert.put("actionType", actionType); + insert.put("actionData", data); + + APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, (result, error) -> {}); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/EmailTemplates.java b/src/main/java/net/frozenorb/apiv3/accessor/EmailTemplates.java new file mode 100644 index 0000000..da2a42b --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/EmailTemplates.java @@ -0,0 +1,24 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.EmailTemplate; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class EmailTemplates { + + public static final String COLLECTION_NAME = "emailTemplate"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), EmailTemplate::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), EmailTemplate::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Grants.java b/src/main/java/net/frozenorb/apiv3/accessor/Grants.java new file mode 100644 index 0000000..974dc50 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/Grants.java @@ -0,0 +1,34 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.Grant; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; +import java.util.UUID; + +@UtilityClass +public class Grants { + + public static final String COLLECTION_NAME = "grant"; + private static final Document TIME_BASED_SORT = new Document("addedAt", -1); + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, Grant::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), TIME_BASED_SORT, Grant::new, callback); + } + + public static void findByTarget(UUID target, SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), TIME_BASED_SORT, Grant::new, callback); + } + + public static void findByAddedBy(UUID addedBy, SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/IPBans.java b/src/main/java/net/frozenorb/apiv3/accessor/IPBans.java new file mode 100644 index 0000000..dcfaa82 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/IPBans.java @@ -0,0 +1,24 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.IPBan; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class IPBans { + + public static final String COLLECTION_NAME = "ipBan"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPBan::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPBan::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java b/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java new file mode 100644 index 0000000..223ce8d --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java @@ -0,0 +1,25 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.IPLogEntry; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class IPLog { + + public static final String COLLECTION_NAME = "ipLog"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPLogEntry::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPLogEntry::new, callback); + } + + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/MaxMindCache.java b/src/main/java/net/frozenorb/apiv3/accessor/MaxMindCache.java new file mode 100644 index 0000000..b71d927 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/MaxMindCache.java @@ -0,0 +1,25 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.MaxMindCacheEntry; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class MaxMindCache { + + public static final String COLLECTION_NAME = "maxMindCache"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), MaxMindCacheEntry::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), MaxMindCacheEntry::new, callback); + } + + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java b/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java new file mode 100644 index 0000000..725ef8c --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java @@ -0,0 +1,24 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.NotificationLogEntry; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class NotificationLog { + + public static final String COLLECTION_NAME = "notificationLog"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationLogEntry::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationLogEntry::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java b/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java new file mode 100644 index 0000000..e6bb35d --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java @@ -0,0 +1,24 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.Punishment; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class Punishments { + + public static final String COLLECTION_NAME = "punishment"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Punishment::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/ServerGroups.java b/src/main/java/net/frozenorb/apiv3/accessor/ServerGroups.java new file mode 100644 index 0000000..ea67ec3 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/ServerGroups.java @@ -0,0 +1,24 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.ServerGroup; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class ServerGroups { + + public static final String COLLECTION_NAME = "serverGroup"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), ServerGroup::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), ServerGroup::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Servers.java b/src/main/java/net/frozenorb/apiv3/accessor/Servers.java new file mode 100644 index 0000000..71e8b9f --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/Servers.java @@ -0,0 +1,28 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.Server; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class Servers { + + public static final String COLLECTION_NAME = "server"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Server::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Server::new, callback); + } + + public static void findByGroup(String groupId, SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document("group", groupId), Server::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Users.java b/src/main/java/net/frozenorb/apiv3/accessor/Users.java new file mode 100644 index 0000000..5f7827a --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/accessor/Users.java @@ -0,0 +1,24 @@ +package net.frozenorb.apiv3.accessor; + +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.model.User; +import net.frozenorb.apiv3.util.MongoUtils; +import org.bson.Document; + +import java.util.Collection; + +@UtilityClass +public class Users { + + public static final String COLLECTION_NAME = "user"; + + public static void findAll(SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), User::new, callback); + } + + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), User::new, callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/AuditLog.java b/src/main/java/net/frozenorb/apiv3/collections/AuditLog.java deleted file mode 100644 index d64fa4d..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/AuditLog.java +++ /dev/null @@ -1,91 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import com.mongodb.async.SingleResultCallback; -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.IPUtils; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; -import org.bson.types.ObjectId; - -import java.time.Instant; -import java.util.Collection; -import java.util.Date; -import java.util.UUID; - -@ToString -public final class AuditLog implements LiteFullJson { - - private static final String COLLECTION_NAME = "auditLog"; - private static final Document TIME_BASED_SORT = new Document("performedAt", -1); - - @Getter private String id; - @Getter private UUID performedBy; - @Getter private Instant performedAt; - @Getter private long performedFrom; - @Getter private String actionType; - @Getter private Document actionData; - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, AuditLog::new, callback); - } - - public static void findByPerformer(UUID performer, Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), AuditLog::new, callback); - } - - public static void findByPerformerIp(long machineIp, Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", machineIp), AuditLog::new, callback); - } - - public static void findByActionType(String actionType, Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), AuditLog::new, callback); - } - - public static void log(UUID user, String userIp, String actionType, Document data) { - Document insert = new Document(); - - insert.put("_id", new ObjectId().toString()); - insert.put("performedBy", user.toString()); - insert.put("performedAt", new Date()); - insert.put("performedFrom", IPUtils.humanToMachine(userIp)); - insert.put("actionType", actionType); - insert.put("actionData", data); - - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, (result, error) -> {}); - } - - private AuditLog(Document json) { - this.id = json.getString("_id"); - this.performedBy = UUID.fromString(json.getString("performedBy")); - this.performedAt = json.get("performedAt", Instant.class); - this.performedFrom = json.getLong("performedFrom"); - this.actionType = json.getString("actionType"); - this.actionData = json.get("actionData", Document.class); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id); - json.put("performedBy", performedBy.toString()); - json.put("performedAt", performedAt.toString()); - json.put("performedFrom", IPUtils.machineToHuman(performedFrom)); - json.put("actionType", actionType); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - json.put("actionData", actionData); - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/IPBan.java b/src/main/java/net/frozenorb/apiv3/collections/IPBan.java deleted file mode 100644 index 8584c49..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/IPBan.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@ToString -public final class IPBan implements LiteFullJson { - - private static final String COLLECTION_NAME = "ipBan"; - - @Getter private String id; - - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPBan::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPBan::new, callback); - } - - private IPBan(Document json) { - this.id = json.getString("_id"); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/IPLog.java b/src/main/java/net/frozenorb/apiv3/collections/IPLog.java deleted file mode 100644 index d84c397..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/IPLog.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@ToString -public final class IPLog implements LiteFullJson { - - private static final String COLLECTION_NAME = "ipLog"; - - @Getter private String id; - - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPLog::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPLog::new, callback); - } - - private IPLog(Document json) { - this.id = json.getString("_id"); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/MaxMindCache.java b/src/main/java/net/frozenorb/apiv3/collections/MaxMindCache.java deleted file mode 100644 index 1443088..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/MaxMindCache.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@ToString -public final class MaxMindCache implements LiteFullJson { - - private static final String COLLECTION_NAME = "maxMindCache"; - - @Getter private String id; - - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), MaxMindCache::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), MaxMindCache::new, callback); - } - - private MaxMindCache(Document json) { - this.id = json.getString("_id"); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/NotificationLog.java b/src/main/java/net/frozenorb/apiv3/collections/NotificationLog.java deleted file mode 100644 index d10ce3b..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/NotificationLog.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@ToString -public final class NotificationLog implements LiteFullJson { - - private static final String COLLECTION_NAME = "notificationLog"; - - @Getter private String id; - - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationLog::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationLog::new, callback); - } - - private NotificationLog(Document json) { - this.id = json.getString("_id"); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/Punishment.java b/src/main/java/net/frozenorb/apiv3/collections/Punishment.java deleted file mode 100644 index e397f0d..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/Punishment.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@ToString -public final class Punishment implements LiteFullJson { - - private static final String COLLECTION_NAME = "punishment"; - - @Getter private String id; - - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Punishment::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback); - } - - private Punishment(Document json) { - this.id = json.getString("_id"); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/ServerGroup.java b/src/main/java/net/frozenorb/apiv3/collections/ServerGroup.java deleted file mode 100644 index f7ff4d5..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/ServerGroup.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@ToString -public final class ServerGroup implements LiteFullJson { - - private static final String COLLECTION_NAME = "serverGroup"; - - @Getter private String id; - - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), ServerGroup::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), ServerGroup::new, callback); - } - - private ServerGroup(Document json) { - this.id = json.getString("_id"); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/User.java b/src/main/java/net/frozenorb/apiv3/collections/User.java deleted file mode 100644 index 765fde4..0000000 --- a/src/main/java/net/frozenorb/apiv3/collections/User.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.frozenorb.apiv3.collections; - -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@ToString -public final class User implements LiteFullJson { - - private static final String COLLECTION_NAME = "user"; - - @Getter private String id; - - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), User::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), User::new, callback); - } - - private User(Document json) { - this.id = json.getString("_id"); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java new file mode 100644 index 0000000..d742065 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java @@ -0,0 +1,51 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.util.IPUtils; +import org.bson.Document; + +import java.time.Instant; +import java.util.UUID; + +@ToString +public final class AuditLogEntry implements LiteFullJson { + + @Getter private String id; + @Getter private UUID performedBy; + @Getter private Instant performedAt; + @Getter private long performedFrom; + @Getter private String actionType; + @Getter private Document actionData; + + public AuditLogEntry(Document json) { + this.id = json.getString("_id"); + this.performedBy = UUID.fromString(json.getString("performedBy")); + this.performedAt = (Instant) json.get("performedAt"); + this.performedFrom = json.getLong("performedFrom"); + this.actionType = json.getString("actionType"); + this.actionData = (Document) json.get("actionData"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("id", id); + json.put("performedBy", performedBy.toString()); + json.put("performedAt", performedAt.toString()); + json.put("performedFrom", IPUtils.machineToHuman(performedFrom)); + json.put("actionType", actionType); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + json.put("actionData", actionData); + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/EmailTemplate.java b/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java similarity index 63% rename from src/main/java/net/frozenorb/apiv3/collections/EmailTemplate.java rename to src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java index 3171606..64c6739 100644 --- a/src/main/java/net/frozenorb/apiv3/collections/EmailTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java @@ -1,34 +1,20 @@ -package net.frozenorb.apiv3.collections; +package net.frozenorb.apiv3.model; -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; import lombok.Getter; import lombok.ToString; import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; import org.bson.Document; -import java.util.Collection; import java.util.Map; @ToString public final class EmailTemplate implements LiteFullJson { - private static final String COLLECTION_NAME = "emailTemplate"; - @Getter private String id; @Getter private String title; @Getter private String body; - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), EmailTemplate::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), EmailTemplate::new, callback); - } - - private EmailTemplate(Document json) { + public EmailTemplate(Document json) { this.id = json.getString("_id"); this.title = json.getString("title"); this.body = json.getString("body"); diff --git a/src/main/java/net/frozenorb/apiv3/collections/Grant.java b/src/main/java/net/frozenorb/apiv3/model/Grant.java similarity index 64% rename from src/main/java/net/frozenorb/apiv3/collections/Grant.java rename to src/main/java/net/frozenorb/apiv3/model/Grant.java index a33e404..e466b02 100644 --- a/src/main/java/net/frozenorb/apiv3/collections/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -1,12 +1,9 @@ -package net.frozenorb.apiv3.collections; +package net.frozenorb.apiv3.model; import com.google.common.collect.ImmutableSet; -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; import lombok.Getter; import lombok.ToString; import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; import org.bson.Document; import java.time.Instant; @@ -17,8 +14,6 @@ import java.util.UUID; @ToString public final class Grant implements LiteFullJson { - private static final String COLLECTION_NAME = "grant"; - @Getter private String id; @Getter private UUID target; @Getter private String reason; @@ -31,23 +26,7 @@ public final class Grant implements LiteFullJson { @Getter private Instant removedAt; @Getter private String removalReason; - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Grant::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Grant::new, callback); - } - - public static void findByTarget(UUID target, Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), Grant::new, callback); - } - - public static void findByAddedBy(UUID addedBy, Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), Grant::new, callback); - } - - private Grant(Document json) { + public Grant(Document json) { this.id = json.getString("_id"); this.target = UUID.fromString(json.getString("target")); this.reason = json.getString("reason"); diff --git a/src/main/java/net/frozenorb/apiv3/model/IPBan.java b/src/main/java/net/frozenorb/apiv3/model/IPBan.java new file mode 100644 index 0000000..f289f2c --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/IPBan.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +@ToString +public final class IPBan implements LiteFullJson { + + @Getter private String id; + + public IPBan(Document json) { + this.id = json.getString("_id"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("_id", id); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java new file mode 100644 index 0000000..41fb149 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +@ToString +public final class IPLogEntry implements LiteFullJson { + + @Getter private String id; + + public IPLogEntry(Document json) { + this.id = json.getString("_id"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("_id", id); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java b/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java new file mode 100644 index 0000000..5be4fd5 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +@ToString +public final class MaxMindCacheEntry implements LiteFullJson { + + @Getter private String id; + + public MaxMindCacheEntry(Document json) { + this.id = json.getString("_id"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("_id", id); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java new file mode 100644 index 0000000..eb5d414 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +@ToString +public final class NotificationLogEntry implements LiteFullJson { + + @Getter private String id; + + public NotificationLogEntry(Document json) { + this.id = json.getString("_id"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("_id", id); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java new file mode 100644 index 0000000..f686b50 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +@ToString +public final class Punishment implements LiteFullJson { + + @Getter private String id; + + public Punishment(Document json) { + this.id = json.getString("_id"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("_id", id); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/collections/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java similarity index 55% rename from src/main/java/net/frozenorb/apiv3/collections/Server.java rename to src/main/java/net/frozenorb/apiv3/model/Server.java index 3d2e1d3..b1d4c05 100644 --- a/src/main/java/net/frozenorb/apiv3/collections/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -1,23 +1,19 @@ -package net.frozenorb.apiv3.collections; +package net.frozenorb.apiv3.model; -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; import lombok.Getter; import lombok.ToString; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.LiteFullJson; -import net.frozenorb.apiv3.utils.MongoUtils; +import net.frozenorb.apiv3.accessor.Servers; import org.bson.Document; import java.time.Instant; import java.util.*; import java.util.stream.Collectors; -@ToString(exclude={ "secret" }) +@ToString public final class Server implements LiteFullJson { - private static final String COLLECTION_NAME = "server"; - @Getter private String id; @Getter private String bungeeId; @Getter private String displayName; @@ -28,42 +24,30 @@ public final class Server implements LiteFullJson { @Getter private double lastTps; @Getter private List players; - public static void findAll(Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Server::new, callback); - } - - public static void findById(String id, Handler> callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Server::new, callback); - } - - public static void findByGroup(String groupId, Handler>> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("group", groupId), Server::new, callback); - } - - private Server(Document json) { + public Server(Document json) { this.id = json.getString("_id"); this.bungeeId = json.getString("bungeeId"); this.displayName = json.getString("displayName"); this.secret = json.getString("secret"); this.group = json.getString("group"); this.internalIp = json.getString("internalIp"); - this.lastUpdate = json.get("lastUpdate", Instant.class); - this.lastTps = json.get("lastTps", Number.class).doubleValue(); + this.lastUpdate = (Instant) json.get("lastUpdate"); + this.lastTps = ((Number) json.get("lastTps")).doubleValue(); this.players = new ArrayList<>(); - for (Object uuidString : json.get("players", Collection.class)) { + for (Object uuidString : (Collection) json.get("players")) { players.add(UUID.fromString((String) uuidString)); } } public void update() { - APIv3.getMongo().getCollection(COLLECTION_NAME).updateOne(new Document("_id", id), new Document("$set", new Document("lastUpdate", new Date())), (result, error) -> {}); + APIv3.getMongo().getCollection(Servers.COLLECTION_NAME).updateOne(new Document("_id", id), new Document("$set", new Document("lastUpdate", new Date())), (result, error) -> {}); } public Document toLiteJson() { Document json = new Document(); - json.put("_id", id); + json.put("id", id); json.put("bungeeId", bungeeId); json.put("displayName", displayName); json.put("group", group); diff --git a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java new file mode 100644 index 0000000..e413d8c --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +@ToString +public final class ServerGroup implements LiteFullJson { + + @Getter private String id; + + public ServerGroup(Document json) { + this.id = json.getString("_id"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("_id", id); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java new file mode 100644 index 0000000..fce46dc --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import lombok.ToString; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +@ToString +public final class User implements LiteFullJson { + + @Getter private String id; + + public User(Document json) { + this.id = json.getString("_id"); + } + + public Document toLiteJson() { + Document json = new Document(); + + json.put("_id", id); + + return json; + } + + public Document toFullJson() { + Document json = toLiteJson(); + + + + return json; + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/utils/ErrorUtils.java b/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java similarity index 79% rename from src/main/java/net/frozenorb/apiv3/utils/ErrorUtils.java rename to src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java index 0b08d8d..8d3ccf7 100644 --- a/src/main/java/net/frozenorb/apiv3/utils/ErrorUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java @@ -1,4 +1,4 @@ -package net.frozenorb.apiv3.utils; +package net.frozenorb.apiv3.util; import lombok.experimental.UtilityClass; import org.bson.Document; @@ -18,7 +18,11 @@ public class ErrorUtils { return createResponse(throwable.getClass().getSimpleName(), identifier); } - public static Document createResponse(String reason, String identifier) { + public static Document createResponse(String reason) { + return createResponse(reason, null); + } + + private static Document createResponse(String reason, String identifier) { Document json = new Document(); json.put("failed", true); diff --git a/src/main/java/net/frozenorb/apiv3/utils/IPUtils.java b/src/main/java/net/frozenorb/apiv3/util/IPUtils.java similarity index 95% rename from src/main/java/net/frozenorb/apiv3/utils/IPUtils.java rename to src/main/java/net/frozenorb/apiv3/util/IPUtils.java index 258a2a6..15a8c9a 100644 --- a/src/main/java/net/frozenorb/apiv3/utils/IPUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/IPUtils.java @@ -1,4 +1,4 @@ -package net.frozenorb.apiv3.utils; +package net.frozenorb.apiv3.util; import lombok.experimental.UtilityClass; diff --git a/src/main/java/net/frozenorb/apiv3/util/JsonUtils.java b/src/main/java/net/frozenorb/apiv3/util/JsonUtils.java new file mode 100644 index 0000000..ed6e00e --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/util/JsonUtils.java @@ -0,0 +1,40 @@ +package net.frozenorb.apiv3.util; + +import io.vertx.core.json.JsonArray; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.LiteFullJson; + +import java.util.Collection; + +@UtilityClass +public class JsonUtils { + + public static String toLiteJsonString(Collection encode) { + JsonArray result = new JsonArray(); + + for (LiteFullJson entry : encode) { + result.add(entry.toLiteJson()); + } + + return result.encode(); + } + + public static String toLiteJsonString(LiteFullJson encode) { + return encode.toLiteJson().toJson(); + } + + public static String toFullJsonString(Collection encode) { + JsonArray result = new JsonArray(); + + for (LiteFullJson entry : encode) { + result.add(entry.toFullJson()); + } + + return result.encode(); + } + + public static String toFullJsonString(LiteFullJson encode) { + return encode.toFullJson().toJson(); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java b/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java new file mode 100644 index 0000000..0aa9ccd --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java @@ -0,0 +1,98 @@ +package net.frozenorb.apiv3.util; + +import com.google.common.base.Function; +import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; +import com.mongodb.async.SingleResultCallback; +import com.mongodb.async.client.FindIterable; +import com.mongodb.async.client.MongoClientSettings; +import com.mongodb.async.client.MongoClients; +import com.mongodb.async.client.MongoDatabase; +import com.mongodb.connection.ClusterConnectionMode; +import com.mongodb.connection.ClusterSettings; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.mongoCodec.InstantCodec; +import org.bson.BsonType; +import org.bson.Document; +import org.bson.codecs.BsonTypeClassMap; +import org.bson.codecs.BsonValueCodecProvider; +import org.bson.codecs.DocumentCodecProvider; +import org.bson.codecs.ValueCodecProvider; +import org.bson.codecs.configuration.CodecRegistries; +import org.bson.codecs.configuration.CodecRegistry; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@UtilityClass +public class MongoUtils { + + public static MongoDatabase initializeConnection(List hosts, String databaseName, String databaseUsername, char[] databasePassword) { + BsonTypeClassMap codecMap = new BsonTypeClassMap(ImmutableMap.of( + BsonType.DATE_TIME, Instant.class + )); + CodecRegistry codecRegistry = CodecRegistries.fromRegistries( + CodecRegistries.fromCodecs(new InstantCodec()), + CodecRegistries.fromProviders(new DocumentCodecProvider(codecMap)), + CodecRegistries.fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider()) + ); + MongoClientSettings settings = + MongoClientSettings.builder() + .clusterSettings( + ClusterSettings.builder() + .mode(hosts.size() == 1 ? ClusterConnectionMode.SINGLE : ClusterConnectionMode.MULTIPLE) + .hosts(hosts) + .build() + ) + .credentialList(ImmutableList.of(MongoCredential.createCredential(databaseUsername, databaseName, databasePassword))) + .codecRegistry(codecRegistry) + .build(); + + return MongoClients.create(settings).getDatabase(databaseName); + } + + private static FindIterable createFindIterable(String collection, Document query, Document sort) { + return APIv3.getMongo().getCollection(collection).find(query).sort(sort); + } + + public static void findOneAndTransform(String collection, Document query, Function transformation, SingleResultCallback callback) { + findOneAndTransform(collection, query, new Document(), transformation, callback); + } + + public static void findOneAndTransform(String collection, Document query, Document sort, Function transformation, SingleResultCallback callback) { + createFindIterable(collection, query, sort).first((result, error) -> { + if (error != null) { + callback.onResult(null, error); + } else if (result != null) { + T transformed = transformation.apply(result); + callback.onResult(transformed, null); + } else { + callback.onResult(null, null); + } + }); + } + + public static void findAndTransform(String collection, Document query, Function transformation, SingleResultCallback> callback) { + findAndTransform(collection, query, new Document(), transformation, callback); + } + + public static void findAndTransform(String collection, Document query, Document sort, Function transformation, SingleResultCallback> callback) { + createFindIterable(collection, query, sort).into(new ArrayList<>(), (result, error) -> { + if (error != null) { + callback.onResult(null, error); + } else if (!result.isEmpty()) { + Collection transformed = Collections2.transform(result, transformation); + callback.onResult(transformed, null); + } else { + callback.onResult(ImmutableList.of(), null); + } + }); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/utils/MongoUtils.java b/src/main/java/net/frozenorb/apiv3/utils/MongoUtils.java deleted file mode 100644 index 1c970b7..0000000 --- a/src/main/java/net/frozenorb/apiv3/utils/MongoUtils.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.frozenorb.apiv3.utils; - -import com.google.common.base.Function; -import com.google.common.collect.Collections2; -import com.mongodb.async.client.FindIterable; -import io.vertx.core.AsyncResult; -import io.vertx.core.Future; -import io.vertx.core.Handler; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import org.bson.Document; - -import java.util.ArrayList; -import java.util.Collection; - -@UtilityClass -public class MongoUtils { - - private static FindIterable createFindIterable(String collection, Document query, Document sort) { - return APIv3.getMongo().getCollection(collection).find(query).sort(sort); - } - - public static void findOneAndTransform(String collection, Document query, Function transformation, Handler> callback) { - findOneAndTransform(collection, query, new Document(), transformation, callback); - } - - public static void findOneAndTransform(String collection, Document query, Document sort, Function transformation, Handler> callback) { - createFindIterable(collection, query, sort).first((result, error) -> { - if (error != null) { - callback.handle(Future.failedFuture(error)); - } else { - T transformed = transformation.apply(result); - callback.handle(Future.succeededFuture(transformed)); - } - }); - } - - public static void findAndTransform(String collection, Document query, Function transformation, Handler>> callback) { - findAndTransform(collection, query, new Document(), transformation, callback); - } - - public static void findAndTransform(String collection, Document query, Document sort, Function transformation, Handler>> callback) { - createFindIterable(collection, query, sort).into(new ArrayList<>(), (result, error) -> { - if (error != null) { - callback.handle(Future.failedFuture(error)); - } else { - Collection transformed = Collections2.transform(result, transformation); - callback.handle(Future.succeededFuture(transformed)); - } - }); - } - -} \ No newline at end of file