Add position ranking to database service and test haste api
This commit is contained in:
parent
966c131579
commit
f70934202d
@ -12,7 +12,7 @@ allprojects {
|
|||||||
apply(plugin = "com.github.johnrengelman.shadow")
|
apply(plugin = "com.github.johnrengelman.shadow")
|
||||||
|
|
||||||
group = "de.weapie"
|
group = "de.weapie"
|
||||||
version = "2024.07.06-02"
|
version = "2024.07.07-01"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
@ -6,10 +6,12 @@ import com.mongodb.client.model.Sorts;
|
|||||||
import com.mongodb.client.model.UpdateOptions;
|
import com.mongodb.client.model.UpdateOptions;
|
||||||
import de.weapie.devlok.database.Database;
|
import de.weapie.devlok.database.Database;
|
||||||
import de.weapie.devlok.database.utils.BasicService;
|
import de.weapie.devlok.database.utils.BasicService;
|
||||||
|
import de.weapie.devlok.utils.data.Pair;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class MongoDBService implements BasicService {
|
public class MongoDBService implements BasicService {
|
||||||
|
|
||||||
@ -141,4 +143,19 @@ public class MongoDBService implements BasicService {
|
|||||||
return documents;
|
return documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long position(String collection, Pair<String, Object> filterPair, String fieldName) {
|
||||||
|
if(!this.database().isConnected()) return -1;
|
||||||
|
long currentRanking = 1;
|
||||||
|
|
||||||
|
for (Document document : this.collection(collection).find().sort(Sorts.descending(fieldName))) {
|
||||||
|
if ((document.get(filterPair.getKey(), filterPair.getValue().getClass()).equals(filterPair.getValue()))) {
|
||||||
|
return currentRanking;
|
||||||
|
}
|
||||||
|
currentRanking++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package de.weapie.devlok.database.mysql;
|
|||||||
import com.mongodb.client.MongoCollection;
|
import com.mongodb.client.MongoCollection;
|
||||||
import de.weapie.devlok.database.Database;
|
import de.weapie.devlok.database.Database;
|
||||||
import de.weapie.devlok.database.utils.BasicService;
|
import de.weapie.devlok.database.utils.BasicService;
|
||||||
|
import de.weapie.devlok.utils.data.Pair;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@ -143,6 +144,13 @@ public class MySQLService implements BasicService {
|
|||||||
return !documents.isEmpty() ? documents : new ArrayList<>();
|
return !documents.isEmpty() ? documents : new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long position(String collection, Pair<String, Object> filterPair, String fieldName) {
|
||||||
|
if(!this.database().isConnected()) return -1;
|
||||||
|
List<Document> documents = this.executeQuery("SELECT " + filterPair.getKey() + ", FIND_IN_SET(" + fieldName + ", (SELECT GROUP_CONCAT(" + fieldName + " ORDER BY " + fieldName + " DESC) FROM " + collection + ")) AS ranking FROM " + collection + " WHERE " + filterPair.getKey() + " = '" + filterPair.getValue() + "';");
|
||||||
|
return !documents.isEmpty() ? documents.get(0).getLong("ranking") : -1;
|
||||||
|
}
|
||||||
|
|
||||||
private void execute(String sql) {
|
private void execute(String sql) {
|
||||||
Statement statement = null;
|
Statement statement = null;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
package de.weapie.devlok.database.utils;
|
package de.weapie.devlok.database.utils;
|
||||||
|
|
||||||
import com.mongodb.client.MongoCollection;
|
import com.mongodb.client.MongoCollection;
|
||||||
|
import de.weapie.devlok.utils.data.Pair;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public interface BasicService {
|
public interface BasicService {
|
||||||
|
|
||||||
@ -23,5 +25,5 @@ public interface BasicService {
|
|||||||
void deleteMany(String collection, Document filter);
|
void deleteMany(String collection, Document filter);
|
||||||
void deleteAll(String collection);
|
void deleteAll(String collection);
|
||||||
List<Document> top(String collection, int maxPlace, String fieldName);
|
List<Document> top(String collection, int maxPlace, String fieldName);
|
||||||
|
long position(String collection, Pair<String, Object> filter, String fieldName);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import de.weapie.devlok.database.Database;
|
|||||||
import de.weapie.devlok.database.config.DatabaseType;
|
import de.weapie.devlok.database.config.DatabaseType;
|
||||||
import de.weapie.devlok.database.mongodb.MongoDBDatabase;
|
import de.weapie.devlok.database.mongodb.MongoDBDatabase;
|
||||||
import de.weapie.devlok.database.mysql.MySQLDatabase;
|
import de.weapie.devlok.database.mysql.MySQLDatabase;
|
||||||
|
import de.weapie.devlok.utils.data.Pair;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.types.ObjectId;
|
||||||
@ -34,6 +35,10 @@ public class TestDatabase {
|
|||||||
|
|
||||||
System.out.println("Exists: " + Database.getDatabase().getService().exist("players", new Document("uuid", uuid)));
|
System.out.println("Exists: " + Database.getDatabase().getService().exist("players", new Document("uuid", uuid)));
|
||||||
|
|
||||||
|
System.out.println("Position: " + Database.getDatabase().getService().position("players", new Pair<>("uuid", uuid), "coins"));
|
||||||
|
System.out.println("Position (1): " + Database.getDatabase().getService().position("players", new Pair<>("uuid", "e90c4f55-262c-4608-9347-8440ba0f3896"), "coins"));
|
||||||
|
System.out.println("Position (2): " + Database.getDatabase().getService().position("players", new Pair<>("uuid", "23840167-672f-426a-be74-4ae790b9c2aa"), "coins"));
|
||||||
|
|
||||||
// Update entry in database with complete document
|
// Update entry in database with complete document
|
||||||
Database.getDatabase().getService().replace("players", new Document("uuid", uuid), new Document("uuid", uuid).append("coins", coins + 1));
|
Database.getDatabase().getService().replace("players", new Document("uuid", uuid), new Document("uuid", uuid).append("coins", coins + 1));
|
||||||
|
|
||||||
@ -42,7 +47,10 @@ public class TestDatabase {
|
|||||||
System.out.println("Json: " + document.toJson());
|
System.out.println("Json: " + document.toJson());
|
||||||
|
|
||||||
// Delete entry from database
|
// Delete entry from database
|
||||||
Database.getDatabase().getService().deleteOne("players", new Document("uuid", uuid));
|
//Database.getDatabase().getService().deleteOne("players", new Document("uuid", uuid));
|
||||||
|
|
||||||
|
// Delete all entry from database
|
||||||
|
//Database.getDatabase().getService().deleteAll("players");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package de.weapie.devlok.files.test;
|
package de.weapie.devlok.files.test;
|
||||||
|
|
||||||
import de.weapie.devlok.files.config.DevlokConfig;
|
import de.weapie.devlok.files.config.DevlokConfig;
|
||||||
|
import de.weapie.devlok.files.haste.HasteAPI;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@ -16,6 +17,9 @@ public class TestFiles {
|
|||||||
public void handle() {
|
public void handle() {
|
||||||
config.write();
|
config.write();
|
||||||
config.read();
|
config.read();
|
||||||
|
|
||||||
|
HasteAPI.setHasteUrl("https://haste.jf.home");
|
||||||
|
System.out.println(HasteAPI.createHaste(config.value().toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user