1
1
// src/routes/items.ts
2
2
import express , { Request , Response } from 'express' ;
3
- import { Collection } from 'mongodb' ;
3
+ import { Collection , ObjectId } from 'mongodb' ;
4
4
import { connectToDB } from '../db/mongoClient' ;
5
5
import { Questions } from '../models/types' ;
6
6
@@ -39,15 +39,18 @@ router.put('/:questionId', async (req: Request, res: Response) => {
39
39
}
40
40
41
41
const result = await questionsCollection . updateOne (
42
- { _question_id : Number ( questionId ) } ,
43
- { $set : updatedQuestion }
42
+ { _id : new ObjectId ( questionId ) } ,
43
+ { $set : updatedQuestion } ,
44
44
) ;
45
45
46
46
if ( result . matchedCount === 0 ) {
47
47
return res . status ( 404 ) . json ( { error : 'Question not found.' } ) ;
48
48
}
49
49
50
- res . status ( 200 ) . json ( { message : 'Question updated successfully' } ) ;
50
+ res . status ( 200 ) . json ( {
51
+ message : 'Question updated successfully' ,
52
+ data : [ { _id : questionId } ] ,
53
+ } ) ;
51
54
} catch ( error ) {
52
55
console . error ( 'Error updating question:' , error ) ;
53
56
res . status ( 500 ) . json ( { error : 'Failed to update question' } ) ;
@@ -58,11 +61,19 @@ router.put('/:questionId', async (req: Request, res: Response) => {
58
61
router . post ( '/' , async ( req : Request , res : Response ) => {
59
62
try {
60
63
const newQuestion : Questions = req . body ; // Assume the body contains a question object
61
-
62
- const { _question_id, difficulty, description, examples, constraints, tags, title_slug, title, pictures } = newQuestion ;
64
+
65
+ const {
66
+ difficulty,
67
+ description,
68
+ examples,
69
+ constraints,
70
+ tags,
71
+ title_slug,
72
+ title,
73
+ pictures,
74
+ } = newQuestion ;
63
75
64
76
if (
65
- typeof _question_id !== 'number' ||
66
77
typeof difficulty !== 'number' ||
67
78
typeof description !== 'string' ||
68
79
! Array . isArray ( examples ) ||
@@ -71,12 +82,17 @@ router.post('/', async (req: Request, res: Response) => {
71
82
typeof title_slug !== 'string' ||
72
83
typeof title !== 'string'
73
84
) {
74
- return res . status ( 400 ) . json ( { error : 'Invalid question data. Please check your input.' } ) ;
85
+ return res
86
+ . status ( 400 )
87
+ . json ( { error : 'Invalid question data. Please check your input.' } ) ;
75
88
}
76
89
77
90
const result = await questionsCollection . insertOne ( newQuestion ) ;
78
91
79
- res . status ( 201 ) . json ( { message : 'Question inserted successfully' , questionId : result . insertedId } ) ;
92
+ res . status ( 201 ) . json ( {
93
+ message : 'Question inserted successfully' ,
94
+ data : [ { _id : result . insertedId } ] ,
95
+ } ) ;
80
96
} catch ( error ) {
81
97
console . error ( 'Error inserting question:' , error ) ;
82
98
res . status ( 500 ) . json ( { error : 'Failed to insert question' } ) ;
@@ -88,13 +104,22 @@ router.delete('/:questionId', async (req: Request, res: Response) => {
88
104
const { questionId } = req . params ;
89
105
90
106
try {
91
- const result = await questionsCollection . deleteOne ( { _question_id : Number ( questionId ) } ) ;
107
+ const result = await questionsCollection . deleteOne ( {
108
+ _id : new ObjectId ( questionId ) ,
109
+ } ) ;
92
110
93
111
if ( result . deletedCount === 0 ) {
94
112
return res . status ( 404 ) . json ( { error : 'Question not found.' } ) ;
95
113
}
96
114
97
- res . status ( 200 ) . json ( { message : 'Question deleted successfully' } ) ;
115
+ res . status ( 200 ) . json ( {
116
+ message : 'Question deleted successfully' ,
117
+ data : [
118
+ {
119
+ _id : questionId ,
120
+ } ,
121
+ ] ,
122
+ } ) ;
98
123
} catch ( error ) {
99
124
console . error ( 'Error deleting question:' , error ) ;
100
125
res . status ( 500 ) . json ( { error : 'Failed to delete question' } ) ;
0 commit comments