Add nickname retrieval support: implemented NicknameMapper for database queries, updated ChatService to return player names with optional nicknames, and extended /chat/player/{uuid}/name API endpoint.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.alttd.altitudeweb.database.chat;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface NicknameMapper {
|
||||
@Select("""
|
||||
SELECT nickname
|
||||
FROM nicknames
|
||||
WHERE uuid = #{uuid}
|
||||
""")
|
||||
Optional<String> getNicknameFromUUID(@Param("uuid") String uuid);
|
||||
}
|
||||
@@ -144,6 +144,26 @@ public class Connection {
|
||||
}).start();
|
||||
}
|
||||
|
||||
public <T> T runQueryWithResult(QueryFunction<T> function) {
|
||||
if (sqlSessionFactory == null) {
|
||||
sqlSessionFactory = createSqlSessionFactory(settings, addMappers);
|
||||
}
|
||||
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
T result = function.apply(session);
|
||||
session.commit();
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to run query", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface QueryFunction<T> {
|
||||
T apply(SqlSession session);
|
||||
}
|
||||
|
||||
private SqlSessionFactory createSqlSessionFactory(DatabaseSettings settings, AddMappers addMappers) {
|
||||
try {
|
||||
Configuration configuration = getConfiguration(settings);
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.alttd.altitudeweb.setup;
|
||||
|
||||
import com.alttd.altitudeweb.database.Databases;
|
||||
import com.alttd.altitudeweb.database.chat.ChatLogMapper;
|
||||
import com.alttd.altitudeweb.database.chat.NicknameMapper;
|
||||
import com.alttd.altitudeweb.database.votingplugin.VotingPluginUsersMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -12,6 +13,7 @@ public class InitializeChat {
|
||||
log.info("Initializing Chat");
|
||||
Connection.getConnection(Databases.CHAT, (configuration) -> {
|
||||
configuration.addMapper(ChatLogMapper.class);
|
||||
configuration.addMapper(NicknameMapper.class);
|
||||
}).join();
|
||||
log.debug("Initialized Chat");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user