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