More work on models!
This commit is contained in:
parent
5f6113ce77
commit
734ac190d8
@ -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() {
|
||||
|
28
src/main/java/net/frozenorb/apiv3/model/BaseModel.java
Normal file
28
src/main/java/net/frozenorb/apiv3/model/BaseModel.java
Normal file
@ -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<UpdateResult> callback) {
|
||||
APIv3.getMongo().getCollection(collectionName).updateOne(new Document("_id", id), update, callback);
|
||||
}
|
||||
|
||||
protected void delete(SingleResultCallback<DeleteResult> callback) {
|
||||
APIv3.getMongo().getCollection(collectionName).deleteOne(new Document("_id", id), callback);
|
||||
}
|
||||
|
||||
}
|
@ -2,22 +2,32 @@ 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() {
|
||||
@ -26,10 +36,32 @@ public final class EmailTemplate implements LiteFullJson {
|
||||
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<String, Object> replacements) {
|
||||
return fill(title, replacements);
|
||||
}
|
||||
|
@ -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<String> 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());
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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<UUID> 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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user