Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add topics + Add question duplication check when updating qn #61

Merged
merged 10 commits into from
Nov 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ export const createQuestion = async (req: Request, res: Response): Promise<void>

// Update a question
export const updateQuestion = async (req: Request, res: Response): Promise<void> => {
const { title } = req.body;

try {
// Check if another question with the same title exists (excluding the current question by ID)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

const duplicateQuestion = await Question.findOne({ title, _id: { $ne: req.params.id } });
if (duplicateQuestion) {
res.status(400).json({ message: 'A question with this title already exists' });
return;
}

const updatedQuestion = await Question.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true
Expand Down Expand Up @@ -154,3 +163,40 @@ export const getRandomQuestionByTopicAndDifficultyOld = async (topic: string, di
throw new Error("Failed to retrieve a random question");
}
};

// Get all unique categories
export const getAllCategories = async (req: Request, res: Response): Promise<void> => {
try {
const categories = await Question.distinct('categories');
res.status(200).json(categories);
} catch (error) {
res.status(500).json({ message: 'Error fetching categories', error });
}
};

// Get all unique difficulties
export const getAllDifficulties = async (req: Request, res: Response): Promise<void> => {
try {
const difficulties = await Question.distinct('difficulty');
res.status(200).json(difficulties);
} catch (error) {
res.status(500).json({ message: 'Error fetching difficulties', error });
}
};

// Check if a specific category and difficulty combination exists
export const checkCategoryDifficultyAvailability = async (req: Request, res: Response): Promise<void> => {
const { category, difficulty } = req.query;

try {
const question = await Question.findOne({ categories: category, difficulty: difficulty });
if (question) {
res.status(200).json({ available: true });
} else {
res.status(404).json({ available: false, message: 'No question found for the specified category and difficulty.' });
}
} catch (error) {
res.status(500).json({ message: 'Error checking category and difficulty availability', error });
}
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Request, Response, NextFunction } from 'express';

export const normalizeQuestionData = (req: Request, res: Response, next: NextFunction): void => {
if (req.body) {
if (Array.isArray(req.body.categories)) {
req.body.categories = req.body.categories.map((cat: string) => cat.toLowerCase());
}
if (typeof req.body.difficulty === 'string') {
req.body.difficulty = req.body.difficulty.toLowerCase();
}
}
next();
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Router } from 'express';
import { getQuestions, getQuestionById, createQuestion, updateQuestion, deleteQuestion, getRandomQuestionEndpoint } from '../controllers/questionController';
import { getQuestions, getQuestionById, createQuestion, updateQuestion, deleteQuestion, getRandomQuestionEndpoint, getAllCategories, getAllDifficulties, checkCategoryDifficultyAvailability } from '../controllers/questionController';

const router: Router = Router();

// Define routes for CRUD operations
router.get('/questions/random-question', getRandomQuestionEndpoint);
router.get('/questions', getQuestions);
router.get('/questions/:id', getQuestionById);
router.get('/categories', getAllCategories);
router.get('/difficulties', getAllDifficulties);
router.get('/availability', checkCategoryDifficultyAvailability);
router.post('/questions', createQuestion);
router.put('/questions/:id', updateQuestion);
router.delete('/questions/:id', deleteQuestion);



export default router;
112 changes: 56 additions & 56 deletions peerprep/backend/question-service/src/sampleData.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,6 @@
import { Request, Response } from 'express';
import { createQuestion } from './controllers/questionController';
import Question from './models/questionModel';
/*
{
questionId: 6,
title: 'Implement Stack using Queues',
description: `Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).`,
categories: 'data-structures',
difficulty: 'easy',
},
{
questionId: 7,
title: 'Combine Two Tables',
description: `Given table Person with the following columns: personId, lastName, firstName. And table Address with the following columns: addressId, personId, city, state. Write a solution to report the first name, last name, city, and state of each person.`,
categories: 'Databases',
difficulty: 'easy',
},
{
questionId: 12,
title: 'Rotate Image',
description: `You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).`,
categories: 'Arrays, algorithms',
difficulty: 'medium',
},
{
questionId: 13,
title: 'Airplane Seat Assignment Probability',
description: `n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. After that, the rest of the passengers will: take their own seat if it is still available, or pick other seats randomly if their seat is taken. Return the probability that the nth person gets their own seat.`,
categories: 'Brainteaser',
difficulty: 'medium',
},
{
questionId: 19,
title: 'Chalkboard XOR Game',
description: `You are given an array of integers representing numbers written on a chalkboard. Alice and Bob take turns erasing exactly one number from the chalkboard. Return true if and only if Alice wins the game, assuming both players play optimally.`,
categories: 'Brainteaser',
difficulty: 'hard',
},
{
questionId: 18,
title: 'Wildcard Matching',
description: `Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where:
- '?' matches any single character.
- '*' matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).`,
categories: 'Strings, algorithms',
difficulty: 'hard',
}
*/

const sampleQuestions = [
{
Expand Down Expand Up @@ -83,41 +35,69 @@ const sampleQuestions = [
{
title: 'Add Binary',
description: `Given two binary strings a and b, return their sum as a binary string.`,
categories: 'algorithms',
categories: 'bit-manipulation, algorithms',
difficulty: 'easy',
},
{
title: 'Fibonacci Number',
description: `The Fibonacci numbers, commonly denoted F(n) form a sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1. Given n, calculate F(n).`,
categories: 'algorithms',
categories: 'recursion, algorithms',
difficulty: 'easy',
},
{
title: 'Implement Stack using Queues',
description: `Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).`,
categories: 'data-structures',
difficulty: 'easy',
},
{
title: 'Repeated DNA Sequences',
description: `Given a string s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.`,
categories: 'algorithms, graphs',
description: `The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'.

For example, "ACGAATTCCG" is a DNA sequence. When studying DNA, it is useful to identify repeated sequences within the DNA.

Given a string s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in any order.`,
categories: 'algorithms, bit-manipulation',
difficulty: 'medium',
},
{
title: 'Course Schedule',
description: `There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. Return true if you can finish all courses.`,
description: `There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return true if you can finish all courses. Otherwise, return false.`,
categories: 'data-structures, algorithms',
difficulty: 'medium',
},
{
title: 'LRU Cache Design',
description: `Design and implement an LRU (Least Recently Used) cache.`,
categories: 'data-structures, dynamic-programming',
categories: 'data-structures',
difficulty: 'medium',
},
{
title: 'Longest Common Subsequence',
description: `Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

A subsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters.`,
A subsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters.

For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings.`,
categories: 'strings, algorithms',
difficulty: 'medium',
},
{
title: 'Rotate Image',
description: `You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).`,
categories: 'arrays, algorithms',
difficulty: 'medium',
},
{
title: 'Airplane Seat Assignment Probability',
description: `n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of the passengers will: Take their own seat if it is still available, and Pick other seats randomly when they find their seat occupied

Return the probability that the nth person gets his own seat.`,
categories: 'brainteaser',
difficulty: 'medium',
},
{
title: 'Validate Binary Search Tree',
description: `Given the root of a binary tree, determine if it is a valid binary search tree (BST).`,
Expand All @@ -127,7 +107,7 @@ const sampleQuestions = [
{
title: 'Sliding Window Maximum',
description: `You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position, return the maximum sliding window.`,
categories: 'graphs, algorithms',
categories: 'arrays, algorithms',
difficulty: 'hard',
},
{
Expand All @@ -141,6 +121,26 @@ const sampleQuestions = [
description: `Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection. Design an algorithm to serialize and deserialize a binary tree.`,
categories: 'data-structures, algorithms',
difficulty: 'hard',
},
{
title: 'Wildcard Matching',
description: `Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where:

'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial).`,
categories: 'strings, algorithms',
difficulty: 'hard',
},
{
title: 'Chalkboard XOR Game',
description: `You are given an array of integers nums represents the numbers written on a chalkboard.

Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses. The bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.

Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.

Return true if and only if Alice wins the game, assuming both players play optimally.`,
categories: 'brainteaser',
difficulty: 'hard',
}
];

Expand Down
6 changes: 6 additions & 0 deletions peerprep/backend/question-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import databaseRoutes from './routes/databaseRoutes';
import gptRoutes from './routes/gptRoutes';
import testcaseRoutes from './routes/testcaseRoutes';
import loadSampleData from './sampleData';
import { normalizeQuestionData } from './middleware/normalizationMiddleware';

connectDB() // Initialize MongoDB connection
.then(() => {
Expand Down Expand Up @@ -44,6 +45,11 @@ app.use(cors({
} as CorsOptions));
app.use(express.json());

// Apply normalization middleware to specific routes
// This middleware will normalize `categories` and `difficulty` fields to lowercase
app.use('/api/questions', normalizeQuestionData);


// API routes
app.use('/api', questionRoutes);

Expand Down
26 changes: 22 additions & 4 deletions peerprep/frontend/src/api/questionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Question } from '../models/Question';

const API_URL = 'http://localhost:8080/api/questions';

class ApiError extends Error {
export class ApiError extends Error {
constructor(message: string, public statusCode?: number) {
super(message);
this.name = 'ApiError';
Expand All @@ -13,18 +13,26 @@ class ApiError extends Error {
const handleApiError = (error: unknown): never => {
if (axios.isAxiosError(error)) {
const axiosError = error as AxiosError;

// Explicitly cast response data as an object with a `message` string field if expected
const responseData = axiosError.response?.data as { message?: string } | undefined;

if (axiosError.response) {
if (axiosError.response.status === 400 && responseData?.message?.includes('title already exists')) {
throw new ApiError('A question with this title already exists.', 400);
}
throw new ApiError(`API error: ${axiosError.response.statusText}`, axiosError.response.status);
} else if (axiosError.request) {
throw new ApiError('API error: No response received from the server');
} else {
throw new ApiError(`API error: ${axiosError.message}`);
}
} else {
throw new ApiError(`API error: An unexpected error occurred ${error}`);
throw new ApiError(`API error: An unexpected error occurred ${String(error)}`);
}
};


const validateQuestionData = (data: any): data is Question => {
return (
typeof data === 'object' &&
Expand All @@ -38,6 +46,14 @@ const validateQuestionData = (data: any): data is Question => {
);
};

const normalizeQuestionData = (data: Omit<Question, '_id'>): Omit<Question, '_id'> => {
return {
...data,
categories: data.categories.map(cat => cat.toLowerCase()),
difficulty: data.difficulty.toLowerCase()
};
};

export const fetchQuestions = async (): Promise<Question[]> => {
try {
const response = await axios.get<any[]>(API_URL);
Expand All @@ -53,7 +69,8 @@ export const fetchQuestions = async (): Promise<Question[]> => {

export const createQuestion = async (questionData: Omit<Question, '_id'>): Promise<Question> => {
try {
const response = await axios.post<any>(API_URL, questionData);
const normalizedData = normalizeQuestionData(questionData);
const response = await axios.post<any>(API_URL, normalizedData);
if (!validateQuestionData(response.data)) {
throw new Error('Invalid question data received from server');
}
Expand All @@ -65,7 +82,8 @@ export const createQuestion = async (questionData: Omit<Question, '_id'>): Promi

export const updateQuestion = async (id: string, questionData: Omit<Question, '_id'>): Promise<Question> => {
try {
const response = await axios.put<any>(`${API_URL}/${id}`, questionData);
const normalizedData = normalizeQuestionData(questionData);
const response = await axios.put<any>(`${API_URL}/${id}`, normalizedData);
if (!validateQuestionData(response.data)) {
throw new Error('Invalid question data received from server');
}
Expand Down
Loading
Loading