Browse Source

banlist

master
gsd 1 year ago
parent
commit
999904a49c
  1. 5
      src/main/java/app/controllers/admin/BanController.java
  2. 17
      src/main/java/app/controllers/user/PublicController.java
  3. 12
      src/main/java/app/services/db/BanService.java

5
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);
}
}

17
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<List<Ban>> getDiscordIDsBanList(
HttpServletRequest request) {
return new ResponseEntity<>(banService.getLastBans(20), HttpStatus.OK);
public ResponseEntity<HashMap> 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);
}
}

12
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<Ban> getLastBans(Integer limit) {
return ((List<Ban>) entityManager.createNativeQuery("SELECT * FROM `light_bans` WHERE `active` = ?1 ORDER BY `light_bans`.`id` DESC LIMIT ?2")
public List<Ban> getLastBans(Integer limit, Integer offset) {
return ((List<Ban>) 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")

Loading…
Cancel
Save