From 3f761b96f36c78a4b3ad8df19a99df52269bac0c Mon Sep 17 00:00:00 2001 From: dedsecrattle Date: Fri, 27 Sep 2024 19:18:32 +0800 Subject: [PATCH 1/2] Bug fix questionId --- .../question-service/src/models/question.ts | 4 ++-- .../src/routes/questionRoutes.ts | 20 +++++++++++++------ .../QuestionDialog/QuestionDialog.tsx | 6 +++--- .../components/QuestionList/QuestionList.tsx | 8 ++++---- frontend/src/constants/mock_questions.ts | 8 ++++---- frontend/src/models/question.model.ts | 2 +- frontend/src/services/question.service.ts | 6 +++--- frontend/src/util/question.helper.ts | 7 +++---- 8 files changed, 34 insertions(+), 27 deletions(-) diff --git a/backend/question-service/src/models/question.ts b/backend/question-service/src/models/question.ts index 7f67404304..eb7519a26f 100644 --- a/backend/question-service/src/models/question.ts +++ b/backend/question-service/src/models/question.ts @@ -1,7 +1,7 @@ import { Schema, model, Document } from "mongoose"; interface IQuestion extends Document { - questionId: string; + questionId: Number; title: string; description: string; categories: string[]; @@ -10,7 +10,7 @@ interface IQuestion extends Document { } const questionSchema = new Schema({ - 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 }, diff --git a/backend/question-service/src/routes/questionRoutes.ts b/backend/question-service/src/routes/questionRoutes.ts index 6a7d44421a..3fcca5f200 100644 --- a/backend/question-service/src/routes/questionRoutes.ts +++ b/backend/question-service/src/routes/questionRoutes.ts @@ -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({ @@ -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) { @@ -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 }); @@ -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 }); diff --git a/frontend/src/components/QuestionDialog/QuestionDialog.tsx b/frontend/src/components/QuestionDialog/QuestionDialog.tsx index ec4c7cf456..2b7075f51d 100644 --- a/frontend/src/components/QuestionDialog/QuestionDialog.tsx +++ b/frontend/src/components/QuestionDialog/QuestionDialog.tsx @@ -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; @@ -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))} /> { // Shared states for adding or editing question const [isAddNew, setIsAddNew] = useState(true); const [isQuestionDialogOpen, setIsQuestionDialogOpen] = useState(false); - const [newQuestionId, setNewQuestionId] = useState(""); + const [newQuestionId, setNewQuestionId] = useState(0); const [newQuestionTitle, setNewQuestionTitle] = useState(""); const [newQuestionDescription, setNewQuestionDescription] = useState(""); const [newQuestionCategories, setNewQuestionCategories] = useState([]); @@ -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 ?? []); @@ -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; } diff --git a/frontend/src/constants/mock_questions.ts b/frontend/src/constants/mock_questions.ts index 6ef0bc7c2c..3967478982 100644 --- a/frontend/src/constants/mock_questions.ts +++ b/frontend/src/constants/mock_questions.ts @@ -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.", @@ -11,7 +11,7 @@ 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"], @@ -19,7 +19,7 @@ export const mockQuestions: Question[] = [ 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.", @@ -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"], diff --git a/frontend/src/models/question.model.ts b/frontend/src/models/question.model.ts index c1c9cb25d4..c86ca93bd8 100644 --- a/frontend/src/models/question.model.ts +++ b/frontend/src/models/question.model.ts @@ -1,7 +1,7 @@ export type QuestionComplexity = "Easy" | "Medium" | "Hard"; export interface Question { - questionId: string; + questionId: number; title: string; description: string; categories: string[]; diff --git a/frontend/src/services/question.service.ts b/frontend/src/services/question.service.ts index 6245309dc3..c5b7ff4518 100644 --- a/frontend/src/services/question.service.ts +++ b/frontend/src/services/question.service.ts @@ -9,7 +9,7 @@ export default class QuestionService { } static async addQuestion( - id: string, + id: number, title: string, description: string, categoriesString: string, @@ -22,7 +22,7 @@ export default class QuestionService { } static async editQuestion( - id: string, + id: number, title: string, description: string, categoriesString: string, @@ -34,7 +34,7 @@ export default class QuestionService { return response.data; } - static async deleteQuestion(id: string): Promise { + static async deleteQuestion(id: number): Promise { const response = await axios.delete(`/api/questions/${id}`); return response.data; } diff --git a/frontend/src/util/question.helper.ts b/frontend/src/util/question.helper.ts index 418d7e1b26..a102e11868 100644 --- a/frontend/src/util/question.helper.ts +++ b/frontend/src/util/question.helper.ts @@ -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 => { @@ -86,7 +85,7 @@ const verifyLink = (link: string): string => { }; export const verifyNewQuestion = ( - id: string, + id: number, title: string, description: string, categoriesString: string, From 07913de4cc8e629425cb17385729f5e6be305ef8 Mon Sep 17 00:00:00 2001 From: dedsecrattle Date: Sat, 28 Sep 2024 01:16:58 +0800 Subject: [PATCH 2/2] Fix page title and favicon --- frontend/public/favicon.ico | Bin 3870 -> 4286 bytes frontend/public/index.html | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/public/favicon.ico b/frontend/public/favicon.ico index a11777cc471a4344702741ab1c8a588998b1311a..f3f76bf10edc6f6aa6cf5ced5383b68483fdd9a8 100644 GIT binary patch literal 4286 zcmeHKYiv|i5T3ef6E&6uskR#Bj|rjL3Z{aH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Jao_Q90R-gl~5 z_xAb2J%eArrC1CN4G$}-zVvCqF1;H;abAu6G*+PDHSYFx@Tdbfox*uEd3}BUyYY-l zTfEsOqsi#f9^FoLO;ChK<554qkri&Av~SIM*{fEYRE?vH7pTAOmu2pz3X?Wn*!ROX ztd54huAk&mFBemMooL33RV-*1f0Q3_(7hl$<#*|WF9P!;r;4_+X~k~uKEqdzZ$5Al zV63XN@)j$FN#cCD;ek1R#l zv%pGrhB~KWgoCj%GT?%{@@o(AJGt*PG#l3i>lhmb_twKH^EYvacVY-6bsCl5*^~L0 zonm@lk2UvvTKr2RS%}T>^~EYqdL1q4nD%0n&Xqr^cK^`J5W;lRRB^R-O8b&HENO||mo0xaD+S=I8RTlIfVgqN@SXDr2&-)we--K7w= zJVU8?Z+7k9dy;s;^gDkQa`0nz6N{T?(A&Iz)2!DEecLyRa&FI!id#5Z7B*O2=PsR0 zEvc|8{NS^)!d)MDX(97Xw}m&kEO@5jqRaDZ!+%`wYOI<23q|&js`&o4xvjP7D_xv@ z5hEwpsp{HezI9!~6O{~)lLR@oF7?J7i>1|5a~UuoN=q&6N}EJPV_GD`&M*v8Y`^2j zKII*d_@Fi$+i*YEW+Hbzn{iQk~yP z>7N{S4)r*!NwQ`(qcN#8SRQsNK6>{)X12nbF`*7#ecO7I)Q$uZsV+xS4E7aUn+U(K baj7?x%VD!5Cxk2YbYLNVeiXvvpMCWYo=by@ diff --git a/frontend/public/index.html b/frontend/public/index.html index d79d7a78ca..34cd06b9d6 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -21,7 +21,7 @@ work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. --> - React App + LuckyJinx