Skip to content

Commit b26a7b2

Browse files
committed
add tag route
1 parent cf5318d commit b26a7b2

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

peerprep-fe/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
"@radix-ui/react-scroll-area": "^1.2.0",
2020
"@radix-ui/react-select": "^2.1.1",
2121
"@radix-ui/react-slot": "^1.1.0",
22+
"@types/js-cookie": "^3.0.6",
2223
"axios": "^1.7.7",
2324
"class-variance-authority": "^0.7.0",
2425
"clsx": "^2.1.1",
2526
"cmdk": "1.0.0",
27+
"js-cookie": "^3.0.5",
2628
"lucide-react": "^0.441.0",
2729
"next": "14.2.12",
2830
"nuqs": "^1.19.3",

peerprep-fe/pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

question-service/src/routes/questionsController.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// src/routes/items.ts
21
import express, { Request, Response } from 'express';
32
import { Collection, ObjectId } from 'mongodb';
43
import { connectToDB } from '../db/mongoClient';
@@ -21,20 +20,27 @@ router.use(async (_, res, next) => {
2120
}
2221
});
2322

24-
// GET all items
23+
// GET all items with filters
2524
router.get('/', async (req: Request, res: Response) => {
2625
try {
27-
const { difficulty, status, topics } = req.query;
26+
const { difficulty, status, topics, search } = req.query;
2827

29-
let query = {};
28+
let query: any = {};
3029
if (difficulty) {
31-
query = { ...query, difficulty: parseInt(difficulty as string) };
30+
query.difficulty = parseInt(difficulty as string);
3231
}
3332
if (status) {
34-
query = { ...query, status: status as string };
33+
query.status = status as string;
3534
}
36-
if (topics) {
37-
query = { ...query, topics: topics as string[] };
35+
if (topics && typeof topics === 'string') {
36+
const topicsArray = topics.split(',');
37+
query.tags = { $in: topicsArray };
38+
}
39+
if (search && typeof search === 'string') {
40+
query.$or = [
41+
{ title: { $regex: search, $options: 'i' } }, // only search in title
42+
// { description: { $regex: search, $options: 'i' } },
43+
];
3844
}
3945

4046
const items = await questionsCollection.find(query).toArray();
@@ -142,4 +148,23 @@ router.delete('/:id', async (req: Request, res: Response) => {
142148
}
143149
});
144150

151+
// GET all unique tags
152+
router.get('/tags', async (_: Request, res: Response) => {
153+
try {
154+
const uniqueTags = await questionsCollection
155+
.aggregate([
156+
{ $unwind: '$tags' },
157+
{ $group: { _id: '$tags' } },
158+
{ $project: { _id: 0, tag: '$_id' } },
159+
])
160+
.toArray();
161+
162+
const tags = uniqueTags.map((item) => item.tag);
163+
res.status(200).json(tags);
164+
} catch (error) {
165+
console.error('Error fetching tags:', error);
166+
res.status(500).json({ error: 'Failed to fetch tags' });
167+
}
168+
});
169+
145170
export default router;

0 commit comments

Comments
 (0)