Skip to content

Commit 31ae571

Browse files
committed
Link frontend delete question to backend
1 parent 8eed18c commit 31ae571

File tree

6 files changed

+49
-11
lines changed

6 files changed

+49
-11
lines changed

QuestionService/javascript/question/question.controller.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ function handleDeleteQuestion(req, res) {
6363
return __awaiter(this, void 0, void 0, function* () {
6464
const questionId = req.params.questionId;
6565
try {
66-
const docRef = (0, firestore_1.doc)(question_service_1.db, "questions", questionId);
67-
const result = yield (0, firestore_1.deleteDoc)(docRef);
66+
console.log(`deleting question with id ${questionId}`);
67+
yield (0, question_service_1.deleteQuestion)(questionId);
68+
res.status(200).send(`question with id "${questionId}" deleted`);
6869
}
6970
catch (err) {
7071
console.log(`error when deleting question with id ${questionId}` + err);

QuestionService/javascript/question/question.service.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
Object.defineProperty(exports, "__esModule", { value: true });
12-
exports.addQuestion = exports.db = void 0;
12+
exports.addQuestion = 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");
1616
(0, app_1.initializeApp)(firebase_config_1.firebaseConfig);
1717
exports.db = (0, firestore_1.getFirestore)();
18+
function deleteQuestion(questionId) {
19+
return __awaiter(this, void 0, void 0, function* () {
20+
try {
21+
const docRef = (0, firestore_1.doc)(exports.db, "questions", questionId);
22+
yield (0, firestore_1.deleteDoc)(docRef);
23+
return Promise.resolve();
24+
}
25+
catch (error) {
26+
return Promise.reject(error);
27+
}
28+
});
29+
}
30+
exports.deleteQuestion = deleteQuestion;
1831
function addQuestion(question) {
1932
return __awaiter(this, void 0, void 0, function* () {
2033
try {

QuestionService/src/question/question.controller.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response } from "express";
2-
import {addQuestion, db} from "./question.service";
2+
import {addQuestion, db, deleteQuestion} from "./question.service";
33
import {
44
getDocs,
55
collection,
@@ -78,8 +78,9 @@ const getExamples = async (id: string) => {
7878
export async function handleDeleteQuestion(req: Request, res: Response) {
7979
const questionId = req.params.questionId;
8080
try {
81-
const docRef = doc(db, "questions", questionId);
82-
const result = await deleteDoc(docRef);
81+
console.log(`deleting question with id ${questionId}`);
82+
await deleteQuestion(questionId);
83+
res.status(200).send(`question with id "${questionId}" deleted`);
8384
} catch (err) {
8485
console.log(`error when deleting question with id ${questionId}` + err);
8586
res.status(500).send(err);

QuestionService/src/question/question.service.ts

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

55
initializeApp(firebaseConfig);
@@ -20,6 +20,16 @@ interface Example {
2020
image: string;
2121
}
2222

23+
export async function deleteQuestion(questionId: string) {
24+
try {
25+
const docRef = doc(db, "questions", questionId);
26+
await deleteDoc(docRef);
27+
return Promise.resolve();
28+
} catch (error) {
29+
return Promise.reject(error);
30+
}
31+
}
32+
2333
export async function addQuestion(question: Question): Promise<Question> {
2434
try {
2535
let questionDoc: Omit<Question, "examples"> = question

frontend/src/api/questions/data.ts

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

19-
export const deleteQuestion = () =>
20-
questionhttpClient.delete("/questions/:questionId");
19+
export const deleteQuestion = (questionId: string) =>
20+
questionhttpClient.delete(`/questions/${questionId}`);

frontend/src/components/Questions/EditQuestionsTab.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import EditQuestionsTable from "./EditQuestionsTable";
33
import {Box, Button} from "@mui/material";
44
import Grid from "@mui/material/Grid";
55
import Question from "./Question";
6+
import {Delete} from "@mui/icons-material";
7+
import {addQuestion, deleteQuestion} from "../../api/questions/data";
8+
import {AxiosError} from "axios";
69

710

811
const EditQuestionsTab: React.FC = () => {
@@ -21,10 +24,20 @@ const EditQuestionsTab: React.FC = () => {
2124
//TODO: Update question in database
2225
}
2326

24-
const onDelete = (questionToDelete: Question) => {
27+
const onDelete = async (questionToDelete: Question) => {
2528
console.log("Question to Delete: ", questionToDelete);
2629

27-
//TODO: Delete question from database
30+
try {
31+
await deleteQuestion(questionToDelete.title);
32+
console.log("Question deleted: ",questionToDelete.title);
33+
setEditQuestions(false);
34+
} catch (e) {
35+
if (e instanceof AxiosError && e.response) {
36+
console.log(e.response.data.code);
37+
} else if (e instanceof Error) {
38+
console.log(e.message);
39+
}
40+
}
2841
}
2942

3043

0 commit comments

Comments
 (0)