summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-05-16 17:26:53 +0500
committerilotterytea <iltsu@alright.party>2025-05-16 17:26:53 +0500
commit3a37adb613e7e0ba674833082811ff802bb9960f (patch)
tree8ee0bcc9ac65ab8f725402c9d7a30f63b7b629ea
parent89da0f12c59768bce39371b12a181706df982999 (diff)
feat: get user badges (for tinyrino)
-rw-r--r--public/badges.php50
1 files changed, 50 insertions, 0 deletions
diff --git a/public/badges.php b/public/badges.php
new file mode 100644
index 0000000..c4444b1
--- /dev/null
+++ b/public/badges.php
@@ -0,0 +1,50 @@
+<?php
+include_once "../src/utils.php";
+include_once "../src/config.php";
+include_once "../src/user.php";
+
+$db = new PDO(DB_URL, DB_USER, DB_PASS);
+
+$stmt = $db->prepare("SELECT
+ u.id, u.username,
+ r.name AS role_name,
+ r.badge_id AS role_badge_id,
+ ub.badge_id AS custom_badge_id,
+ co.alias_id AS connection_alias_id,
+ co.platform AS connection_platform
+ FROM users u
+ JOIN role_assigns ra ON ra.user_id = u.id
+ JOIN roles r ON r.id = ra.role_id
+ LEFT JOIN user_badges ub ON ub.user_id = u.id
+ LEFT JOIN connections co ON co.user_id = u.id
+ WHERE r.badge_id IS NOT NULL OR ub.badge_id IS NOT NULL
+");
+$stmt->execute();
+
+$rows = $stmt->fetchAll();
+
+$badges = [];
+
+foreach ($rows as $row) {
+ $badge = [
+ "id" => $row["id"],
+ "username" => $row["username"],
+ "role" => Role::from_array($row),
+ "custom_badge" => Badge::from_array($row, "custom"),
+ "connection" => match (isset($row["connection_alias_id"], $row["connection_platform"])) {
+ true => [
+ "alias_id" => $row["connection_alias_id"],
+ "platform" => $row["connection_platform"]
+ ],
+ false => null
+ }
+ ];
+
+ array_push($badges, $badge);
+}
+
+json_response([
+ "status_code" => 200,
+ "message" => null,
+ "data" => $badges
+]); \ No newline at end of file