Skip to content

Commit

Permalink
Bug fix questionId
Browse files Browse the repository at this point in the history
  • Loading branch information
dedsecrattle committed Sep 27, 2024
1 parent 802b73f commit 3f761b9
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 27 deletions.
4 changes: 2 additions & 2 deletions backend/question-service/src/models/question.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema, model, Document } from "mongoose";

interface IQuestion extends Document {
questionId: string;
questionId: Number;
title: string;
description: string;
categories: string[];
Expand All @@ -10,7 +10,7 @@ interface IQuestion extends Document {
}

const questionSchema = new Schema<IQuestion>({
questionId: { type: String, required: true, unique: true },
questionId: { type: Number, required: true, unique: true },
title: { type: String, required: true },
description: { type: String, required: true },
categories: { type: [String], required: true },
Expand Down
20 changes: 14 additions & 6 deletions backend/question-service/src/routes/questionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const router: Router = Router();

// Create a new question (POST)
router.post("/", async (req: Request, res: Response) => {
const { questionId, title, description, categories, complexity, link } = req.body;
const { questionId, title, description, categories, complexity, link } =
req.body;

try {
const newQuestion = new Question({
Expand Down Expand Up @@ -36,7 +37,10 @@ router.get("/", async (req: Request, res: Response) => {
// Get a single question by ID (GET)
router.get("/:id", async (req: Request, res: Response) => {
try {
const question = await Question.findOne({ questionId: req.params.id });
const id = Number(req.params.id);
const question = await Question.findOne({
questionId: id,
});
if (!question) return res.status(404).json({ error: "Question not found" });
res.json(question);
} catch (err) {
Expand All @@ -49,12 +53,14 @@ router.put("/:id", async (req: Request, res: Response) => {
const { title, description, categories, complexity } = req.body;

try {
const id = Number(req.params.id);
const updatedQuestion = await Question.findOneAndUpdate(
{ questionId: req.params.id },
{ questionId: id },
{ title, description, categories, complexity },
{ new: true }
);
if (!updatedQuestion) return res.status(404).json({ error: "Question not found" });
if (!updatedQuestion)
return res.status(404).json({ error: "Question not found" });
res.json(updatedQuestion);
} catch (err) {
res.status(500).json({ error: (err as Error).message });
Expand All @@ -64,10 +70,12 @@ router.put("/:id", async (req: Request, res: Response) => {
// Delete a question by ID (DELETE)
router.delete("/:id", async (req: Request, res: Response) => {
try {
const id = Number(req.params.id);
const deletedQuestion = await Question.findOneAndDelete({
questionId: req.params.id,
questionId: id,
});
if (!deletedQuestion) return res.status(404).json({ error: "Question not found" });
if (!deletedQuestion)
return res.status(404).json({ error: "Question not found" });
res.json({ message: "Question deleted successfully" });
} catch (err) {
res.status(500).json({ error: (err as Error).message });
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/QuestionDialog/QuestionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import {
const QuestionDialog = (props: {
isAddNew: boolean;
isOpen: boolean;
id: string;
id: number;
title: string;
description: string;
categories: string[];
complexity: QuestionComplexity;
link: string;
setIsOpen: (isOpen: boolean) => void;
setId: (id: string) => void;
setId: (id: number) => void;
setMainDialogTitle: (title: string) => void;
setDescription: (description: string) => void;
setCategories: (categories: string[]) => void;
Expand Down Expand Up @@ -103,7 +103,7 @@ const QuestionDialog = (props: {
label="ID"
type="text"
value={id}
onChange={(e) => setId(e.target.value)}
onChange={(e) => setId(Number(e.target.value))}
/>
<TextField
className="QuestionDialog-input QuestionDialog-input-title"
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/QuestionList/QuestionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const QuestionList = (): ReactElement => {
// Shared states for adding or editing question
const [isAddNew, setIsAddNew] = useState<boolean>(true);
const [isQuestionDialogOpen, setIsQuestionDialogOpen] = useState<boolean>(false);
const [newQuestionId, setNewQuestionId] = useState<string>("");
const [newQuestionId, setNewQuestionId] = useState<number>(0);
const [newQuestionTitle, setNewQuestionTitle] = useState<string>("");
const [newQuestionDescription, setNewQuestionDescription] = useState<string>("");
const [newQuestionCategories, setNewQuestionCategories] = useState<string[]>([]);
Expand All @@ -35,7 +35,7 @@ const QuestionList = (): ReactElement => {

const openQuestionDialog = (question: Question | null) => {
setIsAddNew(!question);
setNewQuestionId(question?.questionId ?? "");
setNewQuestionId(question?.questionId ?? 0);
setNewQuestionTitle(question?.title ?? "");
setNewQuestionDescription(question?.description ?? "");
setNewQuestionCategories(question?.categories ?? []);
Expand Down Expand Up @@ -103,8 +103,8 @@ const QuestionList = (): ReactElement => {

const sortedQuestions = [...questions].sort((a, b) => {
if (key === "questionId") {
const aId = parseInt(a[key]);
const bId = parseInt(b[key]);
const aId = a[key];
const bId = b[key];
return order === "asc" ? aId - bId : bId - aId;
}

Expand Down
8 changes: 4 additions & 4 deletions frontend/src/constants/mock_questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Question } from "../models/question.model";

export const mockQuestions: Question[] = [
{
questionId: "1",
questionId: 1,
title: "What is a Binary Search Tree?",
description:
"A binary search tree is a data structure that facilitates fast lookup, addition, and removal of items.",
Expand All @@ -11,15 +11,15 @@ export const mockQuestions: Question[] = [
link: "",
},
{
questionId: "2",
questionId: 2,
title: "Explain the concept of closures in JavaScript",
description: "Closures are a way to access variables defined outsquestionIde of a function's scope.",
categories: ["JavaScript", "Functional Programming"],
complexity: "Hard",
link: "",
},
{
questionId: "3",
questionId: 3,
title: "What is polymorphism in OOP?",
description:
"Polymorphism is the ability of different objects to respond in different ways to the same method call.",
Expand All @@ -28,7 +28,7 @@ export const mockQuestions: Question[] = [
link: "",
},
{
questionId: "4",
questionId: 4,
title: "Reverse a String",
description: "Write a function that reverses a string. The input string is given as an array of characters.",
categories: ["Strings", "Algorithms"],
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/models/question.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type QuestionComplexity = "Easy" | "Medium" | "Hard";

export interface Question {
questionId: string;
questionId: number;
title: string;
description: string;
categories: string[];
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/services/question.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class QuestionService {
}

static async addQuestion(
id: string,
id: number,
title: string,
description: string,
categoriesString: string,
Expand All @@ -22,7 +22,7 @@ export default class QuestionService {
}

static async editQuestion(
id: string,
id: number,
title: string,
description: string,
categoriesString: string,
Expand All @@ -34,7 +34,7 @@ export default class QuestionService {
return response.data;
}

static async deleteQuestion(id: string): Promise<any> {
static async deleteQuestion(id: number): Promise<any> {
const response = await axios.delete(`/api/questions/${id}`);
return response.data;
}
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/util/question.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ const verifyLength = (value: string, field: QuestionValidationFields): void => {
}
};

const verifyId = (id: string): string => {
verifyLength(id, "id");
return id.trim();
const verifyId = (id: number): number => {
return id;
};

const verifyTitle = (title: string): string => {
Expand Down Expand Up @@ -86,7 +85,7 @@ const verifyLink = (link: string): string => {
};

export const verifyNewQuestion = (
id: string,
id: number,
title: string,
description: string,
categoriesString: string,
Expand Down

0 comments on commit 3f761b9

Please sign in to comment.