Add API endpoint to retrieve total punishment counts

Introduced a new API endpoint `/history/total` for fetching the total counts of bans, mutes, kicks, and warnings. Added database mapping and DTO classes to support this functionality, along with necessary schema and controller updates.
This commit is contained in:
2025-04-11 23:27:20 +02:00
parent 807569a0a1
commit 44d28494e5
6 changed files with 107 additions and 7 deletions
@@ -1,6 +1,7 @@
package com.alttd.altitudeweb.controllers.history;
import com.alttd.altitudeweb.api.HistoryApi;
import com.alttd.altitudeweb.model.HistoryCountDto;
import com.alttd.altitudeweb.setup.Connection;
import com.alttd.altitudeweb.database.Databases;
import com.alttd.altitudeweb.database.litebans.*;
@@ -88,6 +89,34 @@ public class HistoryApiController implements HistoryApi {
return ResponseEntity.ok().body(playerGroupFuture.join());
}
@Override
public ResponseEntity<HistoryCountDto> getTotalPunishments() {
CompletableFuture<HistoryCount> historyCountCompletableFuture = new CompletableFuture<>();
Connection.getConnection(Databases.LITE_BANS)
.runQuery(sqlSession -> {
log.debug("Loading history count");
try {
HistoryCount punishmentCounts = sqlSession.getMapper(HistoryCountMapper.class)
.getPunishmentCounts();
historyCountCompletableFuture.complete(punishmentCounts);
} catch (Exception e) {
log.error("Failed to load history count", e);
historyCountCompletableFuture.completeExceptionally(e);
}
});
return mapHistoryCount(historyCountCompletableFuture.join());
}
private ResponseEntity<HistoryCountDto> mapHistoryCount(HistoryCount historyCount) {
HistoryCountDto historyCountDto = new HistoryCountDto();
historyCountDto.setBans(historyCount.getBans());
historyCountDto.setMutes(historyCount.getMutes());
historyCountDto.setWarnings(historyCount.getWarnings());
historyCountDto.setKicks(historyCount.getKicks());
return ResponseEntity.ok().body(historyCountDto);
}
private ResponseEntity<PunishmentHistoryDto> mapPunishmentHistory(PunishmentHistoryDto punishmentHistory, CompletableFuture<List<HistoryRecord>> historyRecords) {
historyRecords.join().forEach(historyRecord -> {
PunishmentHistoryInnerDto.TypeEnum type = switch (historyRecord.getType().toLowerCase()) {