Skip to content

Commit 06ae17e

Browse files
feat: add student history API endpoint
1 parent 7b68dbf commit 06ae17e

2 files changed

Lines changed: 73 additions & 9 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"dev": "node ./server.js"
7+
"start": "node index.js",
8+
"dev": "nodemon index.js"
89
},
910
"repository": {
1011
"type": "git",

server.js

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,100 @@
11
const express = require("express");
22
const cors = require("cors");
3-
const path = require("path");
4-
const fs = require("fs");
3+
54
const app = express();
65
const PORT = process.env.PORT || 3000;
76

87
app.use(cors());
9-
app.use(express.static(path.join(__dirname, "frontend")));
8+
app.use(express.static("frontend"));
109

10+
/* ---------------- HOME ROUTES ---------------- */
1111
app.get("/", (req, res) => {
12-
res.sendFile(path.join(__dirname, "frontend", "index.html"));
12+
res.sendFile(__dirname + "/frontend/index.html");
1313
});
1414

1515
app.get("/leaderboard", (req, res) => {
16-
res.sendFile(path.join(__dirname, "frontend", "leaderboard.html"));
16+
res.sendFile(__dirname + "/frontend/leaderboard.html");
1717
});
1818

1919
app.get("/about", (req, res) => {
20-
res.sendFile(path.join(__dirname, "frontend", "about.html"));
20+
res.sendFile(__dirname + "/frontend/about.html");
2121
});
2222

2323
app.get("/registration", (req, res) => {
24-
res.sendFile(path.join(__dirname, "frontend", "registration.html"));
24+
res.sendFile(__dirname + "/frontend/registration.html");
2525
});
2626

2727
app.get("/uptime", (req, res) => {
2828
res.json({ status: "Website is running ✅" });
2929
});
3030

31+
/* ---------------- API: STUDENT HISTORY ---------------- */
32+
app.get("/api/student/:username", async (req, res) => {
33+
const { username } = req.params;
34+
35+
try {
36+
console.log("Fetching history for:", username);
37+
38+
// 1. Get list of all daily files from GitHub repo
39+
const apiURL =
40+
"https://api.github.com/repos/codepvg/leetcode-ranking-data/contents/daily?ref=main";
41+
42+
const response = await fetch(apiURL);
43+
const files = await response.json();
44+
45+
46+
if (!Array.isArray(files)) {
47+
return res.status(500).json({
48+
error: "GitHub API failed",
49+
details: files.message || files,
50+
});
51+
}
52+
53+
let history = [];
54+
55+
// 2. Loop through each file
56+
for (const file of files) {
57+
if (!file.name.endsWith(".json")) continue;
58+
59+
const fileRes = await fetch(file.download_url);
60+
const data = await fileRes.json();
61+
62+
const user = data.find((u) => u.id === username);
63+
64+
if (user) {
65+
const date = file.name.split("-").slice(0, 3).join("-");
66+
67+
history.push({
68+
date,
69+
easy: user.data.easySolved,
70+
medium: user.data.mediumSolved,
71+
hard: user.data.hardSolved,
72+
});
73+
}
74+
}
75+
76+
// 3. Sort by date
77+
history.sort((a, b) => new Date(a.date) - new Date(b.date));
78+
79+
res.json({
80+
username,
81+
history,
82+
});
83+
} catch (err) {
84+
console.error(err);
85+
res.status(500).json({
86+
error: "Failed to fetch student history",
87+
details: err.message,
88+
});
89+
}
90+
});
91+
92+
/* ---------------- 404 ---------------- */
3193
app.use((req, res) => {
3294
res.status(404).send("Page not found");
3395
});
3496

97+
/* ---------------- START ---------------- */
3598
app.listen(PORT, () => {
3699
console.log(`Server running at http://localhost:${PORT}`);
37-
});
100+
});

0 commit comments

Comments
 (0)