Skip to content

Commit

Permalink
Add sorting for title and complexity and retreiving unique topics
Browse files Browse the repository at this point in the history
  • Loading branch information
Jweng88 committed Sep 23, 2024
1 parent c557077 commit 93ccdc3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
72 changes: 72 additions & 0 deletions question-service/src/controller/question-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const questionController = {
// testCase,
// testCaseInput,
// testCaseOutput,
sort,
} = req.query;

// Set default values for pagination
Expand Down Expand Up @@ -125,12 +126,52 @@ export const questionController = {
// .skip(skip)
// .limit(Number(limit));

// // Default sorting
// let sortOptions: any = {};

// // Sorting by title
// if (sort === "title" || sort === "-title") {
// sortOptions.title = sort === "title" ? 1 : -1; // Ascending for 'title', descending for '-title'
// }

// // Sorting by complexity using numerical values for custom ordering
// if (sort === "complexity" || sort === "-complexity") {
// const complexityOrder = { Easy: 1, Medium: 2, Hard: 3 };
// sortOptions.complexity =
// sort === "complexity"
// ? complexityOrder
// : { Easy: -1, Medium: -2, Hard: -3 };
// }

// Fetch only the fields you need: question_id, title, category, complexity
const questions = await Question.find(filter)
.select("question_id title category complexity") // Specify fields to fetch
// .sort(sortOptions) // Apply sorting
.skip(skip)
.limit(limit);

// Define complexity order for sorting
const complexityOrder: { [key: string]: number } = {
Easy: 1,
Medium: 2,
Hard: 3,
};

// Apply sorting in the application layer
if (sort === "complexity") {
questions.sort((a: any, b: any) => {
return complexityOrder[a.complexity] - complexityOrder[b.complexity];
});
} else if (sort === "-complexity") {
questions.sort((a: any, b: any) => {
return complexityOrder[b.complexity] - complexityOrder[a.complexity];
});
} else if (sort === "title") {
questions.sort((a: any, b: any) => a.title.localeCompare(b.title)); // Ascending
} else if (sort === "-title") {
questions.sort((a: any, b: any) => b.title.localeCompare(a.title)); // Descending
}

// Count total number of documents matching the filter
const totalQuestions = await Question.countDocuments(filter);
res.status(200).json({
Expand Down Expand Up @@ -255,4 +296,35 @@ export const questionController = {
.json({ message: "Failed to delete question", error: err });
}
},

// Get all unique categories (topics)
getAllUniqueCategories: async (req: Request, res: Response) => {
try {
// Use MongoDB's distinct to retrieve unique category values
const uniqueCategories = await Question.distinct("category");

res.status(200).json({ uniqueCategories });
} catch (err) {
res
.status(500)
.json({ message: "Failed to get unique categories", error: err });
}
},

// Get all unique complexity levels
getAllUniqueComplexityLevels: async (req: Request, res: Response) => {
try {
// Use MongoDB's distinct to retrieve unique complexity values
const uniqueComplexityLevels = await Question.distinct("complexity");

res.status(200).json({ uniqueComplexityLevels });
} catch (err) {
res
.status(500)
.json({
message: "Failed to get unique complexity levels",
error: err,
});
}
},
};
6 changes: 6 additions & 0 deletions question-service/src/routes/question-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ router.delete(
questionController.deleteQuestion
);

router.get("/categories/unique", questionController.getAllUniqueCategories);
router.get(
"/complexity/unique",
questionController.getAllUniqueComplexityLevels
);

export default router;

0 comments on commit 93ccdc3

Please sign in to comment.