@@ -3,8 +3,9 @@ import QnHistory, { IQnHistory } from "../models/QnHistory.ts";
33import {
44 MONGO_OBJ_ID_FORMAT ,
55 MONGO_OBJ_ID_MALFORMED_MESSAGE ,
6+ ORDER_INCORRECT_FORMAT_MESSAGE ,
67 PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ,
7- PAGE_LIMIT_USERID_REQUIRED_MESSAGE ,
8+ PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE ,
89 QN_HIST_CREATED_MESSAGE ,
910 QN_HIST_DELETED_MESSAGE ,
1011 QN_HIST_NOT_FOUND_MESSAGE ,
@@ -24,7 +25,9 @@ export const createQnHistory = async (
2425 submissionStatus,
2526 dateAttempted,
2627 timeTaken,
28+ code,
2729 language,
30+ //compilerRes,
2831 } = req . body ;
2932
3033 const newQnHistory = new QnHistory ( {
@@ -34,7 +37,9 @@ export const createQnHistory = async (
3437 submissionStatus,
3538 dateAttempted,
3639 timeTaken,
40+ code,
3741 language,
42+ //compilerRes,
3843 } ) ;
3944
4045 await newQnHistory . save ( ) ;
@@ -109,45 +114,85 @@ type QnHistListParams = {
109114 page : string ;
110115 qnHistLimit : string ;
111116 userId : string ;
117+ title : string ; //qn title search keyword
118+ status : string ; //submission status
119+ order : string ; //entries sort order
112120} ;
113121
114122export const readQnHistoryList = async (
115123 req : Request < unknown , unknown , unknown , QnHistListParams > ,
116124 res : Response
117125) : Promise < void > => {
118126 try {
119- const { page, qnHistLimit, userId } = req . query ;
127+ const { page, qnHistLimit, userId, title , status , order } = req . query ;
120128
121- if ( ! page || ! qnHistLimit || ! userId ) {
122- res . status ( 400 ) . json ( { message : PAGE_LIMIT_USERID_REQUIRED_MESSAGE } ) ;
129+ if ( ! page || ! qnHistLimit || ! userId || ! order ) {
130+ res
131+ . status ( 400 )
132+ . json ( { message : PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE } ) ;
123133 return ;
124134 }
125135
126136 const pageInt = parseInt ( page , 10 ) ;
127137 const qnHistLimitInt = parseInt ( qnHistLimit , 10 ) ;
138+ const orderInt = parseInt ( order , 10 ) ;
128139
129140 if ( pageInt < 1 || qnHistLimitInt < 1 ) {
130141 res . status ( 400 ) . json ( { message : PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE } ) ;
131142 return ;
132143 }
133144
145+ if ( ! ( orderInt == 1 || orderInt == - 1 ) ) {
146+ res . status ( 400 ) . json ( { message : ORDER_INCORRECT_FORMAT_MESSAGE } ) ;
147+ }
148+
134149 if ( ! userId . match ( MONGO_OBJ_ID_FORMAT ) ) {
135150 res . status ( 400 ) . json ( { message : MONGO_OBJ_ID_MALFORMED_MESSAGE } ) ;
136151 return ;
137152 }
138153
139- const filteredQnHistCount = await QnHistory . countDocuments ( {
140- userIds : userId ,
141- } ) ;
142- const filteredQnHist = await QnHistory . find ( { userIds : userId } )
143- . skip ( ( pageInt - 1 ) * qnHistLimitInt )
144- . limit ( qnHistLimitInt ) ;
154+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
155+ const query : any = { } ;
145156
146- res . status ( 200 ) . json ( {
147- message : QN_HIST_RETRIEVED_MESSAGE ,
148- qnHistoryCount : filteredQnHistCount ,
149- qnHistories : filteredQnHist . map ( formatQnHistoryResponse ) ,
150- } ) ;
157+ if ( title ) {
158+ query . title = { $regex : new RegExp ( title , "i" ) } ;
159+ }
160+
161+ if ( status ) {
162+ query . submissionStatus = {
163+ $in : Array . isArray ( status ) ? status : [ status ] ,
164+ } ;
165+ }
166+
167+ query . userIds = { $in : [ userId ] } ;
168+
169+ if ( orderInt == 1 ) {
170+ //ascending order
171+ const filteredQnHistCount = await QnHistory . countDocuments ( query ) ;
172+ const filteredQnHist = await QnHistory . find ( query )
173+ . sort ( { dateAttempted : 1 } )
174+ . skip ( ( pageInt - 1 ) * qnHistLimitInt )
175+ . limit ( qnHistLimitInt ) ;
176+
177+ res . status ( 200 ) . json ( {
178+ message : QN_HIST_RETRIEVED_MESSAGE ,
179+ qnHistoryCount : filteredQnHistCount ,
180+ qnHistories : filteredQnHist . map ( formatQnHistoryResponse ) ,
181+ } ) ;
182+ } else {
183+ //descending order
184+ const filteredQnHistCount = await QnHistory . countDocuments ( query ) ;
185+ const filteredQnHist = await QnHistory . find ( query )
186+ . sort ( { dateAttempted : - 1 } )
187+ . skip ( ( pageInt - 1 ) * qnHistLimitInt )
188+ . limit ( qnHistLimitInt ) ;
189+
190+ res . status ( 200 ) . json ( {
191+ message : QN_HIST_RETRIEVED_MESSAGE ,
192+ qnHistoryCount : filteredQnHistCount ,
193+ qnHistories : filteredQnHist . map ( formatQnHistoryResponse ) ,
194+ } ) ;
195+ }
151196 } catch ( error ) {
152197 res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
153198 }
@@ -191,5 +236,21 @@ const formatQnHistoryResponse = (qnHistory: IQnHistory) => {
191236 timeTaken : qnHistory . timeTaken ,
192237 code : qnHistory . code ,
193238 language : qnHistory . language ,
239+ //compilerRes: qnHistory.compilerRes.map(formatCompilerRes),
194240 } ;
195241} ;
242+
243+ /*const formatCompilerRes = (compilerRes: ICompilerRes) => {
244+ return {
245+ status: compilerRes.status,
246+ exception: compilerRes.exception,
247+ stdout: compilerRes.stdout,
248+ stderr: compilerRes.stderr,
249+ executionTime: compilerRes.executionTime,
250+ stdin: compilerRes.stdin,
251+ stout: compilerRes.stdout,
252+ actualResult: compilerRes.actualResult,
253+ expectedResult: compilerRes.expectedResult,
254+ isMatch: compilerRes.isMatch,
255+ };
256+ };*/
0 commit comments