Add banned cell carrier model to prepare for full Zang integration

This commit is contained in:
Colin McDonald 2016-06-25 19:21:23 -04:00
parent 49a441fe03
commit 6957b67863
7 changed files with 191 additions and 0 deletions

View File

@ -44,6 +44,10 @@ import net.frozenorb.apiv3.route.bannedAsns.DELETEBannedAsnsId;
import net.frozenorb.apiv3.route.bannedAsns.GETBannedAsns; import net.frozenorb.apiv3.route.bannedAsns.GETBannedAsns;
import net.frozenorb.apiv3.route.bannedAsns.GETBannedAsnsId; import net.frozenorb.apiv3.route.bannedAsns.GETBannedAsnsId;
import net.frozenorb.apiv3.route.bannedAsns.POSTBannedAsns; import net.frozenorb.apiv3.route.bannedAsns.POSTBannedAsns;
import net.frozenorb.apiv3.route.bannedCellCarriers.DELETEBannedCellCarriersId;
import net.frozenorb.apiv3.route.bannedCellCarriers.GETBannedCellCarriers;
import net.frozenorb.apiv3.route.bannedCellCarriers.GETBannedCellCarriersId;
import net.frozenorb.apiv3.route.bannedCellCarriers.POSTBannedCellCarriers;
import net.frozenorb.apiv3.route.chatFilterList.GETChatFilter; import net.frozenorb.apiv3.route.chatFilterList.GETChatFilter;
import net.frozenorb.apiv3.route.emailToken.GETEmailTokensIdOwner; import net.frozenorb.apiv3.route.emailToken.GETEmailTokensIdOwner;
import net.frozenorb.apiv3.route.emailToken.POSTEmailTokensIdConfirm; import net.frozenorb.apiv3.route.emailToken.POSTEmailTokensIdConfirm;
@ -240,6 +244,12 @@ public final class APIv3 extends AbstractVerticle {
//http.put("/bannedAsns/:id").blockingHandler(new PUTBannedAsnsId(), false); //http.put("/bannedAsns/:id").blockingHandler(new PUTBannedAsnsId(), false);
http.delete("/bannedAsns/:id").blockingHandler(new DELETEBannedAsnsId(), false); http.delete("/bannedAsns/:id").blockingHandler(new DELETEBannedAsnsId(), false);
http.get("/bannedCellCarriers/:id").handler(new GETBannedCellCarriersId());
http.get("/bannedCellCarriers").handler(new GETBannedCellCarriers());
http.post("/bannedCellCarriers").blockingHandler(new POSTBannedCellCarriers(), false);
//http.put("/bannedCellCarriers/:id").blockingHandler(new PUTBannedCellCarriersId(), false);
http.delete("/bannedCellCarriers/:id").blockingHandler(new DELETEBannedCellCarriersId(), false);
http.get("/chatFilter").handler(new GETChatFilter()); http.get("/chatFilter").handler(new GETChatFilter());
http.get("/emailTokens/:id/owner").blockingHandler(new GETEmailTokensIdOwner(), false); http.get("/emailTokens/:id/owner").blockingHandler(new GETEmailTokensIdOwner(), false);

View File

@ -0,0 +1,98 @@
package net.frozenorb.apiv3.model;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import fr.javatic.mongo.jacksonCodec.Entity;
import fr.javatic.mongo.jacksonCodec.objectId.Id;
import lombok.Getter;
import lombok.Setter;
import net.frozenorb.apiv3.APIv3;
import org.bson.Document;
import java.time.Instant;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Entity
public final class BannedCellCarrier {
private static final MongoCollection<BannedCellCarrier> bannedCellCarriersCollection = APIv3.getDatabase().getCollection("bannedCellCarriers", BannedCellCarrier.class);
private static Map<Integer, BannedCellCarrier> bannedCellCarrierIdCache = null;
private static List<BannedCellCarrier> bannedCellCarrierCache = null;
@Getter @Id private int id;
@Getter @Setter String note;
@Getter private Instant bannedAt;
@Getter private Instant lastUpdatedAt;
public static List<BannedCellCarrier> findAll() {
return bannedCellCarrierCache;
}
public static BannedCellCarrier findById(int id) {
return bannedCellCarrierIdCache.get(id);
}
static {
updateCache();
APIv3.getVertxInstance().setPeriodic(TimeUnit.MINUTES.toMillis(1), (id) -> updateCache());
}
private static void updateCache() {
bannedCellCarriersCollection.find().into(new LinkedList<>(), (bannedCellCarriers, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
Map<Integer, BannedCellCarrier> working = new HashMap<>();
for (BannedCellCarrier bannedCellCarrier : bannedCellCarriers) {
working.put(bannedCellCarrier.getId(), bannedCellCarrier);
}
bannedCellCarrierIdCache = ImmutableMap.copyOf(working);
bannedCellCarrierCache = ImmutableList.copyOf(bannedCellCarriers);
});
}
private BannedCellCarrier() {} // For Jackson
public BannedCellCarrier(int id, String note) {
this.id = id;
this.note = note;
this.bannedAt = Instant.now();
this.lastUpdatedAt = Instant.now();
}
public void updateNote(String newNote) {
this.note = newNote;
this.lastUpdatedAt = Instant.now();
}
public void insert(SingleResultCallback<Void> callback) {
bannedCellCarrierCache.add(this);
bannedCellCarrierIdCache.put(id, this);
bannedCellCarriersCollection.insertOne(this, callback);
}
public void save(SingleResultCallback<UpdateResult> callback) {
bannedCellCarriersCollection.replaceOne(new Document("_id", id), this, callback);
}
public void delete(SingleResultCallback<DeleteResult> callback) {
bannedCellCarrierCache.remove(this);
bannedCellCarrierIdCache.remove(id);
bannedCellCarriersCollection.deleteOne(new Document("_id", id), callback);
}
}

View File

@ -0,0 +1,27 @@
package net.frozenorb.apiv3.route.bannedCellCarriers;
import com.mongodb.client.result.DeleteResult;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.BannedCellCarrier;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils;
public final class DELETEBannedCellCarriersId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
BannedCellCarrier bannedCellCarrier = BannedCellCarrier.findById(Integer.parseInt(ctx.request().getParam("id")));
if (bannedCellCarrier == null) {
ErrorUtils.respondNotFound(ctx, "Banned cell carrier", ctx.request().getParam("id"));
return;
}
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
bannedCellCarrier.delete(callback);
callback.get();
APIv3.respondJson(ctx, bannedCellCarrier);
}
}

View File

@ -0,0 +1,14 @@
package net.frozenorb.apiv3.route.bannedCellCarriers;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.BannedCellCarrier;
public final class GETBannedCellCarriers implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
APIv3.respondJson(ctx, BannedCellCarrier.findAll());
}
}

View File

@ -0,0 +1,14 @@
package net.frozenorb.apiv3.route.bannedCellCarriers;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.BannedCellCarrier;
public final class GETBannedCellCarriersId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
APIv3.respondJson(ctx, BannedCellCarrier.findById(Integer.parseInt(ctx.request().getParam("id"))));
}
}

View File

@ -0,0 +1,24 @@
package net.frozenorb.apiv3.route.bannedCellCarriers;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.BannedCellCarrier;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
public final class POSTBannedCellCarriers implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson();
int id = requestBody.getInteger("id");
String note = requestBody.getString("note");
BannedCellCarrier bannedCellCarrier = new BannedCellCarrier(id, note);
BlockingCallback<Void> callback = new BlockingCallback<>();
bannedCellCarrier.insert(callback);
callback.get();
APIv3.respondJson(ctx, bannedCellCarrier);
}
}

View File

@ -0,0 +1,4 @@
package net.frozenorb.apiv3.route.bannedCellCarriers;
public class PUTBannedCellCarriersId {
}