diff --git a/src/main/java/app/controllers/discord/DiscordController.java b/src/main/java/app/controllers/discord/DiscordController.java index 0da3155..0a3114d 100644 --- a/src/main/java/app/controllers/discord/DiscordController.java +++ b/src/main/java/app/controllers/discord/DiscordController.java @@ -25,7 +25,18 @@ public class DiscordController { @CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) public ResponseEntity getSteamID(HttpServletRequest request, @RequestParam String discord_id) { - return new ResponseEntity(discordAuthService.getSteamIDofDiscordID(discord_id), HttpStatus.OK); + SteamID steamID = discordAuthService.getSteamIDofDiscordID(discord_id); + if (steamID != null) return new ResponseEntity(steamID, HttpStatus.OK); + else return new ResponseEntity(HttpStatus.NOT_FOUND); + } + + @GetMapping("/steam") + @CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) + public ResponseEntity getDiscordID(HttpServletRequest request, + @RequestParam String steam64) { + String discord_id = discordAuthService.getDiscordIDofSteamID(SteamIDConverter.getSteamID(steam64)); + if (discord_id != null) return new ResponseEntity(discord_id, HttpStatus.OK); + else return new ResponseEntity<>(HttpStatus.NOT_FOUND); } @PostMapping diff --git a/src/main/java/app/controllers/user/ProfileController.java b/src/main/java/app/controllers/user/ProfileController.java index 9700160..e2085e6 100644 --- a/src/main/java/app/controllers/user/ProfileController.java +++ b/src/main/java/app/controllers/user/ProfileController.java @@ -45,7 +45,7 @@ public class ProfileController { @CheckWebAccess public ResponseEntity GetFreeVIP(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String steam64, - SocialAuth socialAuth) { + @RequestBody(required = false) SocialAuth socialAuth) { //return new ResponseEntity<>(freeVIPService.) //1.get social //2.get steam2discord diff --git a/src/main/java/app/entities/SocialAuth.java b/src/main/java/app/entities/SocialAuth.java index 789e221..911c603 100644 --- a/src/main/java/app/entities/SocialAuth.java +++ b/src/main/java/app/entities/SocialAuth.java @@ -6,4 +6,8 @@ import lombok.Data; public class SocialAuth { protected long vk_id = 0; protected long discord_id = 0; + + public boolean haveAny() { + return vk_id != 0 || discord_id != 0; + } } diff --git a/src/main/java/app/services/db/DiscordAuthService.java b/src/main/java/app/services/db/DiscordAuthService.java index 37dc582..e2e8e07 100644 --- a/src/main/java/app/services/db/DiscordAuthService.java +++ b/src/main/java/app/services/db/DiscordAuthService.java @@ -21,6 +21,15 @@ public class DiscordAuthService { .orElse(null); } + public String getDiscordIDofSteamID(SteamID steamID) { + return (String) entityManager.createNativeQuery("SELECT discord_id FROM steam2discord WHERE steam_id = ?1 AND active = 1 LIMIT 1") + .setParameter(1, steamID.steam2) + .getResultStream() + .map(String::valueOf) + .findFirst() + .orElse(null); + } + @Transactional public boolean setSteamIDofDiscordID(SteamID steamID, String discord_id) { return entityManager.createNativeQuery("INSERT INTO `steam2discord` (`id`, `steam_id`, `discord_id`, `timestamp`, `active`) VALUES (NULL, ?1, ?2, current_timestamp(), '1')") diff --git a/src/main/java/app/services/db/FreeVIPService.java b/src/main/java/app/services/db/FreeVIPService.java index 19b97c9..9746cb0 100644 --- a/src/main/java/app/services/db/FreeVIPService.java +++ b/src/main/java/app/services/db/FreeVIPService.java @@ -9,6 +9,7 @@ import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestTemplate; import java.math.BigDecimal; @@ -67,6 +68,16 @@ public class FreeVIPService { } } + @Transactional + public boolean setGivedFreeVip(SteamID steamID, SocialAuth socialAuth) { + if (!socialAuth.haveAny()) return false; + return entityManager.createNativeQuery("INSERT INTO `free_vip` (`id`, `steam3`, `vk_id`, `discord_id`, `date`) VALUES (NULL, ?1, ?2, ?3, current_timestamp())") + .setParameter(1, steamID.steam2) + .setParameter(2, socialAuth.getVk_id()) + .setParameter(3, socialAuth.getDiscord_id()) + .executeUpdate() > 0; + } + public Long addFreeVIP(SteamID steamID, SocialAuth socialAuth) { /* 0 - права уже есть @@ -102,6 +113,7 @@ public class FreeVIPService { //4.недостаточно наиграл return -1 * (need_usertime - total_usertime.longValue()); } + setGivedFreeVip(steamID, socialAuth); permitionService.addFreeVIP(steamID, 86400); publishWebhook(steamID, 86400, "Free"); return 1L;