Skip to content

Commit 4e96605

Browse files
committed
add filters and sorting for qnhistory
1 parent ed7c611 commit 4e96605

File tree

9 files changed

+244
-38
lines changed

9 files changed

+244
-38
lines changed

backend/qn-history-service/src/controllers/questionHistoryController.ts

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import QnHistory, { IQnHistory } from "../models/QnHistory.ts";
33
import {
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

114122
export 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+
};*/

backend/qn-history-service/src/utils/constants.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export const SERVER_ERROR_MESSAGE = "Server error.";
99
export const QN_HIST_RETRIEVED_MESSAGE =
1010
"Question history retrieved successfully.";
1111

12-
export const PAGE_LIMIT_USERID_REQUIRED_MESSAGE =
13-
"Page number, question limit per page and userId should be provided.";
12+
export const PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE =
13+
"Page number, question history limit per page, userId and sort order should be provided.";
1414

1515
export const PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE =
16-
"Page number and question limit per page should be positive integers.";
16+
"Page number and question history limit per page should be positive integers.";
17+
18+
export const ORDER_INCORRECT_FORMAT_MESSAGE = "Order should only be -1 or 1.";
1719

1820
export const MONGO_OBJ_ID_FORMAT = /^[0-9a-fA-F]{24}$/;
1921

backend/qn-history-service/swagger.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ paths:
167167
type: string
168168
required: true
169169
description: User id of user to retrieve question histories
170+
- in: query
171+
name: title
172+
type: string
173+
required: false
174+
description: Search keywords for question history title
175+
- in: query
176+
name: status
177+
type: string
178+
required: false
179+
description: Filter for question history submission status
180+
- in: query
181+
name: order
182+
type: integer
183+
required: true
184+
description: Order (based on date attempted) to sort question history records by (1 for ascending and -1 for descending).
170185
responses:
171186
200:
172187
description: Successful Response

backend/qn-history-service/tests/qnHistoryRoutes.spec.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import app from "../src/app";
44
import {
55
MONGO_OBJ_ID_MALFORMED_MESSAGE,
66
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
7-
PAGE_LIMIT_USERID_REQUIRED_MESSAGE,
7+
PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE,
88
QN_HIST_NOT_FOUND_MESSAGE,
99
} from "../src/utils/constants";
1010
import QnHistory from "../src/models/QnHistory";
@@ -54,53 +54,55 @@ describe("Qn History Routes", () => {
5454
it("Reads existing question histories", async () => {
5555
const qnHistLimit = 10;
5656
const res = await request.get(
57-
`${BASE_URL}?page=1&qnHistLimit=${qnHistLimit}&userId=66f77e9f27ab3f794bdae664`
57+
`${BASE_URL}?page=1&qnHistLimit=${qnHistLimit}&userId=66f77e9f27ab3f794bdae664&order=1`
5858
);
5959
expect(res.status).toBe(200);
6060
expect(res.body.qnHistories.length).toBeLessThanOrEqual(qnHistLimit);
6161
});
6262

6363
it("Does not read without page", async () => {
6464
const res = await request.get(
65-
`${BASE_URL}?qnHistLimit=10&userId=66f77e9f27ab3f794bdae664`
65+
`${BASE_URL}?qnHistLimit=10&userId=66f77e9f27ab3f794bdae664&order=1`
6666
);
6767
expect(res.status).toBe(400);
68-
expect(res.body.message).toBe(PAGE_LIMIT_USERID_REQUIRED_MESSAGE);
68+
expect(res.body.message).toBe(PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE);
6969
});
7070

7171
it("Does not read without qnHistLimit", async () => {
7272
const res = await request.get(
73-
`${BASE_URL}?page=1&userId=66f77e9f27ab3f794bdae664`
73+
`${BASE_URL}?page=1&userId=66f77e9f27ab3f794bdae664&order=1`
7474
);
7575
expect(res.status).toBe(400);
76-
expect(res.body.message).toBe(PAGE_LIMIT_USERID_REQUIRED_MESSAGE);
76+
expect(res.body.message).toBe(PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE);
7777
});
7878

7979
it("Does not read without userId", async () => {
80-
const res = await request.get(`${BASE_URL}?page=1&qnHistLimit=10`);
80+
const res = await request.get(
81+
`${BASE_URL}?page=1&qnHistLimit=10&order=1`
82+
);
8183
expect(res.status).toBe(400);
82-
expect(res.body.message).toBe(PAGE_LIMIT_USERID_REQUIRED_MESSAGE);
84+
expect(res.body.message).toBe(PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE);
8385
});
8486

8587
it("Does not read with negative page", async () => {
8688
const res = await request.get(
87-
`${BASE_URL}?page=-1&qnHistLimit=10&userId=66f77e9f27ab3f794bdae664`
89+
`${BASE_URL}?page=-1&qnHistLimit=10&userId=66f77e9f27ab3f794bdae664&order=1`
8890
);
8991
expect(res.status).toBe(400);
9092
expect(res.body.message).toBe(PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE);
9193
});
9294

9395
it("Does not read with negative qnHistLimit", async () => {
9496
const res = await request.get(
95-
`${BASE_URL}?page=1&qnHistLimit=-10&userId=66f77e9f27ab3f794bdae664`
97+
`${BASE_URL}?page=1&qnHistLimit=-10&userId=66f77e9f27ab3f794bdae664&order=1`
9698
);
9799
expect(res.status).toBe(400);
98100
expect(res.body.message).toBe(PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE);
99101
});
100102

101103
it("Does not read with invalid userId format", async () => {
102104
const res = await request.get(
103-
`${BASE_URL}?page=1&qnHistLimit=10&userId=6`
105+
`${BASE_URL}?page=1&qnHistLimit=10&userId=6&order=1`
104106
);
105107
expect(res.status).toBe(400);
106108
expect(res.body.message).toBe(MONGO_OBJ_ID_MALFORMED_MESSAGE);

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ const CollabSandbox: React.FC = () => {
8181
return;
8282
}
8383
getQuestionById(questionId, dispatch);
84-
setCompilerResult([]);
8584

8685
resetCollab();
8786

0 commit comments

Comments
 (0)