Refactor database structure and improve player handling.
Reorganized database-related code into a dedicated module, added mappings for UUID handling, and updated SQL queries for clarity. Enhanced team members API to use player data directly, ensuring consistency and better handling of UUIDs. Introduced new database table for connection settings and adjusted Gradle configurations for modularization.
This commit is contained in:
@@ -2,6 +2,7 @@ plugins {
|
||||
java
|
||||
id("org.springframework.boot") version "3.4.4"
|
||||
id("io.spring.dependency-management") version "1.1.7"
|
||||
// id("com.github.johnrengelman.shadow") version "8.1.1"
|
||||
}
|
||||
|
||||
group = "com.alttd.altitudeweb"
|
||||
@@ -25,16 +26,62 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation(project(":open_api"))
|
||||
implementation(project(":database"))
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
compileOnly("org.projectlombok:lombok")
|
||||
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
|
||||
annotationProcessor("org.projectlombok:lombok")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation("com.mysql:mysql-connector-j:8.0.32")
|
||||
implementation("org.mybatis:mybatis:3.5.13")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation("org.springframework.boot:spring-boot-configuration-processor")
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
tasks.bootJar {
|
||||
mainClass.set("com.alttd.altitudeweb.AltitudeWebApplication")
|
||||
archiveBaseName.set("altitudeweb")
|
||||
archiveClassifier.set("")
|
||||
}
|
||||
|
||||
|
||||
//tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
|
||||
// mergeServiceFiles()
|
||||
// dependencies {
|
||||
// include(dependency("com.mysql:mysql-connector-j"))
|
||||
// }
|
||||
//}
|
||||
|
||||
//tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
|
||||
// manifest {
|
||||
// attributes["Main-Class"] = "com.alttd.altitudeweb.AltitudeWebApplication"
|
||||
// }
|
||||
// archiveBaseName.set("altitudeweb")
|
||||
// archiveClassifier.set("")
|
||||
// mergeServiceFiles()
|
||||
//
|
||||
// // Include everything
|
||||
// from(sourceSets.main.get().output)
|
||||
//
|
||||
// // Include all project dependencies
|
||||
// configurations = listOf(project.configurations.runtimeClasspath.get())
|
||||
//
|
||||
// // Ensure MySQL is included (even though it should be part of runtimeClasspath already)
|
||||
// dependencies {
|
||||
// include(dependency("com.mysql:mysql-connector-j"))
|
||||
// }
|
||||
//
|
||||
// // Enable zip64 mode for large JARs
|
||||
// isZip64 = true
|
||||
//}
|
||||
//
|
||||
//// Make the shadowJar task the default jar task
|
||||
//tasks.named("jar") {
|
||||
// enabled = false
|
||||
//}
|
||||
//
|
||||
//tasks.named("assemble") {
|
||||
// dependsOn("shadowJar")
|
||||
//}
|
||||
|
||||
@@ -1,18 +1,39 @@
|
||||
package com.alttd.altitudeweb.controllers;
|
||||
|
||||
import com.alttd.altitudeweb.api.TeamApi;
|
||||
import com.alttd.altitudeweb.database.Connection;
|
||||
import com.alttd.altitudeweb.database.Databases;
|
||||
import com.alttd.altitudeweb.database.luckperms.Player;
|
||||
import com.alttd.altitudeweb.database.luckperms.TeamMemberMapper;
|
||||
import com.alttd.altitudeweb.model.PlayerDto;
|
||||
import com.alttd.altitudeweb.model.TeamMemberDto;
|
||||
import com.alttd.altitudeweb.model.TeamMembersDto;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class TeamApiController implements TeamApi {
|
||||
|
||||
@Override
|
||||
public ResponseEntity<TeamMembersDto> getTeamMembers(String group) {
|
||||
TeamMembersDto teamMemberDtos = new TeamMembersDto();
|
||||
teamMemberDtos.add(new TeamMemberDto("test", "good"));
|
||||
CompletableFuture<List<Player>> playerGroupFuture = new CompletableFuture<>();
|
||||
Connection.getConnection(Databases.LUCK_PERMS, configuration -> configuration.addMapper(TeamMemberMapper.class))
|
||||
.thenApply(connection -> {
|
||||
connection.runQuery(sqlSession -> {
|
||||
log.info("Loading team members for group {}", group);
|
||||
List<Player> players = sqlSession.getMapper(TeamMemberMapper.class).getTeamMembers("group." + group);
|
||||
playerGroupFuture.complete(players);
|
||||
});
|
||||
return connection;
|
||||
});
|
||||
List<Player> join = playerGroupFuture.join();
|
||||
join.forEach(player -> teamMemberDtos.add(new PlayerDto(player.username(), player.uuid().toString())));
|
||||
return ResponseEntity.ok().body(teamMemberDtos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
package com.alttd.altitudeweb.database;
|
||||
|
||||
import com.alttd.altitudeweb.database.web_db.DatabaseSettings;
|
||||
import com.alttd.altitudeweb.database.web_db.SettingsMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.datasource.pooled.PooledDataSource;
|
||||
import org.apache.ibatis.mapping.Environment;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Slf4j
|
||||
public class Connection {
|
||||
|
||||
private static final HashMap<Databases, Connection> connections = new HashMap<>();
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
private final DatabaseSettings settings;
|
||||
private final AddMappers addMappers;
|
||||
|
||||
private Connection(DatabaseSettings settings, AddMappers addMappers) {
|
||||
this.settings = settings;
|
||||
this.addMappers = addMappers;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AddMappers {
|
||||
void apply(Configuration configuration);
|
||||
}
|
||||
|
||||
public static CompletableFuture<Connection> getConnection(Databases database, AddMappers addMappers) {
|
||||
if (connections.containsKey(database)) {
|
||||
return CompletableFuture.completedFuture(connections.get(database));
|
||||
}
|
||||
if (database == Databases.DEFAULT) {
|
||||
return loadDefaultDatabase(addMappers);
|
||||
}
|
||||
CompletableFuture<DatabaseSettings> settingsFuture = new CompletableFuture<>();
|
||||
getConnection(Databases.DEFAULT, (mapper -> mapper.addMapper(DatabaseSettings.class))).thenApply(connection -> {
|
||||
connection.runQuery(session -> {
|
||||
DatabaseSettings loadedSettings = session.getMapper(SettingsMapper.class).getSettings(database.getInternalName());
|
||||
settingsFuture.complete(loadedSettings);
|
||||
});
|
||||
return null;
|
||||
});
|
||||
return settingsFuture.thenApply(loadedSettings -> {
|
||||
Connection connection = new Connection(loadedSettings, addMappers);
|
||||
connections.put(database, connection);
|
||||
return connection;
|
||||
});
|
||||
}
|
||||
|
||||
private static CompletableFuture<Connection> loadDefaultDatabase(AddMappers addMappers) {
|
||||
DatabaseSettings databaseSettings = new DatabaseSettings(
|
||||
System.getenv("DB_HOST"),
|
||||
Integer.parseInt(System.getenv("DB_PORT")),
|
||||
System.getenv("DB_NAME"),
|
||||
System.getenv("DB_USER"),
|
||||
System.getenv("DB_PASS")
|
||||
);
|
||||
Connection connection = new Connection(databaseSettings, addMappers);
|
||||
return CompletableFuture.completedFuture(connection);
|
||||
}
|
||||
|
||||
public void runQuery(Consumer<SqlSession> consumer) {
|
||||
new Thread(() -> {
|
||||
if (sqlSessionFactory == null) {
|
||||
sqlSessionFactory = createSqlSessionFactory(settings, addMappers);
|
||||
}
|
||||
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
consumer.accept(session);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to run discord query", e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private SqlSessionFactory createSqlSessionFactory(DatabaseSettings settings, AddMappers addMappers) {
|
||||
PooledDataSource dataSource = new PooledDataSource();
|
||||
dataSource.setDriver("com.mysql.cj.jdbc.Driver");
|
||||
dataSource.setUrl(String.format("jdbc:mysql://%s:%d/%s", settings.host(),
|
||||
settings.port(), settings.name()));
|
||||
dataSource.setUsername(settings.username());
|
||||
dataSource.setPassword(settings.password());
|
||||
Environment environment = new Environment("production", new JdbcTransactionFactory(), dataSource);
|
||||
Configuration configuration = new Configuration(environment);
|
||||
addMappers.apply(configuration);
|
||||
|
||||
return new SqlSessionFactoryBuilder().build(configuration);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.alttd.altitudeweb.database;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum Databases {
|
||||
DEFAULT("web_db"),
|
||||
LUCK_PERMS("luckperms");
|
||||
|
||||
private final String internalName;
|
||||
|
||||
Databases(String internalName) {
|
||||
this.internalName = internalName;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
package com.alttd.altitudeweb.database.luckperms;
|
||||
|
||||
public record PlayerGroup(String username, String groupName) {}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.alttd.altitudeweb.database.luckperms;
|
||||
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TeamMemberMapper {
|
||||
@Select("""
|
||||
SELECT players.username, #{groupName} AS group_name
|
||||
FROM luckperms_user_permissions AS permissions
|
||||
INNER JOIN luckperms_players AS players ON players.uuid = permissions.uuid
|
||||
WHERE permission = 'group.'#{groupName}""")
|
||||
List<PlayerGroup> getTeamMembers(String groupName);
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.alttd.altitudeweb.database.web_db;
|
||||
|
||||
public record DatabaseSettings(String host, int port, String name, String username, String password) {
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.alttd.altitudeweb.database.web_db;
|
||||
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
public interface SettingsMapper {
|
||||
@Select("SELECT * FROM database_settings WHERE name = #{database}")
|
||||
DatabaseSettings getSettings(String database);
|
||||
}
|
||||
Reference in New Issue
Block a user