Skip to content

Commit b65a6ba

Browse files
feat(api): add routes for quiz and user
1 parent eaecb39 commit b65a6ba

29 files changed

+371
-7745
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
# Keep environment variables out of version control
33
.env
4+
/generated
5+

api/package-lock.json

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"author": "CUNY Tech Prep",
1616
"license": "MIT",
1717
"dependencies": {
18+
"@prisma/client": "^6.7.0",
1819
"express": "^5.0.1"
1920
},
2021
"devDependencies": {

api/src/index.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import express, { Request, Response } from "express";
2+
import { PrismaClient } from '@prisma/client'
23

4+
const prisma = new PrismaClient()
35
const app = express();
46
const port = process.env.PORT;
57

@@ -11,6 +13,78 @@ app.get("/api/test", (_req: Request, res: Response) => {
1113
});
1214
});
1315

16+
//User Routes
17+
app.get("/api/users", async (_req: Request, res: Response) => {
18+
try {
19+
const users = await prisma.post.findMany();
20+
res.json(users)
21+
} catch (error) {
22+
res.status(500).json({ error: "Failed to fetch users" });
23+
}
24+
})
25+
26+
app.post("/api/users", async (req: Request, res: Response) => {
27+
const { email, name, password, role } = req.body;
28+
try {
29+
const user = await prisma.pots.create({
30+
data: {
31+
email,
32+
name,
33+
role
34+
},
35+
})
36+
res.json(user)
37+
} catch (error) {
38+
res.status(500).json({ error: "Failed to create user" });
39+
}
40+
})
41+
42+
//Quiz Routes
43+
app.get("/api/quizzes", async (_req: Request, res: Response) => {
44+
try {
45+
const quizzes = await prisma.quiz.findMany();
46+
res.json(quizzes);
47+
} catch (error) {
48+
res.status(500).json({ error: "Failed to fetch quizzes" });
49+
}
50+
});
51+
52+
app.get("/api/users/:userId/quizzes", async (req: Request, res: Response) => {
53+
const userId = parseInt(req.params.userId);
54+
try {
55+
const userQuizzes = await prisma.userCompletion.findMany({
56+
where: { userId },
57+
include: { quiz: true },
58+
});
59+
res.json(userQuizzes);
60+
} catch (error) {
61+
res.status(500).json({ error: `Failed to fetch quizzes for user ID ${userId}` });
62+
}
63+
});
64+
65+
app.post("/api/quizzes", async (req: Request, res: Response) => {
66+
const { name, topicId, numQuestions } = req.body;
67+
try {
68+
const newQuiz = await prisma.quiz.create({
69+
data: {
70+
name,
71+
topicId,
72+
numQuestions,
73+
},
74+
});
75+
res.json(newQuiz);
76+
} catch (error) {
77+
res.status(500).json({ error: "Failed to create quiz" });
78+
}
79+
});
80+
1481
app.listen(port, () => {
1582
console.log(`server started on port: ${port}`);
1683
});
84+
85+
//Table of Questions
86+
87+
//String question
88+
//Array of answers
89+
//Question name
90+
//Function to turn answers into a storabke format

generated/prisma/client.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

generated/prisma/client.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

generated/prisma/default.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

generated/prisma/default.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

generated/prisma/edge.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

generated/prisma/edge.js

Lines changed: 0 additions & 186 deletions
This file was deleted.

0 commit comments

Comments
 (0)