From 44d28494e5364f7ebd6fabf0d9ec86fb586c5569 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 11 Apr 2025 23:27:20 +0200 Subject: [PATCH] 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. --- .../history/HistoryApiController.java | 29 ++++++++++++ .../database/litebans/HistoryCount.java | 11 +++++ .../database/litebans/HistoryCountMapper.java | 20 ++++++++ .../altitudeweb/setup/InitializeLiteBans.java | 5 +- open_api/src/main/resources/api.yml | 2 + .../src/main/resources/schemas/bans/bans.yml | 47 +++++++++++++++++-- 6 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCount.java create mode 100644 database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCountMapper.java diff --git a/backend/src/main/java/com/alttd/altitudeweb/controllers/history/HistoryApiController.java b/backend/src/main/java/com/alttd/altitudeweb/controllers/history/HistoryApiController.java index 3ffabc3..951932d 100644 --- a/backend/src/main/java/com/alttd/altitudeweb/controllers/history/HistoryApiController.java +++ b/backend/src/main/java/com/alttd/altitudeweb/controllers/history/HistoryApiController.java @@ -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 getTotalPunishments() { + CompletableFuture 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 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 mapPunishmentHistory(PunishmentHistoryDto punishmentHistory, CompletableFuture> historyRecords) { historyRecords.join().forEach(historyRecord -> { PunishmentHistoryInnerDto.TypeEnum type = switch (historyRecord.getType().toLowerCase()) { diff --git a/database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCount.java b/database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCount.java new file mode 100644 index 0000000..d6f91a6 --- /dev/null +++ b/database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCount.java @@ -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; +} diff --git a/database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCountMapper.java b/database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCountMapper.java new file mode 100644 index 0000000..d237a0f --- /dev/null +++ b/database/src/main/java/com/alttd/altitudeweb/database/litebans/HistoryCountMapper.java @@ -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(); +} diff --git a/database/src/main/java/com/alttd/altitudeweb/setup/InitializeLiteBans.java b/database/src/main/java/com/alttd/altitudeweb/setup/InitializeLiteBans.java index 8b3d47b..0dc06b3 100644 --- a/database/src/main/java/com/alttd/altitudeweb/setup/InitializeLiteBans.java +++ b/database/src/main/java/com/alttd/altitudeweb/setup/InitializeLiteBans.java @@ -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); diff --git a/open_api/src/main/resources/api.yml b/open_api/src/main/resources/api.yml index 7d49844..bb23678 100644 --- a/open_api/src/main/resources/api.yml +++ b/open_api/src/main/resources/api.yml @@ -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' diff --git a/open_api/src/main/resources/schemas/bans/bans.yml b/open_api/src/main/resources/schemas/bans/bans.yml index 3bc1a3c..774a9b2 100644 --- a/open_api/src/main/resources/schemas/bans/bans.yml +++ b/open_api/src/main/resources/schemas/bans/bans.yml @@ -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