From 999904a49cf91abf2ed88e4d609464b8189c1e55 Mon Sep 17 00:00:00 2001 From: gsd Date: Sun, 29 Oct 2023 17:13:54 +0300 Subject: [PATCH] banlist --- .../app/controllers/admin/BanController.java | 5 +++-- .../app/controllers/user/PublicController.java | 17 ++++++++++++----- src/main/java/app/services/db/BanService.java | 12 +++++------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/app/controllers/admin/BanController.java b/src/main/java/app/controllers/admin/BanController.java index 5635b14..84ca4dc 100644 --- a/src/main/java/app/controllers/admin/BanController.java +++ b/src/main/java/app/controllers/admin/BanController.java @@ -83,8 +83,9 @@ public class BanController { @CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) public ResponseEntity banList( HttpServletRequest request, - @RequestParam(required = false, defaultValue = "10") Integer limit + @RequestParam(required = false, defaultValue = "10") Integer limit, + @RequestParam(required = false, defaultValue = "0") Integer offset ){ - return new ResponseEntity(banService.getLastBans(limit), HttpStatus.OK); + return new ResponseEntity(banService.getLastBans(limit, offset), HttpStatus.OK); } } diff --git a/src/main/java/app/controllers/user/PublicController.java b/src/main/java/app/controllers/user/PublicController.java index 1d6a2d4..4a8d5ff 100644 --- a/src/main/java/app/controllers/user/PublicController.java +++ b/src/main/java/app/controllers/user/PublicController.java @@ -3,7 +3,6 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.WaitAfterNext; -import app.entities.db.Ban; import app.services.db.BanService; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; @@ -11,9 +10,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.util.List; +import java.util.HashMap; @RestController @RequestMapping("api/web") @@ -29,8 +29,15 @@ public class PublicController { @GetMapping("/banlist") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "banlist") - public ResponseEntity> getDiscordIDsBanList( - HttpServletRequest request) { - return new ResponseEntity<>(banService.getLastBans(20), HttpStatus.OK); + public ResponseEntity getBanList( + HttpServletRequest request, + @RequestParam(required = false, defaultValue = "20") Integer limit, + @RequestParam(required = false, defaultValue = "0") Integer offset) { + if (limit > 20) return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); + + return new ResponseEntity<>(new HashMap<>(){{ + put("bans", banService.getLastBans(limit,offset)); + put("count", banService.getBansCount()); + }}, HttpStatus.OK); } } diff --git a/src/main/java/app/services/db/BanService.java b/src/main/java/app/services/db/BanService.java index 16fdbdd..57f1d8e 100644 --- a/src/main/java/app/services/db/BanService.java +++ b/src/main/java/app/services/db/BanService.java @@ -3,17 +3,14 @@ package app.services.db; import app.entities.PlayerProfile; import app.entities.db.Ban; import app.entities.other.SteamID; -import app.services.ProfileService; import app.services.ServerService; import app.utils.CryptedCookie; import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.sql.Timestamp; import java.util.List; @Service @@ -64,10 +61,11 @@ public class BanService { .toList(); } - public List getLastBans(Integer limit) { - return ((List) entityManager.createNativeQuery("SELECT * FROM `light_bans` WHERE `active` = ?1 ORDER BY `light_bans`.`id` DESC LIMIT ?2") + public List getLastBans(Integer limit, Integer offset) { + return ((List) entityManager.createNativeQuery("SELECT * FROM `light_bans` WHERE `active` = ?1 ORDER BY `light_bans`.`id` DESC LIMIT ?2 OFFSET ?3") .setParameter(1, 1) .setParameter(2, limit) + .setParameter(3, offset) .getResultStream().map(obj -> new Ban((Object[]) obj)).toList()).stream() .peek(ban -> ban.cryptIP(cryptedCookie)) .peek(ban -> ban.fillAdmins(permitionService)) @@ -75,8 +73,8 @@ public class BanService { //этот каст конечно пиздец, но он работает } - public Long getBansCount(SteamID steamID) { - return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM light_bans WHERE account_id = ?1 AND active = 0").getSingleResult(); + public Long getBansCount() { + return (Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `light_bans` WHERE active = 1").getSingleResult(); } @Transactional(value = "RwTransactionManager")