@@ -3,14 +3,15 @@ import QnHistory, { IQnHistory } from "../models/QnHistory.ts";
3
3
import {
4
4
MONGO_OBJ_ID_FORMAT ,
5
5
MONGO_OBJ_ID_MALFORMED_MESSAGE ,
6
+ ORDER_INCORRECT_FORMAT_MESSAGE ,
6
7
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ,
7
- PAGE_LIMIT_USERID_REQUIRED_MESSAGE ,
8
+ PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE ,
8
9
QN_HIST_CREATED_MESSAGE ,
9
- QN_HIST_DELETED_MESSAGE ,
10
10
QN_HIST_NOT_FOUND_MESSAGE ,
11
11
QN_HIST_RETRIEVED_MESSAGE ,
12
12
SERVER_ERROR_MESSAGE ,
13
13
} from "../utils/constants.ts" ;
14
+ import { QnHistListParams } from "../utils/types.ts" ;
14
15
15
16
export const createQnHistory = async (
16
17
req : Request ,
@@ -24,6 +25,7 @@ export const createQnHistory = async (
24
25
submissionStatus,
25
26
dateAttempted,
26
27
timeTaken,
28
+ code,
27
29
language,
28
30
} = req . body ;
29
31
@@ -34,6 +36,7 @@ export const createQnHistory = async (
34
36
submissionStatus,
35
37
dateAttempted,
36
38
timeTaken,
39
+ code,
37
40
language,
38
41
} ) ;
39
42
@@ -80,74 +83,81 @@ export const updateQnHistory = async (
80
83
}
81
84
} ;
82
85
83
- export const deleteQnHistory = async (
84
- req : Request ,
85
- res : Response
86
- ) : Promise < void > => {
87
- try {
88
- const { id } = req . params ;
89
-
90
- if ( ! id . match ( MONGO_OBJ_ID_FORMAT ) ) {
91
- res . status ( 400 ) . json ( { message : MONGO_OBJ_ID_MALFORMED_MESSAGE } ) ;
92
- return ;
93
- }
94
-
95
- const currQnHistory = await QnHistory . findById ( id ) ;
96
- if ( ! currQnHistory ) {
97
- res . status ( 404 ) . json ( { message : QN_HIST_NOT_FOUND_MESSAGE } ) ;
98
- return ;
99
- }
100
-
101
- await QnHistory . findByIdAndDelete ( id ) ;
102
- res . status ( 200 ) . json ( { message : QN_HIST_DELETED_MESSAGE } ) ;
103
- } catch ( error ) {
104
- res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
105
- }
106
- } ;
107
-
108
- type QnHistListParams = {
109
- page : string ;
110
- qnHistLimit : string ;
111
- userId : string ;
112
- } ;
113
-
114
86
export const readQnHistoryList = async (
115
87
req : Request < unknown , unknown , unknown , QnHistListParams > ,
116
88
res : Response
117
89
) : Promise < void > => {
118
90
try {
119
- const { page, qnHistLimit, userId } = req . query ;
91
+ const { page, qnHistLimit, userId, title , status , order } = req . query ;
120
92
121
- if ( ! page || ! qnHistLimit || ! userId ) {
122
- res . status ( 400 ) . json ( { message : PAGE_LIMIT_USERID_REQUIRED_MESSAGE } ) ;
93
+ if ( ! page || ! qnHistLimit || ! userId || ! order ) {
94
+ res
95
+ . status ( 400 )
96
+ . json ( { message : PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE } ) ;
123
97
return ;
124
98
}
125
99
126
100
const pageInt = parseInt ( page , 10 ) ;
127
101
const qnHistLimitInt = parseInt ( qnHistLimit , 10 ) ;
102
+ const orderInt = parseInt ( order , 10 ) ;
128
103
129
104
if ( pageInt < 1 || qnHistLimitInt < 1 ) {
130
105
res . status ( 400 ) . json ( { message : PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE } ) ;
131
106
return ;
132
107
}
133
108
109
+ if ( orderInt !== 1 && orderInt !== - 1 ) {
110
+ res . status ( 400 ) . json ( { message : ORDER_INCORRECT_FORMAT_MESSAGE } ) ;
111
+ return ;
112
+ }
113
+
134
114
if ( ! userId . match ( MONGO_OBJ_ID_FORMAT ) ) {
135
115
res . status ( 400 ) . json ( { message : MONGO_OBJ_ID_MALFORMED_MESSAGE } ) ;
136
116
return ;
137
117
}
138
118
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 ) ;
119
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
+ const query : any = { } ;
145
121
146
- res . status ( 200 ) . json ( {
147
- message : QN_HIST_RETRIEVED_MESSAGE ,
148
- qnHistoryCount : filteredQnHistCount ,
149
- qnHistories : filteredQnHist . map ( formatQnHistoryResponse ) ,
150
- } ) ;
122
+ if ( title ) {
123
+ query . title = { $regex : new RegExp ( title , "i" ) } ;
124
+ }
125
+
126
+ if ( status ) {
127
+ query . submissionStatus = {
128
+ $in : Array . isArray ( status ) ? status : [ status ] ,
129
+ } ;
130
+ }
131
+
132
+ query . userIds = { $in : [ userId ] } ;
133
+
134
+ if ( orderInt == 1 ) {
135
+ //ascending order
136
+ const filteredQnHistCount = await QnHistory . countDocuments ( query ) ;
137
+ const filteredQnHist = await QnHistory . find ( query )
138
+ . sort ( { dateAttempted : 1 } )
139
+ . skip ( ( pageInt - 1 ) * qnHistLimitInt )
140
+ . limit ( qnHistLimitInt ) ;
141
+
142
+ res . status ( 200 ) . json ( {
143
+ message : QN_HIST_RETRIEVED_MESSAGE ,
144
+ qnHistoryCount : filteredQnHistCount ,
145
+ qnHistories : filteredQnHist . map ( formatQnHistoryResponse ) ,
146
+ } ) ;
147
+ } else {
148
+ //descending order
149
+ const filteredQnHistCount = await QnHistory . countDocuments ( query ) ;
150
+ const filteredQnHist = await QnHistory . find ( query )
151
+ . sort ( { dateAttempted : - 1 } )
152
+ . skip ( ( pageInt - 1 ) * qnHistLimitInt )
153
+ . limit ( qnHistLimitInt ) ;
154
+
155
+ res . status ( 200 ) . json ( {
156
+ message : QN_HIST_RETRIEVED_MESSAGE ,
157
+ qnHistoryCount : filteredQnHistCount ,
158
+ qnHistories : filteredQnHist . map ( formatQnHistoryResponse ) ,
159
+ } ) ;
160
+ }
151
161
} catch ( error ) {
152
162
res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
153
163
}
0 commit comments