diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index e2ab60c..97c2155 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -44,6 +44,10 @@ import net.frozenorb.apiv3.route.bannedAsns.DELETEBannedAsnsId; import net.frozenorb.apiv3.route.bannedAsns.GETBannedAsns; import net.frozenorb.apiv3.route.bannedAsns.GETBannedAsnsId; 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.emailToken.GETEmailTokensIdOwner; 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.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("/emailTokens/:id/owner").blockingHandler(new GETEmailTokensIdOwner(), false); diff --git a/src/main/java/net/frozenorb/apiv3/model/BannedCellCarrier.java b/src/main/java/net/frozenorb/apiv3/model/BannedCellCarrier.java new file mode 100644 index 0000000..982b02b --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/BannedCellCarrier.java @@ -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 bannedCellCarriersCollection = APIv3.getDatabase().getCollection("bannedCellCarriers", BannedCellCarrier.class); + + private static Map bannedCellCarrierIdCache = null; + private static List bannedCellCarrierCache = null; + + @Getter @Id private int id; + @Getter @Setter String note; + @Getter private Instant bannedAt; + @Getter private Instant lastUpdatedAt; + + public static List 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 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 callback) { + bannedCellCarrierCache.add(this); + bannedCellCarrierIdCache.put(id, this); + bannedCellCarriersCollection.insertOne(this, callback); + } + + public void save(SingleResultCallback callback) { + bannedCellCarriersCollection.replaceOne(new Document("_id", id), this, callback); + } + + public void delete(SingleResultCallback callback) { + bannedCellCarrierCache.remove(this); + bannedCellCarrierIdCache.remove(id); + bannedCellCarriersCollection.deleteOne(new Document("_id", id), callback); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/DELETEBannedCellCarriersId.java b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/DELETEBannedCellCarriersId.java new file mode 100644 index 0000000..24428c6 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/DELETEBannedCellCarriersId.java @@ -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 { + + 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 callback = new BlockingCallback<>(); + bannedCellCarrier.delete(callback); + callback.get(); + APIv3.respondJson(ctx, bannedCellCarrier); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/GETBannedCellCarriers.java b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/GETBannedCellCarriers.java new file mode 100644 index 0000000..da168ec --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/GETBannedCellCarriers.java @@ -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 { + + public void handle(RoutingContext ctx) { + APIv3.respondJson(ctx, BannedCellCarrier.findAll()); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/GETBannedCellCarriersId.java b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/GETBannedCellCarriersId.java new file mode 100644 index 0000000..cb18c0e --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/GETBannedCellCarriersId.java @@ -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 { + + public void handle(RoutingContext ctx) { + APIv3.respondJson(ctx, BannedCellCarrier.findById(Integer.parseInt(ctx.request().getParam("id")))); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/POSTBannedCellCarriers.java b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/POSTBannedCellCarriers.java new file mode 100644 index 0000000..85ad9de --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/POSTBannedCellCarriers.java @@ -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 { + + 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 callback = new BlockingCallback<>(); + bannedCellCarrier.insert(callback); + callback.get(); + APIv3.respondJson(ctx, bannedCellCarrier); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/PUTBannedCellCarriersId.java b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/PUTBannedCellCarriersId.java new file mode 100644 index 0000000..e0e9370 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/PUTBannedCellCarriersId.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.route.bannedCellCarriers; + +public class PUTBannedCellCarriersId { +}