@@ -20,10 +20,17 @@ import {
20
20
MONGO_OBJ_ID_MALFORMED_MESSAGE ,
21
21
} from "../utils/constants.ts" ;
22
22
23
- import { upload } from "../config/multer.ts" ;
23
+ import { upload , uploadTestcaseFiles } from "../config/multer.ts" ;
24
24
import { uploadFileToFirebase } from "../utils/utils" ;
25
25
import { QnListSearchFilterParams , RandomQnCriteria } from "../utils/types.ts" ;
26
26
27
+ const FIREBASE_TESTCASE_FILES_FOLDER_NAME = "testcaseFiles/" ;
28
+
29
+ enum TestcaseFilesUploadRequestTypes {
30
+ CREATE = "create" ,
31
+ UPDATE = "update" ,
32
+ }
33
+
27
34
export const createQuestion = async (
28
35
req : Request ,
29
36
res : Response ,
@@ -34,6 +41,8 @@ export const createQuestion = async (
34
41
description,
35
42
complexity,
36
43
category,
44
+ testcaseInputFileUrl,
45
+ testcaseOutputFileUrl,
37
46
pythonTemplate,
38
47
javaTemplate,
39
48
cTemplate,
@@ -59,6 +68,8 @@ export const createQuestion = async (
59
68
description,
60
69
complexity,
61
70
category,
71
+ testcaseInputFileUrl,
72
+ testcaseOutputFileUrl,
62
73
} ) ;
63
74
64
75
await newQuestion . save ( ) ;
@@ -77,6 +88,7 @@ export const createQuestion = async (
77
88
question : formatQuestionIndivResponse ( newQuestion , newQuestionTemplate ) ,
78
89
} ) ;
79
90
} catch ( error ) {
91
+ console . log ( error ) ;
80
92
res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
81
93
}
82
94
} ;
@@ -110,6 +122,72 @@ export const createImageLink = async (
110
122
} ) ;
111
123
} ;
112
124
125
+ export const createFileLink = async (
126
+ req : Request ,
127
+ res : Response ,
128
+ ) : Promise < void > => {
129
+ uploadTestcaseFiles ( req , res , async ( err ) => {
130
+ if ( err ) {
131
+ return res . status ( 500 ) . json ( {
132
+ message : "Failed to upload testcase files" ,
133
+ error : err . message ,
134
+ } ) ;
135
+ }
136
+
137
+ const isQuestionCreation =
138
+ req . body . requestType === TestcaseFilesUploadRequestTypes . CREATE ;
139
+
140
+ const tcFiles = req . files as {
141
+ testcaseInputFile ?: Express . Multer . File [ ] ;
142
+ testcaseOutputFile ?: Express . Multer . File [ ] ;
143
+ } ;
144
+
145
+ if (
146
+ isQuestionCreation &&
147
+ ( ! tcFiles || ! tcFiles . testcaseInputFile || ! tcFiles . testcaseOutputFile )
148
+ ) {
149
+ return res
150
+ . status ( 400 )
151
+ . json ( { message : "Missing one or both testcase file(s)" } ) ;
152
+ }
153
+
154
+ try {
155
+ const uploadPromises = [ ] ;
156
+
157
+ if ( tcFiles . testcaseInputFile ) {
158
+ const inputFile = tcFiles . testcaseInputFile [ 0 ] as Express . Multer . File ;
159
+ uploadPromises . push (
160
+ uploadFileToFirebase ( inputFile , FIREBASE_TESTCASE_FILES_FOLDER_NAME ) ,
161
+ ) ;
162
+ } else {
163
+ uploadPromises . push ( Promise . resolve ( null ) ) ;
164
+ }
165
+
166
+ if ( tcFiles . testcaseOutputFile ) {
167
+ const outputFile = tcFiles . testcaseOutputFile [ 0 ] as Express . Multer . File ;
168
+ uploadPromises . push (
169
+ uploadFileToFirebase ( outputFile , FIREBASE_TESTCASE_FILES_FOLDER_NAME ) ,
170
+ ) ;
171
+ } else {
172
+ uploadPromises . push ( Promise . resolve ( null ) ) ;
173
+ }
174
+
175
+ const [ tcInputFileUrl , tcOutputFileUrl ] =
176
+ await Promise . all ( uploadPromises ) ;
177
+
178
+ return res . status ( 200 ) . json ( {
179
+ message : "Files uploaded successfully" ,
180
+ urls : {
181
+ testcaseInputFileUrl : tcInputFileUrl || "" ,
182
+ testcaseOutputFileUrl : tcOutputFileUrl || "" ,
183
+ } ,
184
+ } ) ;
185
+ } catch ( error ) {
186
+ return res . status ( 500 ) . json ( { message : "Server error" , error } ) ;
187
+ }
188
+ } ) ;
189
+ } ;
190
+
113
191
export const updateQuestion = async (
114
192
req : Request ,
115
193
res : Response ,
@@ -156,9 +234,9 @@ export const updateQuestion = async (
156
234
const updatedQuestionTemplate = await QuestionTemplate . findOneAndUpdate (
157
235
{ questionId : id } ,
158
236
{
159
- ... ( pythonTemplate !== undefined && { pythonTemplate } ) ,
160
- ... ( javaTemplate !== undefined && { javaTemplate } ) ,
161
- ... ( cTemplate !== undefined && { cTemplate } ) ,
237
+ pythonTemplate,
238
+ javaTemplate,
239
+ cTemplate,
162
240
} ,
163
241
{ new : true } ,
164
242
) ;
@@ -304,9 +382,18 @@ export const readRandomQuestion = async (
304
382
return ;
305
383
}
306
384
385
+ const chosenQuestion = randomQuestion [ 0 ] ;
386
+
387
+ const questionTemplate = await QuestionTemplate . findOne ( {
388
+ questionId : chosenQuestion . _id ,
389
+ } ) ;
390
+
307
391
res . status ( 200 ) . json ( {
308
392
message : QN_RETRIEVED_MESSAGE ,
309
- question : formatQuestionResponse ( randomQuestion [ 0 ] ) ,
393
+ question : formatQuestionIndivResponse (
394
+ chosenQuestion ,
395
+ questionTemplate as IQuestionTemplate ,
396
+ ) ,
310
397
} ) ;
311
398
} catch ( error ) {
312
399
res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
@@ -356,6 +443,8 @@ const formatQuestionIndivResponse = (
356
443
description : question . description ,
357
444
complexity : question . complexity ,
358
445
categories : question . category ,
446
+ testcaseInputFileUrl : question . testcaseInputFileUrl ,
447
+ testcaseOutputFileUrl : question . testcaseOutputFileUrl ,
359
448
pythonTemplate : questionTemplate ? questionTemplate . pythonTemplate : "" ,
360
449
javaTemplate : questionTemplate ? questionTemplate . javaTemplate : "" ,
361
450
cTemplate : questionTemplate ? questionTemplate . cTemplate : "" ,
0 commit comments