From e68de31a6a3d6698eaf786c85054c96994d0d227 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 10 Apr 2023 19:37:34 +0700 Subject: [PATCH] Leaderboards endpoint --- sql/procedures.sql | 19 +++++++++++++++ src/pages/api/leaderboards.ts | 26 +++++++++++++++++++++ src/pages/api/user/[userId]/stakes/start.ts | 13 ----------- test-endpoints.restclient | 4 ++++ 4 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 src/pages/api/leaderboards.ts diff --git a/sql/procedures.sql b/sql/procedures.sql index 9185c6e..733b3c9 100644 --- a/sql/procedures.sql +++ b/sql/procedures.sql @@ -146,6 +146,25 @@ begin end; $$ language plpgsql; +create or replace function get_leaderboard(top_n integer) +returns table ( + id uuid, + name varchar, + wallet varchar, + balance integer +) +as $$ +begin + return query + select users.id, users.name, users.wallet, bank_account.balance + from users + join bank_account on bank_account.user_id = users.id + order by balance desc + limit top_n; +end; +$$ language plpgsql; + + create or replace function purchase_item( p_user_id uuid, p_store_item_id integer diff --git a/src/pages/api/leaderboards.ts b/src/pages/api/leaderboards.ts new file mode 100644 index 0000000..9a80d95 --- /dev/null +++ b/src/pages/api/leaderboards.ts @@ -0,0 +1,26 @@ +import { postgresConnection } from "db"; +import type { NextApiRequest, NextApiResponse } from "next"; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + try { + if (req.method === "GET") { + let { limit } = req.query; + + const db = postgresConnection; + if (limit == undefined || limit == 0) { + limit = 10; + } + const result = await db.query("select * from get_leaderboard($1)", [limit]); + if (result.rowCount > 0) { + return res.status(200).json(result.rows); + } else { + return res.status(404).json({ message: "User not found" }); + } + } + } catch (error) { + res.status(500).json(error); + } +} diff --git a/src/pages/api/user/[userId]/stakes/start.ts b/src/pages/api/user/[userId]/stakes/start.ts index f2bf540..ad90d27 100644 --- a/src/pages/api/user/[userId]/stakes/start.ts +++ b/src/pages/api/user/[userId]/stakes/start.ts @@ -29,19 +29,6 @@ export default async function handler( return res.status(200).json({stakingEventId: result.rows[0].stake}); - // if (invItem.stakeId != undefined) { - // const timeRemaining = calculateRemainingTime(invItem.stakeTime, invItem.duration_in_mins); - // if (timeRemaining > 0) { - // return res.status(400).json({error: `Item is still in use ${timeRemaining}`}); - // } - // } - - // const boost = invItem.tier > 0 ? item.upgrades[invItem.tier - 1].claimBoost : 0; - // const totalClaim = item.claimAmount + boost; - - // await db.run(`INSERT INTO staking_event(user_id, well_id, inventory_item_id, duration_in_mins, stake_amount) - // VALUES (?,?,?,?,?)`, - // [userId, well.id, invItem.id, item.completionTimeInMins, totalClaim]); } catch (error) { return res.status(400).json({ error: error.message}); } diff --git a/test-endpoints.restclient b/test-endpoints.restclient index a2d4c9d..4df6d31 100644 --- a/test-endpoints.restclient +++ b/test-endpoints.restclient @@ -20,6 +20,10 @@ POST http://localhost:3000/api/user/login GET http://localhost:3000/api/user/:user_id/bank-account :headers +# Get bank account +GET http://localhost:3000/api/leaderboards +:headers + # Get Staking Sources GET http://localhost:3000/api/user/:user_id/staking-sources :headers