@@ -36,7 +36,13 @@ router.post(
36
36
} ) ;
37
37
}
38
38
39
- const question = { title, description, category, complexity } ;
39
+ const question = {
40
+ title,
41
+ description,
42
+ category,
43
+ complexity,
44
+ deleted : false ,
45
+ } ;
40
46
const newQuestion = new Question ( question ) ;
41
47
await newQuestion . save ( ) ;
42
48
return res . status ( 200 ) . json ( {
@@ -56,7 +62,7 @@ router.post("/all", async (req: Request, res: Response) => {
56
62
const skip = ( pagination - 1 ) * page_size ; // Calculate how many documents to skip
57
63
try {
58
64
const questions = await Question . find (
59
- { } ,
65
+ { deleted : false } ,
60
66
{
61
67
questionid : 1 ,
62
68
title : 1 ,
@@ -71,7 +77,7 @@ router.post("/all", async (req: Request, res: Response) => {
71
77
. limit ( page_size )
72
78
. exec ( ) ;
73
79
74
- const total = await Question . countDocuments ( ) . exec ( ) ;
80
+ const total = await Question . countDocuments ( { deleted : false } ) . exec ( ) ;
75
81
const totalPages = Math . ceil ( total / page_size ) ;
76
82
if ( totalPages < pagination ) pagination = 1 ;
77
83
@@ -96,12 +102,22 @@ router.get("/:id", [...idValidators], async (req: Request, res: Response) => {
96
102
try {
97
103
const question = await Question . findOne (
98
104
{ questionid : questionId } ,
99
- { questionid : 1 , title : 1 , description : 1 , complexity : 1 , category : 1 }
105
+ {
106
+ questionid : 1 ,
107
+ title : 1 ,
108
+ description : 1 ,
109
+ complexity : 1 ,
110
+ category : 1 ,
111
+ deleted : 1 ,
112
+ }
100
113
) . exec ( ) ;
101
- if ( ! question ) {
114
+ if ( ! question || question . deleted ) {
102
115
return res . status ( 404 ) . json ( { message : "Question not found" } ) ;
103
116
}
104
- return res . json ( question ) ;
117
+
118
+ const { deleted, ...responseQuestion } = question . toObject ( ) ;
119
+
120
+ return res . json ( responseQuestion ) ;
105
121
} catch ( error ) {
106
122
return res . status ( 500 ) . send ( "Internal server error" ) ;
107
123
}
@@ -142,6 +158,21 @@ router.post(
142
158
}
143
159
144
160
try {
161
+ const question = await Question . findOne (
162
+ { questionid : questionId } ,
163
+ {
164
+ questionid : 1 ,
165
+ title : 1 ,
166
+ description : 1 ,
167
+ complexity : 1 ,
168
+ category : 1 ,
169
+ deleted : 1 ,
170
+ }
171
+ ) . exec ( ) ;
172
+ if ( ! question || question . deleted ) {
173
+ return res . status ( 404 ) . json ( { message : "Question not found" } ) ;
174
+ }
175
+
145
176
const updatedQuestion = await Question . findOneAndUpdate (
146
177
{ questionid : questionId } ,
147
178
{ $set : updateData } ,
@@ -170,9 +201,11 @@ router.post(
170
201
171
202
const questionId = parseInt ( req . params . id ) ;
172
203
try {
173
- const deletedQuestion = await Question . findOneAndDelete ( {
174
- questionid : questionId ,
175
- } ) . exec ( ) ;
204
+ const deletedQuestion = await Question . findOneAndUpdate (
205
+ { questionid : questionId } ,
206
+ { $set : { deleted : true } } ,
207
+ { new : true }
208
+ ) . exec ( ) ;
176
209
return res . json ( deletedQuestion ) ;
177
210
} catch ( error ) {
178
211
//to catch pre-middleware defined error
0 commit comments