-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathadmin-contest.ts
More file actions
121 lines (87 loc) · 2.85 KB
/
Copy pathadmin-contest.ts
File metadata and controls
121 lines (87 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { client } from "db/client";
import { Router } from "express";
import { adminMiddleware } from "../middleware/admin";
import z from "zod"
const router = Router();
// Admin
router.post("/contest", adminMiddleware, async(req, res ) => {
try {
const createContestSchema = z.object({
title: z.string().min(1, "title is missing "),
startTime: z.string().datetime("Invalid datetime format"),
challengeIds: z.array(z.string()).min(1, "one challenge required")
})
const { title, startTime, challengeIds } = createContestSchema.parse(req.body);
const challenges = await client.challenge.findMany({
where: {
id: {
in: challengeIds
}
}
})
if (challenges.length !== challengeIds.length) {
return res.status(404).json({ error: "Challenge not found"});
}
const contest = await client.contest.create({
data: {
title,
startTime: new Date(startTime),
contestToChallengeMapping: {
create: challengeIds.map((challengeId, index ) => ({
challengeId,
index
}))
}
},
include: {
contestToChallengeMapping: {
include: {
challenge: true
}
}
}
})
res.status(201).json({
"msg": "Contest created successfully",
"contest": contest
});
} catch (error) {
res.status(500).json({ error: "Internal server error"});
}
})
//create challenge
router.post("/challenge", adminMiddleware, async(req, res) => {
try {
const createChallengeSchema = z.object({
title: z.string().min(1, "Title is required"),
notionDocId: z.string().min(1, "Notion doc ID is required"),
maxPoints: z.number().int().positive("Max points must be positive")
});
const { title, notionDocId, maxPoints } = createChallengeSchema.parse(req.body);
const challenge = await client.challenge.create({
data: {
title,
notionDocId,
maxPoints
}
})
res.status(201).json({
"msg": "Challenge created successfully",
"challenge": challenge
});
} catch (error) {
res.status(500).json({ error: "Internal server error"});
}
});
// getting all challenges
router.get("/challenges", adminMiddleware, async(req, res) => {
const challengs = await client.challenge.findMany({
orderBy: {
title: "asc"
}
})
res.status(200).json({
"challenges": challengs
});
})
export default router;