diff --git a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java index d742065..10a75ba 100644 --- a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java @@ -2,7 +2,7 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.AuditLog; import net.frozenorb.apiv3.util.IPUtils; import org.bson.Document; @@ -10,7 +10,7 @@ import java.time.Instant; import java.util.UUID; @ToString -public final class AuditLogEntry implements LiteFullJson { +public final class AuditLogEntry extends BaseModel { @Getter private String id; @Getter private UUID performedBy; @@ -20,12 +20,16 @@ public final class AuditLogEntry implements LiteFullJson { @Getter private Document actionData; public AuditLogEntry(Document json) { + super(AuditLog.COLLECTION_NAME); + 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"); + + setId(id); } public Document toLiteJson() { diff --git a/src/main/java/net/frozenorb/apiv3/model/BaseModel.java b/src/main/java/net/frozenorb/apiv3/model/BaseModel.java new file mode 100644 index 0000000..ce224a8 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/BaseModel.java @@ -0,0 +1,28 @@ +package net.frozenorb.apiv3.model; + +import com.mongodb.async.SingleResultCallback; +import com.mongodb.client.result.DeleteResult; +import com.mongodb.client.result.UpdateResult; +import lombok.Setter; +import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.LiteFullJson; +import org.bson.Document; + +public abstract class BaseModel implements LiteFullJson { + + private final String collectionName; + @Setter private String id; + + public BaseModel(String collectionName) { + this.collectionName = collectionName; + } + + protected void update(Document update, SingleResultCallback callback) { + APIv3.getMongo().getCollection(collectionName).updateOne(new Document("_id", id), update, callback); + } + + protected void delete(SingleResultCallback callback) { + APIv3.getMongo().getCollection(collectionName).deleteOne(new Document("_id", id), callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java b/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java index 64c6739..15118b5 100644 --- a/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java @@ -2,34 +2,66 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.EmailTemplates; import org.bson.Document; +import java.time.Instant; import java.util.Map; +import java.util.UUID; @ToString -public final class EmailTemplate implements LiteFullJson { +public final class EmailTemplate extends BaseModel { @Getter private String id; @Getter private String title; @Getter private String body; + @Getter private Instant lastUpdatedAt; + @Getter private UUID lastUpdatedBy; public EmailTemplate(Document json) { + super(EmailTemplates.COLLECTION_NAME); + this.id = json.getString("_id"); this.title = json.getString("title"); this.body = json.getString("body"); + this.lastUpdatedAt = (Instant) json.get("lastUpdatedAt"); + this.lastUpdatedBy = UUID.fromString(json.getString("lastUpdatedBy")); + + setId(id); } public Document toLiteJson() { - Document json = new Document(); + Document json = new Document(); json.put("id", id); json.put("title", title); json.put("body", body); + json.put("lastUpdatedAt", lastUpdatedAt.toString()); + json.put("lastUpdatedBy", lastUpdatedBy.toString()); return json; } + public void update(String title, String body, UUID updatedBy) { + this.title = title; + this.body = body; + this.lastUpdatedAt = Instant.now(); + this.lastUpdatedBy = updatedBy; + + Document update = new Document(); + + update.put("title", title); + update.put("body", body); + update.put("lastUpdatedAt", lastUpdatedAt.toString()); + update.put("lastUpdatedBy", lastUpdatedBy.toString()); + + super.update(new Document("$set", update), (result, error) -> {}); + } + + public void delete() { + super.delete((result, error) -> {}); + } + public String fillTitle(Map replacements) { return fill(title, replacements); } diff --git a/src/main/java/net/frozenorb/apiv3/model/Grant.java b/src/main/java/net/frozenorb/apiv3/model/Grant.java index e466b02..7e695be 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -3,7 +3,7 @@ package net.frozenorb.apiv3.model; import com.google.common.collect.ImmutableSet; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.Grants; import org.bson.Document; import java.time.Instant; @@ -12,13 +12,14 @@ import java.util.Set; import java.util.UUID; @ToString -public final class Grant implements LiteFullJson { +public final class Grant extends BaseModel { @Getter private String id; @Getter private UUID target; @Getter private String reason; @Getter private Set scopes; @Getter private String rank; + @Getter private Instant expiresAt; @Getter private UUID addedBy; @Getter private Instant addedAt; @@ -27,11 +28,14 @@ public final class Grant implements LiteFullJson { @Getter private String removalReason; public Grant(Document json) { + super(Grants.COLLECTION_NAME); + this.id = json.getString("_id"); this.target = UUID.fromString(json.getString("target")); this.reason = json.getString("reason"); this.scopes = ImmutableSet.copyOf((Collection) json.get("scopes")); // This is a safe cast, the collection's type is always String this.rank = json.getString("rank"); + this.expiresAt = (Instant) json.get("expiresAt"); this.addedBy = UUID.fromString(json.getString("addedBy")); this.addedAt = (Instant) json.get("addedAt"); @@ -40,6 +44,38 @@ public final class Grant implements LiteFullJson { this.removedAt = (Instant) json.get("removedAt"); this.removalReason = json.getString("removalReason"); } + + setId(id); + } + + public void delete(UUID removedBy, String reason) { + this.removedBy = removedBy; + this.removedAt = Instant.now(); + this.removalReason = reason; + + Document update = new Document(); + + update.put("removedBy", removedBy.toString()); + update.put("removedAt", removedAt); + update.put("removalReason", removalReason); + + super.update(new Document("$set", update), (result, error) -> {}); + } + + public boolean isActive() { + return !(isExpired() || isRemoved()); + } + + public boolean isExpired() { + if (expiresAt == null) { + return false; // Never expires + } else { + return expiresAt.isAfter(Instant.now()); + } + } + + public boolean isRemoved() { + return removedBy != null; } public Document toLiteJson() { @@ -53,6 +89,10 @@ public final class Grant implements LiteFullJson { json.put("addedBy", addedBy.toString()); json.put("addedAt", addedAt.toString()); + json.put("active", isActive()); + json.put("expired", isExpired()); + json.put("removed", isRemoved()); + if (removedBy != null) { json.put("removedBy", removedBy.toString()); json.put("removedAt", removedAt.toString()); diff --git a/src/main/java/net/frozenorb/apiv3/model/IPBan.java b/src/main/java/net/frozenorb/apiv3/model/IPBan.java index f289f2c..cff38dc 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IPBan.java +++ b/src/main/java/net/frozenorb/apiv3/model/IPBan.java @@ -2,16 +2,20 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.IPBans; import org.bson.Document; @ToString -public final class IPBan implements LiteFullJson { +public final class IPBan extends BaseModel { @Getter private String id; public IPBan(Document json) { + super(IPBans.COLLECTION_NAME); + this.id = json.getString("_id"); + + setId(id); } public Document toLiteJson() { diff --git a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java index 41fb149..a9aa284 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java @@ -2,16 +2,20 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.IPLog; import org.bson.Document; @ToString -public final class IPLogEntry implements LiteFullJson { +public final class IPLogEntry extends BaseModel { @Getter private String id; public IPLogEntry(Document json) { + super(IPLog.COLLECTION_NAME); + this.id = json.getString("_id"); + + setId(id); } public Document toLiteJson() { diff --git a/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java b/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java index 5be4fd5..79f5f96 100644 --- a/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/MaxMindCacheEntry.java @@ -2,16 +2,20 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.MaxMindCache; import org.bson.Document; @ToString -public final class MaxMindCacheEntry implements LiteFullJson { +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() { diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java index eb5d414..e9eba3c 100644 --- a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java @@ -2,16 +2,20 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.NotificationLog; import org.bson.Document; @ToString -public final class NotificationLogEntry implements LiteFullJson { +public final class NotificationLogEntry extends BaseModel { @Getter private String id; public NotificationLogEntry(Document json) { + super(NotificationLog.COLLECTION_NAME); + this.id = json.getString("_id"); + + setId(id); } public Document toLiteJson() { diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index f686b50..eb84e96 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -2,16 +2,20 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.Punishments; import org.bson.Document; @ToString -public final class Punishment implements LiteFullJson { +public final class Punishment extends BaseModel { @Getter private String id; public Punishment(Document json) { + super(Punishments.COLLECTION_NAME); + this.id = json.getString("_id"); + + setId(id); } public Document toLiteJson() { diff --git a/src/main/java/net/frozenorb/apiv3/model/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index b1d4c05..0e1d942 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -2,17 +2,18 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.LiteFullJson; import net.frozenorb.apiv3.accessor.Servers; import org.bson.Document; import java.time.Instant; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; @ToString -public final class Server implements LiteFullJson { +public final class Server extends BaseModel { @Getter private String id; @Getter private String bungeeId; @@ -25,6 +26,8 @@ public final class Server implements LiteFullJson { @Getter private List players; public Server(Document json) { + super(Servers.COLLECTION_NAME); + this.id = json.getString("_id"); this.bungeeId = json.getString("bungeeId"); this.displayName = json.getString("displayName"); @@ -38,10 +41,12 @@ public final class Server implements LiteFullJson { for (Object uuidString : (Collection) json.get("players")) { players.add(UUID.fromString((String) uuidString)); } + + setId(id); } public void update() { - APIv3.getMongo().getCollection(Servers.COLLECTION_NAME).updateOne(new Document("_id", id), new Document("$set", new Document("lastUpdate", new Date())), (result, error) -> {}); + super.update(new Document("$set", new Document("lastUpdate", Instant.now())), (result, error) -> {}); } public Document toLiteJson() { diff --git a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java index e413d8c..09477b1 100644 --- a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java @@ -2,16 +2,20 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.ServerGroups; import org.bson.Document; @ToString -public final class ServerGroup implements LiteFullJson { +public final class ServerGroup extends BaseModel { @Getter private String id; public ServerGroup(Document json) { + super(ServerGroups.COLLECTION_NAME); + this.id = json.getString("_id"); + + setId(id); } public Document toLiteJson() { diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index fce46dc..91676cc 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -2,16 +2,20 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.LiteFullJson; +import net.frozenorb.apiv3.accessor.Users; import org.bson.Document; @ToString -public final class User implements LiteFullJson { +public final class User extends BaseModel { @Getter private String id; public User(Document json) { + super(Users.COLLECTION_NAME); + this.id = json.getString("_id"); + + setId(id); } public Document toLiteJson() {