Leaderboards endpoint
This commit is contained in:
parent
39df55b19c
commit
e68de31a6a
@ -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
|
||||
|
26
src/pages/api/leaderboards.ts
Normal file
26
src/pages/api/leaderboards.ts
Normal 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);
|
||||
}
|
||||
}
|
@ -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});
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user