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:
Teriuihi 2025-04-11 23:27:20 +02:00
parent 807569a0a1
commit 44d28494e5
6 changed files with 107 additions and 7 deletions

View File

@ -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()) {

View File

@ -0,0 +1,11 @@
package com.alttd.altitudeweb.database.litebans;
import lombok.Data;
@Data
public class HistoryCount {
private int bans;
private int mutes;
private int warnings;
private int kicks;
}

View File

@ -0,0 +1,20 @@
package com.alttd.altitudeweb.database.litebans;
import org.apache.ibatis.annotations.Select;
public interface HistoryCountMapper {
/**
* Gets the total count of punishments from all LiteBans tables.
*
* @return A PunishmentCount object containing counts from each table
*/
@Select({"""
SELECT
(SELECT COUNT(*) FROM litebans_bans) AS bans,
(SELECT COUNT(*) FROM litebans_mutes) AS mutes,
(SELECT COUNT(*) FROM litebans_warnings) AS warnings,
(SELECT COUNT(*) FROM litebans_kicks) AS kicks
"""
})
HistoryCount getPunishmentCounts();
}

View File

@ -1,9 +1,7 @@
package com.alttd.altitudeweb.setup;
import com.alttd.altitudeweb.database.Databases;
import com.alttd.altitudeweb.database.litebans.NameHistoryMapper;
import com.alttd.altitudeweb.database.litebans.RecentNamesMapper;
import com.alttd.altitudeweb.database.litebans.UUIDHistoryMapper;
import com.alttd.altitudeweb.database.litebans.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
@ -19,6 +17,7 @@ public class InitializeLiteBans {
configuration.addMapper(RecentNamesMapper.class);
configuration.addMapper(NameHistoryMapper.class);
configuration.addMapper(UUIDHistoryMapper.class);
configuration.addMapper(HistoryCountMapper.class);
}).join()
.runQuery(sqlSession -> {
createAllPunishmentsView(sqlSession);

View File

@ -22,3 +22,5 @@ paths:
$ref: './schemas/bans/bans.yml#/getHistoryForAll'
/history/{userType}/uuid/{type}/{uuid}/{page}:
$ref: './schemas/bans/bans.yml#/getHistoryForUuid'
/history/total:
$ref: './schemas/bans/bans.yml#/getTotalPunishments'

View File

@ -23,7 +23,7 @@ getUserNames:
content:
application/json:
schema:
$ref: "../generic/errors.yml#/components/schemas/ApiError"
$ref: '../generic/errors.yml#/components/schemas/ApiError'
getHistoryForUsers:
get:
tags:
@ -50,7 +50,7 @@ getHistoryForUsers:
content:
application/json:
schema:
$ref: "../generic/errors.yml#/components/schemas/ApiError"
$ref: '../generic/errors.yml#/components/schemas/ApiError'
getHistoryForAll:
get:
tags:
@ -76,7 +76,7 @@ getHistoryForAll:
content:
application/json:
schema:
$ref: "../generic/errors.yml#/components/schemas/ApiError"
$ref: '../generic/errors.yml#/components/schemas/ApiError'
getHistoryForUuid:
get:
tags:
@ -103,7 +103,27 @@ getHistoryForUuid:
content:
application/json:
schema:
$ref: "../generic/errors.yml#/components/schemas/ApiError"
$ref: '../generic/errors.yml#/components/schemas/ApiError'
getTotalPunishments:
get:
tags:
- history
summary: Get total history count per type
description: Retrieves the total count of punishments for each type available (ban, mute, kick, warn, or all).
operationId: getTotalPunishments
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/HistoryCount'
default:
description: Unexpected error
content:
application/json:
schema:
$ref: '../generic/errors.yml#/components/schemas/ApiError'
components:
parameters:
HistoryType:
@ -205,3 +225,22 @@ components:
required:
- name
- uuid
HistoryCount:
type: object
properties:
bans:
type: integer
description: The total amounts of bans
example: 10528
mutes:
type: integer
description: The total amounts of mutes
example: 2350
warnings:
type: integer
description: The total amounts of warnings
example: 3881
kicks:
type: integer
description: The total amounts of kicks
example: 1769