2
2
import express , { Request , Response } from 'express' ;
3
3
import { Collection , ObjectId } from 'mongodb' ;
4
4
import { connectToDB } from '../db/mongoClient' ;
5
- import { Questions } from '../models/types' ;
5
+ import { QuestionsSchema } from '../models/types' ;
6
+ import { z } from 'zod' ;
6
7
7
8
const router = express . Router ( ) ;
9
+ type Questions = z . infer < typeof QuestionsSchema > ;
10
+
8
11
let questionsCollection : Collection < Questions > ;
9
12
10
13
// Middleware to connect to MongoDB and get the collection
@@ -19,7 +22,7 @@ router.use(async (_, res, next) => {
19
22
} ) ;
20
23
21
24
// GET all items
22
- router . get ( '/' , async ( req : Request , res : Response ) => {
25
+ router . get ( '/' , async ( _ , res : Response ) => {
23
26
try {
24
27
const items = await questionsCollection . find ( ) . toArray ( ) ;
25
28
res . status ( 200 ) . json ( items ) ;
@@ -29,18 +32,19 @@ router.get('/', async (req: Request, res: Response) => {
29
32
} ) ;
30
33
31
34
// Update a question
32
- router . put ( '/:questionId' , async ( req : Request , res : Response ) => {
33
- const { questionId } = req . params ;
34
- const updatedQuestion : Questions = req . body ;
35
+ router . put ( '/:id' , async ( req : Request , res : Response ) => {
36
+ const { id } = req . params ;
37
+ const parsedResult = QuestionsSchema . safeParse ( req . body ) ;
38
+ if ( ! parsedResult . success ) {
39
+ return res
40
+ . status ( 400 )
41
+ . json ( { error : 'Invalid question data. Please check your input.' } ) ;
42
+ }
35
43
36
44
try {
37
- if ( ! updatedQuestion || typeof updatedQuestion !== 'object' ) {
38
- return res . status ( 400 ) . json ( { error : 'Invalid question data.' } ) ;
39
- }
40
-
41
45
const result = await questionsCollection . updateOne (
42
- { _id : new ObjectId ( questionId ) } ,
43
- { $set : updatedQuestion } ,
46
+ { _id : new ObjectId ( id ) } ,
47
+ { $set : parsedResult . data } ,
44
48
) ;
45
49
46
50
if ( result . matchedCount === 0 ) {
@@ -49,7 +53,7 @@ router.put('/:questionId', async (req: Request, res: Response) => {
49
53
50
54
res . status ( 200 ) . json ( {
51
55
message : 'Question updated successfully' ,
52
- data : [ { _id : questionId } ] ,
56
+ data : [ { _id : id } ] ,
53
57
} ) ;
54
58
} catch ( error ) {
55
59
console . error ( 'Error updating question:' , error ) ;
@@ -58,54 +62,29 @@ router.put('/:questionId', async (req: Request, res: Response) => {
58
62
} ) ;
59
63
60
64
// POST a new question
61
- router . post ( '/' , async ( req : Request , res : Response ) => {
62
- try {
63
- const newQuestion : Questions = req . body ; // Assume the body contains a question object
64
-
65
- const {
66
- difficulty,
67
- description,
68
- examples,
69
- constraints,
70
- tags,
71
- title_slug,
72
- title,
73
- pictures,
74
- } = newQuestion ;
65
+ router . post ( '/post' , async ( req : Request , res : Response ) => {
66
+ const parseResult = QuestionsSchema . safeParse ( req . body ) ;
75
67
76
- if (
77
- typeof difficulty !== 'number' ||
78
- typeof description !== 'string' ||
79
- ! Array . isArray ( examples ) ||
80
- typeof constraints !== 'string' ||
81
- ! Array . isArray ( tags ) ||
82
- typeof title_slug !== 'string' ||
83
- typeof title !== 'string'
84
- ) {
85
- return res
86
- . status ( 400 )
87
- . json ( { error : 'Invalid question data. Please check your input.' } ) ;
88
- }
89
-
90
- const result = await questionsCollection . insertOne ( newQuestion ) ;
91
-
92
- res . status ( 201 ) . json ( {
93
- message : 'Question inserted successfully' ,
94
- data : [ { _id : result . insertedId } ] ,
95
- } ) ;
68
+ if ( ! parseResult . success ) {
69
+ return res
70
+ . status ( 400 )
71
+ . json ( { error : 'Invalid question data. Please check your input.' } ) ;
72
+ }
73
+ try {
74
+ const result = await questionsCollection . insertOne ( parseResult . data ) ;
75
+ res . status ( 201 ) . json ( result ) ;
96
76
} catch ( error ) {
97
- console . error ( 'Error inserting question:' , error ) ;
98
77
res . status ( 500 ) . json ( { error : 'Failed to insert question' } ) ;
99
78
}
100
79
} ) ;
101
80
102
81
// DELETE a question
103
- router . delete ( '/:questionId ' , async ( req : Request , res : Response ) => {
104
- const { questionId } = req . params ;
82
+ router . delete ( '/:id ' , async ( req : Request , res : Response ) => {
83
+ const { id } = req . params ;
105
84
106
85
try {
107
86
const result = await questionsCollection . deleteOne ( {
108
- _id : new ObjectId ( questionId ) ,
87
+ _id : new ObjectId ( id ) ,
109
88
} ) ;
110
89
111
90
if ( result . deletedCount === 0 ) {
@@ -116,7 +95,7 @@ router.delete('/:questionId', async (req: Request, res: Response) => {
116
95
message : 'Question deleted successfully' ,
117
96
data : [
118
97
{
119
- _id : questionId ,
98
+ _id : id ,
120
99
} ,
121
100
] ,
122
101
} ) ;
0 commit comments