Finish up work on models

This commit is contained in:
Colin McDonald 2016-03-05 23:21:49 -05:00
parent 0b13eed247
commit e9c3198e5b
15 changed files with 135 additions and 65 deletions

View File

@ -13,7 +13,6 @@ import net.frozenorb.apiv3.accessor.Users;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.JsonUtils; import net.frozenorb.apiv3.util.JsonUtils;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.UUID; import java.util.UUID;
@ -21,6 +20,9 @@ public final class APIv3 extends AbstractVerticle {
@Getter private static MongoDatabase mongo; @Getter private static MongoDatabase mongo;
// TODO: review the find* methods in models to make sure they're good (and sometimes not too broad, ex findAll on Users)
// TODO: consistency -- sometimes we refer to a user as user or target.
public void start() { public void start() {
Router coreHttpRouter = Router.router(vertx); Router coreHttpRouter = Router.router(vertx);
mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray()); mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray());
@ -94,7 +96,7 @@ public final class APIv3 extends AbstractVerticle {
coreHttpRouter.get("/doAction/:actionType").handler(ctx -> { coreHttpRouter.get("/doAction/:actionType").handler(ctx -> {
String actionType = ctx.request().getParam("actionType"); String actionType = ctx.request().getParam("actionType");
AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document()); //AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document());
ctx.response().end("{'logged': true}"); ctx.response().end("{'logged': true}");
}); });

View File

@ -34,8 +34,8 @@ public class AuditLog {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback);
} }
public static void findByEntryId(String entryId, SingleResultCallback<AuditLogEntry> callback) { public static void log(UUID user, String userIp, String actionType, SingleResultCallback<Void> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", entryId), AuditLogEntry::new, callback); log(user, userIp, actionType, callback);
} }
public static void log(UUID user, String userIp, String actionType, Document data, SingleResultCallback<Void> callback) { public static void log(UUID user, String userIp, String actionType, Document data, SingleResultCallback<Void> callback) {

View File

@ -10,7 +10,6 @@ import org.bson.types.ObjectId;
import java.time.Instant; import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -20,12 +19,8 @@ public class Grants {
public static final String COLLECTION_NAME = "grant"; public static final String COLLECTION_NAME = "grant";
private static final Document TIME_BASED_SORT = new Document("addedAt", -1); private static final Document TIME_BASED_SORT = new Document("addedAt", -1);
public static void findAll(SingleResultCallback<Collection<Grant>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, Grant::new, callback);
}
public static void findById(String id, SingleResultCallback<Grant> callback) { public static void findById(String id, SingleResultCallback<Grant> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), TIME_BASED_SORT, Grant::new, callback); MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Grant::new, callback);
} }
public static void findByTarget(UUID target, SingleResultCallback<Collection<Grant>> callback) { public static void findByTarget(UUID target, SingleResultCallback<Collection<Grant>> callback) {

View File

@ -2,24 +2,53 @@ package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.model.IPLogEntry; import net.frozenorb.apiv3.model.IPLogEntry;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.UUID;
@UtilityClass @UtilityClass
public class IPLog { public class IPLog {
public static final String COLLECTION_NAME = "ipLog"; public static final String COLLECTION_NAME = "ipLog";
private static final Document TIME_BASED_SORT = new Document("lastSeen", -1);
public static void findAll(SingleResultCallback<Collection<IPLogEntry>> callback) { public static void findByUser(UUID user, SingleResultCallback<Collection<IPLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPLogEntry::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("user", user.toString()), TIME_BASED_SORT, IPLogEntry::new, callback);
} }
public static void findById(String id, SingleResultCallback<IPLogEntry> callback) { public static void findByIp(String ip, SingleResultCallback<Collection<IPLogEntry>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPLogEntry::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("ip", ip), TIME_BASED_SORT, IPLogEntry::new, callback);
} }
public static void log(UUID user, String ip , SingleResultCallback<Void> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("user", user.toString()).append("ip", ip), IPLogEntry::new, (ipLogEntry, error) -> {
if (error != null) {
callback.onResult(null, error);
} else if (ipLogEntry != null) {
ipLogEntry.used(callback);
} else {
Document insert = new Document();
insert.put("_id", new ObjectId().toString());
insert.put("user", user.toString());
insert.put("ip", ip);
insert.put("lastSeen", Instant.now());
insert.put("firstSeen", Instant.now());
insert.put("uses", 1);
APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, (ignored, error2) -> {
callback.onResult(null, error2);
});
}
});
}
} }

View File

@ -2,23 +2,44 @@ package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.NotificationLogEntry; import net.frozenorb.apiv3.model.NotificationLogEntry;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.UUID;
@UtilityClass @UtilityClass
public class NotificationLog { public class NotificationLog {
public static final String COLLECTION_NAME = "notificationLog"; public static final String COLLECTION_NAME = "notificationLog";
public static void findAll(SingleResultCallback<Collection<NotificationLogEntry>> callback) { public static void findByTarget(UUID target, SingleResultCallback<Collection<NotificationLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationLogEntry::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), NotificationLogEntry::new, callback);
} }
public static void findById(String id, SingleResultCallback<NotificationLogEntry> callback) { public static void logEmail(UUID user, String title, String body, SingleResultCallback<Void> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationLogEntry::new, callback); log(user, NotificationLogEntry.NotificationType.EMAIL, title, body, callback);
}
public static void logSMS(UUID user, String title, String body, SingleResultCallback<Void> callback) {
log(user, NotificationLogEntry.NotificationType.SMS, title, body, callback);
}
private static void log(UUID user, NotificationLogEntry.NotificationType type, String title, String body, SingleResultCallback<Void> callback) {
Document insert = new Document();
insert.put("_id", new ObjectId().toString());
insert.put("target", user.toString());
insert.put("sentAt", Instant.now());
insert.put("type", type.name());
insert.put("title", title);
insert.put("body", body);
APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback);
} }
} }

View File

@ -3,7 +3,7 @@ package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.EmailTemplate; import net.frozenorb.apiv3.model.NotificationTemplate;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document; import org.bson.Document;
@ -12,19 +12,19 @@ import java.util.Collection;
import java.util.UUID; import java.util.UUID;
@UtilityClass @UtilityClass
public class EmailTemplates { public class NotificationTemplates {
public static final String COLLECTION_NAME = "emailTemplate"; public static final String COLLECTION_NAME = "notificationTemplate";
public static void findAll(SingleResultCallback<Collection<EmailTemplate>> callback) { public static void findAll(SingleResultCallback<Collection<NotificationTemplate>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), EmailTemplate::new, callback); MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationTemplate::new, callback);
} }
public static void findById(String id, SingleResultCallback<EmailTemplate> callback) { public static void findById(String id, SingleResultCallback<NotificationTemplate> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), EmailTemplate::new, callback); MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationTemplate::new, callback);
} }
public static void createTemplate(String id, String title, String body, UUID creator, SingleResultCallback<Void> callback) { public static void createNotificationTemplate(String id, String title, String body, UUID creator, SingleResultCallback<Void> callback) {
Document insert = new Document(); Document insert = new Document();
insert.put("_id", id); insert.put("_id", id);

View File

@ -2,23 +2,50 @@ package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.Set;
import java.util.UUID;
@UtilityClass @UtilityClass
public class Punishments { public class Punishments {
public static final String COLLECTION_NAME = "punishment"; public static final String COLLECTION_NAME = "punishment";
private static final Document TIME_BASED_SORT = new Document("addedAt", -1);
public static void findAll(SingleResultCallback<Collection<Punishment>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Punishment::new, callback);
}
public static void findById(String id, SingleResultCallback<Punishment> callback) { public static void findById(String id, SingleResultCallback<Punishment> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback); MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback);
} }
public static void findByTarget(UUID target, SingleResultCallback<Collection<Punishment>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), TIME_BASED_SORT, Punishment::new, callback);
}
public static void findByAddedBy(UUID addedBy, SingleResultCallback<Collection<Grant>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback);
}
public static void createPunishment(UUID target, Punishment.PunishmentType type, Instant expiresAt, UUID addedBy, String addedOn, String reason, SingleResultCallback<Void> callback) {
Document insert = new Document();
insert.put("_id", new ObjectId().toString());
insert.put("target", target.toString());
insert.put("reason", reason);
insert.put("type", type.name());
insert.put("expiresAt", expiresAt);
insert.put("addedBy", addedBy.toString());
insert.put("addedAt", Instant.now());
insert.put("addedOn", addedOn);
APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback);
}
} }

View File

@ -39,13 +39,6 @@ public final class AuditLogEntry extends BaseModel {
json.put("performedAt", performedAt.toString()); json.put("performedAt", performedAt.toString());
json.put("performedFrom", performedFrom); json.put("performedFrom", performedFrom);
json.put("actionType", actionType); json.put("actionType", actionType);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
json.put("actionData", actionData); json.put("actionData", actionData);
return json; return json;

View File

@ -17,12 +17,16 @@ public abstract class BaseModel implements LiteFullJson {
this.collectionName = collectionName; this.collectionName = collectionName;
} }
protected void update(Document update, SingleResultCallback<UpdateResult> callback) { protected void update(Document update, SingleResultCallback<Void> callback) {
APIv3.getMongo().getCollection(collectionName).updateOne(new Document("_id", id), update, callback); APIv3.getMongo().getCollection(collectionName).updateOne(new Document("_id", id), update, (updateResult, error) -> {
callback.onResult(null, error);
});
} }
protected void delete(SingleResultCallback<DeleteResult> callback) { protected void delete(SingleResultCallback<Void> callback) {
APIv3.getMongo().getCollection(collectionName).deleteOne(new Document("_id", id), callback); APIv3.getMongo().getCollection(collectionName).deleteOne(new Document("_id", id), (deleteResult, error) -> {
callback.onResult(null, error);
});
} }
} }

View File

@ -2,7 +2,6 @@ package net.frozenorb.apiv3.model;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult; import com.mongodb.client.result.UpdateResult;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
@ -53,7 +52,7 @@ public final class Grant extends BaseModel {
setId(id); setId(id);
} }
public void delete(UUID removedBy, String reason, SingleResultCallback<UpdateResult> callback) { public void delete(UUID removedBy, String reason, SingleResultCallback<Void> callback) {
this.removedBy = removedBy; this.removedBy = removedBy;
this.removedAt = Instant.now(); this.removedAt = Instant.now();
this.removalReason = reason; this.removalReason = reason;

View File

@ -1,9 +1,10 @@
package net.frozenorb.apiv3.model; package net.frozenorb.apiv3.model;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.client.result.UpdateResult;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import net.frozenorb.apiv3.accessor.IPLog; import net.frozenorb.apiv3.accessor.IPLog;
import net.frozenorb.apiv3.util.IPUtils;
import org.bson.Document; import org.bson.Document;
import java.time.Instant; import java.time.Instant;
@ -32,7 +33,7 @@ public final class IPLogEntry extends BaseModel {
setId(id); setId(id);
} }
public void used() { public void used(SingleResultCallback<Void> callback) {
this.lastSeen = Instant.now(); this.lastSeen = Instant.now();
this.uses++; this.uses++;
@ -41,7 +42,9 @@ public final class IPLogEntry extends BaseModel {
set.put("lastSeen", lastSeen); set.put("lastSeen", lastSeen);
set.put("uses", uses); set.put("uses", uses);
update(new Document("$set", set), (result, error) -> {}); super.update(new Document("$set", set), (updateResult, error) -> {
callback.onResult(null, error);
});
} }
public Document toLiteJson() { public Document toLiteJson() {
@ -49,7 +52,7 @@ public final class IPLogEntry extends BaseModel {
json.put("id", id); json.put("id", id);
json.put("user", user.toString()); json.put("user", user.toString());
json.put("ip", IPUtils.machineToHuman(ip)); json.put("ip", ip);
json.put("firstSeen", firstSeen.toString()); json.put("firstSeen", firstSeen.toString());
json.put("lastSeen", lastSeen.toString()); json.put("lastSeen", lastSeen.toString());
json.put("uses", uses); json.put("uses", uses);

View File

@ -46,7 +46,7 @@ public final class NotificationLogEntry extends BaseModel {
public enum NotificationType { public enum NotificationType {
EMAIL, TEXT EMAIL, SMS
} }

View File

@ -1,11 +1,9 @@
package net.frozenorb.apiv3.model; package net.frozenorb.apiv3.model;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import net.frozenorb.apiv3.accessor.EmailTemplates; import net.frozenorb.apiv3.accessor.NotificationTemplates;
import org.bson.Document; import org.bson.Document;
import java.time.Instant; import java.time.Instant;
@ -13,7 +11,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ToString @ToString
public final class EmailTemplate extends BaseModel { public final class NotificationTemplate extends BaseModel {
@Getter private String id; @Getter private String id;
@Getter private String title; @Getter private String title;
@ -21,8 +19,8 @@ public final class EmailTemplate extends BaseModel {
@Getter private Instant lastUpdatedAt; @Getter private Instant lastUpdatedAt;
@Getter private UUID lastUpdatedBy; @Getter private UUID lastUpdatedBy;
public EmailTemplate(Document json) { public NotificationTemplate(Document json) {
super(EmailTemplates.COLLECTION_NAME); super(NotificationTemplates.COLLECTION_NAME);
this.id = json.getString("_id"); this.id = json.getString("_id");
this.title = json.getString("title"); this.title = json.getString("title");
@ -45,7 +43,7 @@ public final class EmailTemplate extends BaseModel {
return json; return json;
} }
public void update(String title, String body, UUID updatedBy, SingleResultCallback<UpdateResult> callback) { public void update(String title, String body, UUID updatedBy, SingleResultCallback<Void> callback) {
this.title = title; this.title = title;
this.body = body; this.body = body;
this.lastUpdatedAt = Instant.now(); this.lastUpdatedAt = Instant.now();
@ -58,10 +56,12 @@ public final class EmailTemplate extends BaseModel {
set.put("lastUpdatedAt", lastUpdatedAt.toString()); set.put("lastUpdatedAt", lastUpdatedAt.toString());
set.put("lastUpdatedBy", lastUpdatedBy.toString()); set.put("lastUpdatedBy", lastUpdatedBy.toString());
super.update(new Document("$set", set), callback); super.update(new Document("$set", set), (updateResult, error) -> {
callback.onResult(null, error);
});
} }
public void delete(SingleResultCallback<DeleteResult> callback) { public void delete(SingleResultCallback<Void> callback) {
super.delete(callback); super.delete(callback);
} }

View File

@ -1,5 +1,6 @@
package net.frozenorb.apiv3.model; package net.frozenorb.apiv3.model;
import com.mongodb.async.SingleResultCallback;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import net.frozenorb.apiv3.accessor.Punishments; import net.frozenorb.apiv3.accessor.Punishments;
@ -47,7 +48,7 @@ public final class Punishment extends BaseModel {
setId(id); setId(id);
} }
public void delete(UUID removedBy, String reason) { public void delete(UUID removedBy, String reason, SingleResultCallback<Void> callback) {
this.removedBy = removedBy; this.removedBy = removedBy;
this.removedAt = Instant.now(); this.removedAt = Instant.now();
this.removalReason = reason; this.removalReason = reason;
@ -58,7 +59,7 @@ public final class Punishment extends BaseModel {
set.put("removedAt", removedAt); set.put("removedAt", removedAt);
set.put("removalReason", removalReason); set.put("removalReason", removalReason);
super.update(new Document("$set", set), (result, error) -> {}); super.update(new Document("$set", set), callback);
} }
public boolean isActive() { public boolean isActive() {

View File

@ -62,11 +62,7 @@ public class MongoUtils {
} }
public static <T> void findOneAndTransform(String collection, Document query, Function<Document, T> transformation, SingleResultCallback<T> callback) { public static <T> void findOneAndTransform(String collection, Document query, Function<Document, T> transformation, SingleResultCallback<T> callback) {
findOneAndTransform(collection, query, new Document(), transformation, callback); createFindIterable(collection, query, new Document()).first((result, error) -> {
}
public static <T> void findOneAndTransform(String collection, Document query, Document sort, Function<Document, T> transformation, SingleResultCallback<T> callback) {
createFindIterable(collection, query, sort).first((result, error) -> {
if (error != null) { if (error != null) {
callback.onResult(null, error); callback.onResult(null, error);
} else if (result != null) { } else if (result != null) {