More models!

This commit is contained in:
Colin McDonald 2016-02-12 15:12:05 -05:00
parent 734ac190d8
commit f8f23e7de6
6 changed files with 158 additions and 37 deletions

View File

@ -48,14 +48,14 @@ public final class EmailTemplate extends BaseModel {
this.lastUpdatedAt = Instant.now();
this.lastUpdatedBy = updatedBy;
Document update = new Document();
Document set = new Document();
update.put("title", title);
update.put("body", body);
update.put("lastUpdatedAt", lastUpdatedAt.toString());
update.put("lastUpdatedBy", lastUpdatedBy.toString());
set.put("title", title);
set.put("body", body);
set.put("lastUpdatedAt", lastUpdatedAt.toString());
set.put("lastUpdatedBy", lastUpdatedBy.toString());
super.update(new Document("$set", update), (result, error) -> {});
super.update(new Document("$set", set), (result, error) -> {});
}
public void delete() {

View File

@ -20,6 +20,7 @@ public final class Grant extends BaseModel {
@Getter private Set<String> scopes;
@Getter private String rank;
@Getter private Instant expiresAt;
@Getter private UUID addedBy;
@Getter private Instant addedAt;
@ -36,6 +37,7 @@ public final class Grant extends BaseModel {
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");
@ -53,13 +55,13 @@ public final class Grant extends BaseModel {
this.removedAt = Instant.now();
this.removalReason = reason;
Document update = new Document();
Document set = new Document();
update.put("removedBy", removedBy.toString());
update.put("removedAt", removedAt);
update.put("removalReason", removalReason);
set.put("removedBy", removedBy.toString());
set.put("removedAt", removedAt);
set.put("removalReason", removalReason);
super.update(new Document("$set", update), (result, error) -> {});
super.update(new Document("$set", set), (result, error) -> {});
}
public boolean isActive() {
@ -86,12 +88,18 @@ public final class Grant extends BaseModel {
json.put("reason", reason);
json.put("scopes", scopes);
json.put("rank", rank);
json.put("expiresAt", expiresAt == null ? null : expiresAt.toString());
json.put("addedBy", addedBy.toString());
json.put("addedAt", addedAt.toString());
json.put("active", isActive());
json.put("expired", isExpired());
json.put("removed", isRemoved());
Document statusJson = new Document();
statusJson.put("active", isActive());
statusJson.put("expired", isExpired());
statusJson.put("removed", isRemoved());
json.put("status", statusJson);
if (removedBy != null) {
json.put("removedBy", removedBy.toString());

View File

@ -3,33 +3,56 @@ package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.accessor.IPLog;
import net.frozenorb.apiv3.util.IPUtils;
import org.bson.Document;
import java.time.Instant;
import java.util.UUID;
@ToString
public final class IPLogEntry extends BaseModel {
@Getter private String id;
@Getter private UUID user;
@Getter private long ip;
@Getter private Instant firstSeen;
@Getter private Instant lastSeen;
@Getter private int uses;
public IPLogEntry(Document json) {
super(IPLog.COLLECTION_NAME);
this.id = json.getString("_id");
this.user = UUID.fromString(json.getString("user"));
this.ip = json.getLong("ip");
this.lastSeen = (Instant) json.get("lastSeen");
this.firstSeen = (Instant) json.get("firstSeen");
this.uses = json.getInteger("uses");
setId(id);
}
public void used() {
this.lastSeen = Instant.now();
this.uses++;
Document set = new Document();
set.put("lastSeen", lastSeen);
set.put("uses", uses);
update(new Document("$set", set), (result, error) -> {});
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
json.put("id", id);
json.put("user", user.toString());
json.put("ip", IPUtils.machineToHuman(ip));
json.put("firstSeen", firstSeen.toString());
json.put("lastSeen", lastSeen.toString());
json.put("uses", uses);
return json;
}

View File

@ -5,15 +5,28 @@ import lombok.ToString;
import net.frozenorb.apiv3.accessor.NotificationLog;
import org.bson.Document;
import java.time.Instant;
import java.util.UUID;
@ToString
public final class NotificationLogEntry extends BaseModel {
@Getter private String id;
@Getter private UUID target;
@Getter private Instant sentAt;
@Getter private NotificationType type;
@Getter private String title;
@Getter private String body;
public NotificationLogEntry(Document json) {
super(NotificationLog.COLLECTION_NAME);
this.id = json.getString("_id");
this.target = UUID.fromString(json.getString("target"));
this.sentAt = (Instant) json.get("sentAt");
this.type = NotificationType.valueOf(json.getString("type"));
this.title = json.getString("title");
this.body = json.getString("body");
setId(id);
}
@ -21,17 +34,20 @@ public final class NotificationLogEntry extends BaseModel {
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
json.put("id", id);
json.put("target", target.toString());
json.put("sentAt", sentAt.toString());
json.put("type", type.name());
json.put("title", title);
json.put("body", body);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
public enum NotificationType {
EMAIL, TEXT
return json;
}
}

View File

@ -5,33 +5,111 @@ import lombok.ToString;
import net.frozenorb.apiv3.accessor.Punishments;
import org.bson.Document;
import java.time.Instant;
import java.util.UUID;
@ToString
public final class Punishment extends BaseModel {
@Getter private String id;
@Getter private UUID target;
@Getter private String reason;
@Getter private PunishmentType type;
@Getter private Instant expiresAt;
@Getter private UUID addedBy;
@Getter private Instant addedAt;
@Getter private String addedOn;
@Getter private UUID removedBy;
@Getter private Instant removedAt;
@Getter private String removalReason;
public Punishment(Document json) {
super(Punishments.COLLECTION_NAME);
this.id = json.getString("_id");
this.target = UUID.fromString(json.getString("target"));
this.reason = json.getString("reason");
this.type = PunishmentType.valueOf(json.getString("type"));
this.expiresAt = (Instant) json.get("expiresAt");
this.addedBy = UUID.fromString(json.getString("addedBy"));
this.addedAt = (Instant) json.get("addedAt");
this.addedOn = json.getString("addedOn");
if (json.containsKey("removedBy")) {
this.removedBy = UUID.fromString(json.getString("removedBy"));
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 set = new Document();
set.put("removedBy", removedBy.toString());
set.put("removedAt", removedAt);
set.put("removalReason", removalReason);
super.update(new Document("$set", set), (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() {
Document json = new Document();
json.put("_id", id);
json.put("id", id);
json.put("target", target.toString());
json.put("reason", reason);
json.put("type", type.name());
json.put("expiresAt", expiresAt == null ? null : expiresAt.toString());
json.put("addedBy", addedBy.toString());
json.put("addedAt", addedAt.toString());
Document statusJson = new Document();
statusJson.put("active", isActive());
statusJson.put("expired", isExpired());
statusJson.put("removed", isRemoved());
json.put("status", statusJson);
if (removedBy != null) {
json.put("removedBy", removedBy.toString());
json.put("removedAt", removedAt.toString());
json.put("removalReason", removalReason);
}
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
public enum PunishmentType {
BAN, MUTE, WARN
return json;
}
}

View File

@ -45,10 +45,6 @@ public final class Server extends BaseModel {
setId(id);
}
public void update() {
super.update(new Document("$set", new Document("lastUpdate", Instant.now())), (result, error) -> {});
}
public Document toLiteJson() {
Document json = new Document();