Skip to content

Commit 0e40193

Browse files
committed
Add update question API in QuestionService
Link frontend form for updating question to QuestionService
1 parent af54f15 commit 0e40193

File tree

9 files changed

+111
-12
lines changed

9 files changed

+111
-12
lines changed

QuestionService/javascript/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ app.get("/", (req, res) => {
1515
});
1616
app.get("/questions", question_controller_1.handleGetQuestions);
1717
app.post("/questions", question_controller_1.handleAddQuestion);
18+
app.put("/questions/:questionId", question_controller_1.handleUpdateQuestion);
1819
app.delete("/questions/:questionId", question_controller_1.handleDeleteQuestion);
1920
app.listen(port, () => {
2021
console.log(`Question Service listening on port ${port}`);

QuestionService/javascript/question/question.controller.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
Object.defineProperty(exports, "__esModule", { value: true });
12-
exports.handleAddQuestion = exports.handleDeleteQuestion = exports.handleGetQuestions = void 0;
12+
exports.handleAddQuestion = exports.handleUpdateQuestion = exports.handleDeleteQuestion = exports.handleGetQuestions = void 0;
1313
const question_service_1 = require("./question.service");
1414
const firestore_1 = require("firebase/firestore");
1515
function handleGetQuestions(req, res) {
@@ -74,6 +74,30 @@ function handleDeleteQuestion(req, res) {
7474
});
7575
}
7676
exports.handleDeleteQuestion = handleDeleteQuestion;
77+
function handleUpdateQuestion(req, res) {
78+
return __awaiter(this, void 0, void 0, function* () {
79+
const questionId = req.params.questionId;
80+
try {
81+
const { title, tags, categories, constraints, difficulty, description, examples, } = req.body;
82+
console.log(`updating question ${questionId}: ${title}`);
83+
const question = yield (0, question_service_1.updateQuestion)(questionId, {
84+
title: title,
85+
tags: tags,
86+
categories: categories,
87+
constraints: constraints,
88+
difficulty: difficulty,
89+
description: description,
90+
examples: examples,
91+
});
92+
res.status(200).send(question);
93+
}
94+
catch (err) {
95+
console.log(err);
96+
res.status(500).send(err);
97+
}
98+
});
99+
}
100+
exports.handleUpdateQuestion = handleUpdateQuestion;
77101
function handleAddQuestion(req, res) {
78102
return __awaiter(this, void 0, void 0, function* () {
79103
try {

QuestionService/javascript/question/question.service.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
Object.defineProperty(exports, "__esModule", { value: true });
12-
exports.addQuestion = exports.deleteQuestion = exports.db = void 0;
12+
exports.addQuestion = exports.updateQuestion = exports.deleteQuestion = exports.db = void 0;
1313
const app_1 = require("firebase/app");
1414
const firestore_1 = require("firebase/firestore");
1515
const firebase_config_1 = require("../firebase/firebase.config");
@@ -28,6 +28,23 @@ function deleteQuestion(questionId) {
2828
});
2929
}
3030
exports.deleteQuestion = deleteQuestion;
31+
function updateQuestion(questionId, question) {
32+
return __awaiter(this, void 0, void 0, function* () {
33+
try {
34+
let questionDoc = question;
35+
const docRef = (0, firestore_1.doc)(exports.db, "questions", questionId);
36+
yield (0, firestore_1.setDoc)(docRef, questionDoc);
37+
for (let i = 0; i < question.examples.length; i++) {
38+
const add = (0, firestore_1.setDoc)((0, firestore_1.doc)(docRef, "examples", (i + 1).toString()), question.examples[i]);
39+
}
40+
return Promise.resolve(question);
41+
}
42+
catch (error) {
43+
return Promise.reject(error);
44+
}
45+
});
46+
}
47+
exports.updateQuestion = updateQuestion;
3148
function addQuestion(question) {
3249
return __awaiter(this, void 0, void 0, function* () {
3350
try {

QuestionService/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import express from "express";
22
import cors from "cors";
33

4-
import { handleGetQuestions, handleAddQuestion, handleDeleteQuestion } from "./question/question.controller";
4+
import { handleGetQuestions, handleAddQuestion, handleUpdateQuestion, handleDeleteQuestion } from "./question/question.controller";
55
const app = express();
66
const port = 3002;
77

@@ -15,6 +15,7 @@ app.get("/", (req, res) => {
1515

1616
app.get("/questions", handleGetQuestions);
1717
app.post("/questions", handleAddQuestion)
18+
app.put("/questions/:questionId", handleUpdateQuestion)
1819
app.delete("/questions/:questionId", handleDeleteQuestion)
1920

2021
app.listen(port, () => {

QuestionService/src/question/question.controller.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response } from "express";
2-
import {addQuestion, db, deleteQuestion} from "./question.service";
2+
import {addQuestion, db, deleteQuestion, updateQuestion} from "./question.service";
33
import {
44
getDocs,
55
collection,
@@ -87,6 +87,35 @@ export async function handleDeleteQuestion(req: Request, res: Response) {
8787
}
8888
}
8989

90+
export async function handleUpdateQuestion(req: Request, res: Response) {
91+
const questionId = req.params.questionId;
92+
try {
93+
const {
94+
title,
95+
tags,
96+
categories,
97+
constraints,
98+
difficulty,
99+
description,
100+
examples,
101+
} = req.body;
102+
console.log(`updating question ${questionId}: ${title}`);
103+
const question = await updateQuestion(questionId,{
104+
title: title,
105+
tags: tags,
106+
categories: categories,
107+
constraints: constraints,
108+
difficulty: difficulty,
109+
description: description,
110+
examples: examples,
111+
});
112+
res.status(200).send(question);
113+
} catch (err) {
114+
console.log(err);
115+
res.status(500).send(err);
116+
}
117+
}
118+
90119
export async function handleAddQuestion(req: Request, res: Response) {
91120
try {
92121
const {

QuestionService/src/question/question.service.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { initializeApp } from "firebase/app";
2-
import {deleteDoc, doc, getFirestore, setDoc} from "firebase/firestore";
2+
import {addDoc, collection, deleteDoc, doc, getFirestore, setDoc} from "firebase/firestore";
33
import { firebaseConfig } from "../firebase/firebase.config"
44

55
initializeApp(firebaseConfig);
@@ -30,10 +30,10 @@ export async function deleteQuestion(questionId: string) {
3030
}
3131
}
3232

33-
export async function addQuestion(question: Question): Promise<Question> {
33+
export async function updateQuestion(questionId: string, question: Question): Promise<Question> {
3434
try {
3535
let questionDoc: Omit<Question, "examples"> = question
36-
const docRef = doc(db, "questions", question.title);
36+
const docRef = doc(db, "questions", questionId);
3737
await setDoc(docRef, questionDoc);
3838
for (let i = 0; i < question.examples.length; i++) {
3939
const add = setDoc(doc(docRef, "examples", (i+1).toString()), question.examples[i]);
@@ -42,4 +42,18 @@ export async function addQuestion(question: Question): Promise<Question> {
4242
} catch (error) {
4343
return Promise.reject(error);
4444
}
45-
}
45+
}
46+
47+
export async function addQuestion(question: Question): Promise<Question> {
48+
try {
49+
let questionDoc: Omit<Question, "examples"> = question
50+
const docRef = await addDoc(collection(db, "questions"), questionDoc);
51+
for (let i = 0; i < question.examples.length; i++) {
52+
const add = setDoc(doc(docRef, "examples", (i+1).toString()), question.examples[i]);
53+
}
54+
return Promise.resolve(question);
55+
} catch (error) {
56+
return Promise.reject(error);
57+
}
58+
}
59+

frontend/src/api/questions/data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ export const getAllQuestions = () => questionhttpClient.get("/questions");
1616
export const addQuestion = (params: QuestionDTO) =>
1717
questionhttpClient.post("/questions", params);
1818

19+
export const updateQuestion = (questionId: string, params: QuestionDTO) =>
20+
questionhttpClient.put(`/questions/${questionId}`, params);
21+
1922
export const deleteQuestion = (questionId: string) =>
2023
questionhttpClient.delete(`/questions/${questionId}`);

frontend/src/components/Questions/EditQuestionTab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const EditQuestionTab: React.FC<EditQuestionPreviewProps> = ({question, onEdit,
2020
}
2121

2222
const handleFinishEdit = (editedQuestion: Question) => {
23-
onEdit(question);
23+
onEdit(editedQuestion);
2424
setEditQuestion(false);
2525
};
2626

frontend/src/components/Questions/EditQuestionsTab.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Box, Button} from "@mui/material";
44
import Grid from "@mui/material/Grid";
55
import Question from "./Question";
66
import {Delete} from "@mui/icons-material";
7-
import {addQuestion, deleteQuestion} from "../../api/questions/data";
7+
import {addQuestion, deleteQuestion, updateQuestion} from "../../api/questions/data";
88
import {AxiosError} from "axios";
99

1010

@@ -18,10 +18,20 @@ const EditQuestionsTab: React.FC = () => {
1818
setEditQuestions(false);
1919
};
2020

21-
const onEdit = (editedQuestion: Question) => {
21+
const onEdit = async (editedQuestion: Question) => {
2222
console.log("Edited Question: ", editedQuestion);
2323

24-
//TODO: Update question in database
24+
try {
25+
await updateQuestion(editedQuestion.id, editedQuestion);
26+
console.log(`Question updated: ${editedQuestion.id}: ${editedQuestion.title}`);
27+
setEditQuestions(false);
28+
} catch (e) {
29+
if (e instanceof AxiosError && e.response) {
30+
console.log(e.response.data.code);
31+
} else if (e instanceof Error) {
32+
console.log(e.message);
33+
}
34+
}
2535
}
2636

2737
const onDelete = async (questionToDelete: Question) => {

0 commit comments

Comments
 (0)