Skip to content

Commit 460e487

Browse files
committed
works
1 parent 1573e76 commit 460e487

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

backend/question/src/controller/question-controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export const getRandomQuestion = async (req: Request, res: Response): Promise<Re
7878
const result = await getRandomQuestionService(payload);
7979
return res.status(result.code).json(result);
8080
} catch (error) {
81+
console.log('error', error);
82+
8183
return res
8284
.status(StatusCodes.INTERNAL_SERVER_ERROR)
8385
.json({ success: false, message: 'An error occurred', error });

backend/question/src/routes/question.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ router.get('/:questionId', getQuestionDetails);
2020

2121
router.post('/random', getRandomQuestion);
2222

23-
router.post('/questions', createQuestion);
24-
router.put('/questions/:questionId', updateQuestion);
25-
router.delete('/questions/:questionId', deleteQuestion);
23+
router.post('/create', createQuestion);
24+
router.put('/:questionId', updateQuestion);
25+
router.delete('/:questionId', deleteQuestion);
2626

2727
export default router;

backend/question/src/services/get/index.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { and, arrayOverlaps, eq, ilike, notInArray, sql } from 'drizzle-orm';
1+
import { and, arrayOverlaps, eq, ilike, inArray, not, sql } from 'drizzle-orm';
22

33
import { db } from '@/lib/db/index';
44
import { questions } from '@/lib/db/schema';
@@ -92,31 +92,45 @@ export const getQuestionDetailsService = async (
9292
export const getRandomQuestionService = async (
9393
payload: IGetRandomQuestionPayload
9494
): Promise<IGetRandomQuestionResponse> => {
95-
const { attemptedQuestions, difficulty, topic } = payload;
95+
const { difficulty, topic, attemptedQuestions } = payload;
9696
const whereClause = [];
9797

98+
console.log('Starting query construction');
99+
98100
if (difficulty) {
101+
console.log(`Adding difficulty filter: ${difficulty}`);
99102
whereClause.push(eq(questions.difficulty, difficulty));
100103
}
101104

102-
if (topic && topic.length > 0) {
103-
whereClause.push(arrayOverlaps(questions.topic, topic));
105+
const topicArray = (Array.isArray(topic) ? topic : [topic]).filter(
106+
(t): t is string => t !== undefined
107+
);
108+
if (topicArray.length > 0) {
109+
whereClause.push(arrayOverlaps(questions.topic, topicArray));
104110
}
105111

106112
if (attemptedQuestions && attemptedQuestions.length > 0) {
107-
whereClause.push(notInArray(questions.id, attemptedQuestions));
113+
console.log(`Excluding attempted questions: ${attemptedQuestions.join(', ')}`);
114+
whereClause.push(not(inArray(questions.id, attemptedQuestions)));
108115
}
109116

110-
// randomize the order of questions
111-
const query = db
112-
.select()
113-
.from(questions)
114-
.where(and(...whereClause))
115-
.orderBy(sql`RANDOM()`)
116-
.limit(1);
117+
console.log(`Where clause conditions: ${whereClause.length}`);
118+
119+
let query = db.select().from(questions);
120+
121+
if (whereClause.length > 0) {
122+
query = query.where(and(...whereClause)) as typeof query;
123+
}
124+
125+
query = (query as any).orderBy(sql`RANDOM()`).limit(1);
126+
127+
console.log('Executing query');
128+
console.log(query.toSQL()); // This will log the SQL query
117129

118130
const result = await query;
119131

132+
console.log(`Query result: ${JSON.stringify(result)}`);
133+
120134
if (result.length === 0) {
121135
return {
122136
code: StatusCodes.NOT_FOUND,
@@ -126,7 +140,6 @@ export const getRandomQuestionService = async (
126140
},
127141
};
128142
}
129-
130143
return {
131144
code: StatusCodes.OK,
132145
data: { question: result[0] },

0 commit comments

Comments
 (0)