Refactor history APIs and integrate LiteBans database support

Redesigned history-related APIs to streamline handling of user and UUID punishments, moving from POST to GET endpoints. Added support for LiteBans database with mappers for retrieving punishment records by name and UUID, and implemented global exception handling for better error reporting. Updated schema paths and added enums (UserType, HistoryType) and a new Gradle dependency.
This commit is contained in:
2025-04-11 01:12:46 +02:00
parent 43a5dafd82
commit 0f9761da3a
12 changed files with 475 additions and 65 deletions
+1
View File
@@ -34,6 +34,7 @@ dependencies {
implementation("org.mybatis:mybatis:3.5.13")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-hateoas")
}
tasks.withType<Test> {
@@ -0,0 +1,19 @@
package com.alttd.altitudeweb.controllers;
import com.alttd.altitudeweb.model.ApiErrorDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiErrorDto> handleException(Exception ex) {
ApiErrorDto error = new ApiErrorDto();
error.reason("Error: " + ex.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
}
}
@@ -1,21 +1,106 @@
package com.alttd.altitudeweb.controllers.history;
import com.alttd.altitudeweb.api.HistoryApi;
import com.alttd.altitudeweb.database.Connection;
import com.alttd.altitudeweb.database.Databases;
import com.alttd.altitudeweb.database.litebans.*;
import com.alttd.altitudeweb.model.PunishmentHistoryDto;
import com.alttd.altitudeweb.model.PunishmentHistoryInnerDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@Slf4j
@RestController
public class HistoryApiController implements HistoryApi {
@Override
public ResponseEntity<Void> getHistoryForUsers(String userType, String type, String user) throws Exception {
return null;
public ResponseEntity<PunishmentHistoryDto> getHistoryForUsers(String userType, String type, String user) throws Exception {
UserType userTypeEnum = UserType.getUserType(userType);
HistoryType historyTypeEnum = HistoryType.getHistoryType(type);
PunishmentHistoryDto punishmentHistory = new PunishmentHistoryDto();
CompletableFuture<List<HistoryRecord>> historyRecords = new CompletableFuture<>();
Connection.getConnection(Databases.LITE_BANS, configuration -> configuration.addMapper(NameHistoryMapper.class))
.thenApply(connection -> {
connection.runQuery(sqlSession -> {
log.debug("Loading user names for type {}", type);
List<HistoryRecord> temp = sqlSession.getMapper(NameHistoryMapper.class)
.getRecent(historyTypeEnum, userTypeEnum, user);
historyRecords.complete(temp);
});
return connection;
});
historyRecords.join().forEach(historyRecord -> {
PunishmentHistoryInnerDto innerDto = new PunishmentHistoryInnerDto()
.uuid(historyRecord.getUuid())
.username(historyRecord.getPunishedName())
.reason(historyRecord.getReason())
.punishmentUserUuid(historyRecord.getBannedByUuid())
.punishmentUser(historyRecord.getBannedByName())
.removedBy(historyRecord.getRemovedByName())
.punishmentTime(historyRecord.getTime())
.expiryTime(historyRecord.getUntil())
.removedReason(historyRecord.getRemovedByReason())
.type(historyRecord.getType());
punishmentHistory.add(innerDto);
});
return ResponseEntity.ok().body(punishmentHistory);
}
@Override
public ResponseEntity<Void> getHistoryForUuid(String userType, String type, String uuid) throws Exception {
return null;
public ResponseEntity<PunishmentHistoryDto> getHistoryForUuid(String userType, String type, String uuid) throws Exception {
UserType userTypeEnum = UserType.getUserType(userType);
HistoryType historyTypeEnum = HistoryType.getHistoryType(type);
PunishmentHistoryDto punishmentHistory = new PunishmentHistoryDto();
CompletableFuture<List<HistoryRecord>> historyRecords = new CompletableFuture<>();
Connection.getConnection(Databases.LITE_BANS, configuration -> configuration.addMapper(UUIDHistoryMapper.class))
.thenApply(connection -> {
connection.runQuery(sqlSession -> {
log.debug("Loading user names for type {}", type);
List<HistoryRecord> temp = sqlSession.getMapper(UUIDHistoryMapper.class)
.getRecent(historyTypeEnum, userTypeEnum, UUID.fromString(uuid));
historyRecords.complete(temp);
});
return connection;
});
historyRecords.join().forEach(historyRecord -> {
PunishmentHistoryInnerDto innerDto = new PunishmentHistoryInnerDto()
.uuid(historyRecord.getUuid())
.username(historyRecord.getPunishedName())
.reason(historyRecord.getReason())
.punishmentUserUuid(historyRecord.getBannedByUuid())
.punishmentUser(historyRecord.getBannedByName())
.removedBy(historyRecord.getRemovedByName())
.punishmentTime(historyRecord.getTime())
.expiryTime(historyRecord.getUntil())
.removedReason(historyRecord.getRemovedByReason())
.type(historyRecord.getType());
punishmentHistory.add(innerDto);
});
return ResponseEntity.ok().body(punishmentHistory);
}
@Override
public ResponseEntity<Void> getUserNames(String userType, String type) throws Exception {
return null;
public ResponseEntity<List<String>> getUserNames(String userType, String type) {
UserType userTypeEnum = UserType.getUserType(userType);
HistoryType historyTypeEnum = HistoryType.getHistoryType(type);
CompletableFuture<List<String>> playerGroupFuture = new CompletableFuture<>();
Connection.getConnection(Databases.LITE_BANS, configuration -> configuration.addMapper(RecentNamesMapper.class))
.thenApply(connection -> {
connection.runQuery(sqlSession -> {
log.debug("Loading user names for type {}", type);
List<String> temp = sqlSession.getMapper(RecentNamesMapper.class)
.getRecent(historyTypeEnum, userTypeEnum);
playerGroupFuture.complete(temp);
});
return connection;
});
return ResponseEntity.ok().body(playerGroupFuture.join());
}
}