diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 83c5c2b..e5b5801 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -9,6 +9,7 @@ import lombok.Getter; import net.frozenorb.apiv3.accessor.AuditLog; import net.frozenorb.apiv3.accessor.Grants; import net.frozenorb.apiv3.accessor.Servers; +import net.frozenorb.apiv3.accessor.Users; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.JsonUtils; import net.frozenorb.apiv3.util.MongoUtils; @@ -54,6 +55,20 @@ public final class APIv3 extends AbstractVerticle { }); }); + coreHttpRouter.get("/user/:user").handler(ctx -> { + UUID target = UUID.fromString(ctx.request().getParam("user")); + + Users.findById(target.toString(), (user, error) -> { + if (error != null) { + ctx.response().end(ErrorUtils.createResponse(error).toJson()); + } else if (user != null) { + ctx.response().end(JsonUtils.toLiteJsonString(user)); + } else { + ctx.response().end(ErrorUtils.createResponse("User '" + target + "' not found.").toJson()); + } + }); + }); + coreHttpRouter.get("/user/:user/grants").handler(ctx -> { UUID target = UUID.fromString(ctx.request().getParam("user")); diff --git a/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java b/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java index 673ae0a..16f1246 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java @@ -12,6 +12,7 @@ import org.bson.types.ObjectId; import java.util.Collection; import java.util.Date; import java.util.UUID; +import java.util.concurrent.Callable; @UtilityClass public class AuditLog { @@ -35,7 +36,7 @@ public class AuditLog { 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) { + public static void log(UUID user, String userIp, String actionType, Document data, SingleResultCallback callback) { Document insert = new Document(); insert.put("_id", new ObjectId().toString()); @@ -45,7 +46,7 @@ public class AuditLog { insert.put("actionType", actionType); insert.put("actionData", data); - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, (result, error) -> {}); + APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback); } } \ 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 index da2a42b..d1c0a28 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/EmailTemplates.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/EmailTemplates.java @@ -2,11 +2,17 @@ package net.frozenorb.apiv3.accessor; import com.mongodb.async.SingleResultCallback; import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.EmailTemplate; +import net.frozenorb.apiv3.util.IPUtils; import net.frozenorb.apiv3.util.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; @UtilityClass public class EmailTemplates { @@ -21,4 +27,16 @@ public class EmailTemplates { MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), EmailTemplate::new, callback); } + public static void createTemplate(String id, String title, String body, UUID creator, SingleResultCallback callback) { + Document insert = new Document(); + + insert.put("_id", id); + insert.put("title", title); + insert.put("body", body); + insert.put("lastUpdatedAt", Instant.now()); + insert.put("lastUpdatedBy", creator.toString()); + + APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, 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 deleted file mode 100644 index b71d927..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/MaxMindCache.java +++ /dev/null @@ -1,25 +0,0 @@ -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/model/MaxMindCacheEntry.java b/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java deleted file mode 100644 index 79f5f96..0000000 --- a/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java +++ /dev/null @@ -1,37 +0,0 @@ -package net.frozenorb.apiv3.model; - -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.MaxMindCache; -import org.bson.Document; - -@ToString -public final class MaxMindCacheEntry extends BaseModel { - - @Getter private String id; - - public MaxMindCacheEntry(Document json) { - super(MaxMindCache.COLLECTION_NAME); - - this.id = json.getString("_id"); - - setId(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/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index 4857a33..768b563 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -20,7 +20,7 @@ public final class Server extends BaseModel { @Getter private String displayName; @Getter private String secret; @Getter private String group; - @Getter private String internalIp; + @Getter private String ip; @Getter private Instant lastUpdate; @Getter private double lastTps; @Getter private List players; @@ -33,7 +33,7 @@ public final class Server extends BaseModel { this.displayName = json.getString("displayName"); this.secret = json.getString("secret"); this.group = json.getString("group"); - this.internalIp = json.getString("internalIp"); + this.ip = json.getString("ip"); this.lastUpdate = (Instant) json.get("lastUpdate"); this.lastTps = ((Number) json.get("lastTps")).doubleValue(); this.players = new ArrayList<>(); @@ -52,7 +52,7 @@ public final class Server extends BaseModel { json.put("bungeeId", bungeeId); json.put("displayName", displayName); json.put("group", group); - json.put("internalIp", internalIp); + json.put("ip", ip); return json; } diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 91676cc..8f15603 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -5,23 +5,53 @@ import lombok.ToString; import net.frozenorb.apiv3.accessor.Users; import org.bson.Document; +import java.time.Instant; +import java.util.Map; +import java.util.UUID; + @ToString public final class User extends BaseModel { - @Getter private String id; + @Getter private UUID id; + @Getter private String lastName; + @Getter private Map aliases; + @Getter private String otpCode; + @Getter private String password; + @Getter private String passwordSalt; + @Getter private String email; + @Getter private int phoneNumber; + @Getter private String lastSeenOn; + @Getter private Instant lastSeenAt; + @Getter private Instant firstSeen; public User(Document json) { super(Users.COLLECTION_NAME); - this.id = json.getString("_id"); + this.id = UUID.fromString(json.getString("_id")); + this.lastName = json.getString("lastName"); + this.aliases = (Map) json.get("aliases"); + this.otpCode = json.getString("otpCode"); + this.password = json.getString("password"); + this.passwordSalt = json.getString("passwordSalt"); + this.email = json.getString("email"); + this.phoneNumber = json.getInteger("phoneNumber"); + this.lastSeenOn = json.getString("lastSeenOn"); + this.lastSeenAt = (Instant) json.get("lastSeenAt"); + this.firstSeen = (Instant) json.get("firstSeen"); - setId(id); + setId(id.toString()); } public Document toLiteJson() { Document json = new Document(); - json.put("_id", id); + json.put("id", id.toString()); + json.put("lastName", lastName); + json.put("email", email); + json.put("phoneNumber", phoneNumber); + json.put("lastSeenOn", lastSeenOn); + json.put("lastSeenAt", lastSeenAt.toString()); + json.put("firstSeen", firstSeen.toString()); return json; } @@ -29,7 +59,7 @@ public final class User extends BaseModel { public Document toFullJson() { Document json = toLiteJson(); - + json.put("aliases", aliases); return json; } diff --git a/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java b/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java index 0aa9ccd..bf8b5b4 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java @@ -70,8 +70,12 @@ public class MongoUtils { if (error != null) { callback.onResult(null, error); } else if (result != null) { - T transformed = transformation.apply(result); - callback.onResult(transformed, null); + try { + T transformed = transformation.apply(result); + callback.onResult(transformed, null); + } catch (Exception ex) { + callback.onResult(null, ex); + } } else { callback.onResult(null, null); } @@ -87,8 +91,12 @@ public class MongoUtils { if (error != null) { callback.onResult(null, error); } else if (!result.isEmpty()) { - Collection transformed = Collections2.transform(result, transformation); - callback.onResult(transformed, null); + try { + Collection transformed = Collections2.transform(result, transformation); + callback.onResult(transformed, null); + } catch (Exception ex) { + callback.onResult(null, ex); + } } else { callback.onResult(ImmutableList.of(), null); }