100 lines
2.9 KiB
Java
100 lines
2.9 KiB
Java
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 BannedAsn {
|
|
|
|
private static final MongoCollection<BannedAsn> bannedAsnsCollection = APIv3.getDatabase().getCollection("bannedAsns", BannedAsn.class);
|
|
|
|
private static Map<Integer, BannedAsn> bannedAsnIdCache = null;
|
|
private static List<BannedAsn> bannedAsnCache = null;
|
|
|
|
@Getter @Id private int id;
|
|
@Getter @Setter String note;
|
|
@Getter private Instant bannedAt;
|
|
@Getter private Instant lastUpdatedAt;
|
|
|
|
public static List<BannedAsn> findAll() {
|
|
return bannedAsnCache;
|
|
}
|
|
|
|
public static BannedAsn findById(int id) {
|
|
return bannedAsnIdCache.get(id);
|
|
}
|
|
|
|
static {
|
|
updateCache();
|
|
|
|
APIv3.getVertxInstance().setPeriodic(TimeUnit.MINUTES.toMillis(1), (id) -> {
|
|
updateCache();
|
|
});
|
|
}
|
|
|
|
private static void updateCache() {
|
|
bannedAsnsCollection.find().into(new LinkedList<>(), (bannedAsns, error) -> {
|
|
if (error != null) {
|
|
error.printStackTrace();
|
|
return;
|
|
}
|
|
|
|
Map<Integer, BannedAsn> working = new HashMap<>();
|
|
|
|
for (BannedAsn bannedAsn : bannedAsns) {
|
|
working.put(bannedAsn.getId(), bannedAsn);
|
|
}
|
|
|
|
bannedAsnIdCache = ImmutableMap.copyOf(working);
|
|
bannedAsnCache = ImmutableList.copyOf(bannedAsns);
|
|
});
|
|
}
|
|
|
|
private BannedAsn() {} // For Jackson
|
|
|
|
public BannedAsn(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) {
|
|
bannedAsnCache.add(this);
|
|
bannedAsnIdCache.put(id, this);
|
|
bannedAsnsCollection.insertOne(this, callback);
|
|
}
|
|
|
|
public void save(SingleResultCallback<UpdateResult> callback) {
|
|
bannedAsnsCollection.replaceOne(new Document("_id", id), this, callback);
|
|
}
|
|
|
|
public void delete(SingleResultCallback<DeleteResult> callback) {
|
|
bannedAsnCache.remove(this);
|
|
bannedAsnIdCache.remove(id);
|
|
bannedAsnsCollection.deleteOne(new Document("_id", id), callback);
|
|
}
|
|
|
|
} |