Leaderboards endpoint

This commit is contained in:
Joseph Ferano 2023-04-10 19:37:34 +07:00
parent 39df55b19c
commit e68de31a6a
4 changed files with 49 additions and 13 deletions

View File

@ -146,6 +146,25 @@ begin
end; end;
$$ language plpgsql; $$ 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( create or replace function purchase_item(
p_user_id uuid, p_user_id uuid,
p_store_item_id integer p_store_item_id integer

View File

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

View File

@ -29,19 +29,6 @@ export default async function handler(
return res.status(200).json({stakingEventId: result.rows[0].stake}); 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) { } catch (error) {
return res.status(400).json({ error: error.message}); return res.status(400).json({ error: error.message});
} }

View File

@ -20,6 +20,10 @@ POST http://localhost:3000/api/user/login
GET http://localhost:3000/api/user/:user_id/bank-account GET http://localhost:3000/api/user/:user_id/bank-account
:headers :headers
# Get bank account
GET http://localhost:3000/api/leaderboards
:headers
# Get Staking Sources # Get Staking Sources
GET http://localhost:3000/api/user/:user_id/staking-sources GET http://localhost:3000/api/user/:user_id/staking-sources
:headers :headers