Skip to content

Commit 3ced66d

Browse files
authored
Merge pull request #65 from CS3219-AY2324S1/add-question-form
Add question form
2 parents 61499b0 + e708b89 commit 3ced66d

23 files changed

+1017
-58
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ npm-debug.log*
4040
yarn-debug.log*
4141
yarn-error.log*
4242

43-
# IDE files
43+
# IDE metadata
4444
/*/.idea/

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: 39 additions & 16 deletions
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) {
@@ -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);
@@ -73,23 +74,45 @@ function handleDeleteQuestion(req, res) {
7374
});
7475
}
7576
exports.handleDeleteQuestion = handleDeleteQuestion;
76-
function handleAddQuestion(req, res) {
77+
function handleUpdateQuestion(req, res) {
7778
return __awaiter(this, void 0, void 0, function* () {
79+
const questionId = req.params.questionId;
7880
try {
79-
const { qtitle, qtags, qcategories, qconstraints, qdifficulty, qdescription, qexamples, } = req.body;
80-
const docRef = yield (0, firestore_1.addDoc)((0, firestore_1.collection)(question_service_1.db, "questions"), {
81-
title: qtitle,
82-
tags: qtags,
83-
categories: qcategories,
84-
constraints: qconstraints,
85-
difficulty: qdifficulty,
86-
description: qdescription,
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,
8791
});
88-
const exampleRef = (0, firestore_1.collection)(docRef, "examples");
89-
qexamples.map((e) => {
90-
const add = (0, firestore_1.addDoc)(exampleRef, e);
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;
101+
function handleAddQuestion(req, res) {
102+
return __awaiter(this, void 0, void 0, function* () {
103+
try {
104+
const { title, tags, categories, constraints, difficulty, description, examples, } = req.body;
105+
console.log(`adding question ${title}`);
106+
const question = yield (0, question_service_1.addQuestion)({
107+
title: title,
108+
tags: tags,
109+
categories: categories,
110+
constraints: constraints,
111+
difficulty: difficulty,
112+
description: description,
113+
examples: examples,
91114
});
92-
// const addExample = setDoc(exampleRef, qexamples)
115+
res.status(200).send(question);
93116
}
94117
catch (err) {
95118
console.log(err);
Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
11
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
211
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.db = void 0;
12+
exports.addQuestion = exports.updateQuestion = exports.deleteQuestion = exports.db = void 0;
413
const app_1 = require("firebase/app");
514
const firestore_1 = require("firebase/firestore");
615
const firebase_config_1 = require("../firebase/firebase.config");
716
(0, app_1.initializeApp)(firebase_config_1.firebaseConfig);
817
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;
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;
48+
function addQuestion(question) {
49+
return __awaiter(this, void 0, void 0, function* () {
50+
try {
51+
let questionDoc = question;
52+
const docRef = (0, firestore_1.doc)(exports.db, "questions", question.title);
53+
yield (0, firestore_1.setDoc)(docRef, questionDoc);
54+
for (let i = 0; i < question.examples.length; i++) {
55+
const add = (0, firestore_1.setDoc)((0, firestore_1.doc)(docRef, "examples", (i + 1).toString()), question.examples[i]);
56+
}
57+
return Promise.resolve(question);
58+
}
59+
catch (error) {
60+
return Promise.reject(error);
61+
}
62+
});
63+
}
64+
exports.addQuestion = addQuestion;

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: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import { Request, Response } from "express";
2-
import { db } from "./question.service";
2+
import {addQuestion, db, deleteQuestion, updateQuestion} from "./question.service";
33
import {
44
getDocs,
55
collection,
6-
deleteDoc,
7-
doc,
8-
addDoc,
9-
setDoc,
106
} from "firebase/firestore";
117

8+
interface Question {
9+
title: string;
10+
tags: string[];
11+
categories: string[];
12+
constraints: string[];
13+
difficulty: string;
14+
description: string;
15+
examples: Example[];
16+
}
17+
1218
interface Example {
1319
text: string;
1420
image: string;
@@ -68,38 +74,66 @@ const getExamples = async (id: string) => {
6874
export async function handleDeleteQuestion(req: Request, res: Response) {
6975
const questionId = req.params.questionId;
7076
try {
71-
const docRef = doc(db, "questions", questionId);
72-
const result = await deleteDoc(docRef);
77+
console.log(`deleting question with id ${questionId}`);
78+
await deleteQuestion(questionId);
79+
res.status(200).send(`question with id "${questionId}" deleted`);
7380
} catch (err) {
7481
console.log(`error when deleting question with id ${questionId}` + err);
7582
res.status(500).send(err);
7683
}
7784
}
7885

79-
export async function handleAddQuestion(req: Request, res: Response) {
86+
export async function handleUpdateQuestion(req: Request, res: Response) {
87+
const questionId = req.params.questionId;
8088
try {
8189
const {
82-
qtitle,
83-
qtags,
84-
qcategories,
85-
qconstraints,
86-
qdifficulty,
87-
qdescription,
88-
qexamples,
90+
title,
91+
tags,
92+
categories,
93+
constraints,
94+
difficulty,
95+
description,
96+
examples,
8997
} = req.body;
90-
const docRef = await addDoc(collection(db, "questions"), {
91-
title: qtitle,
92-
tags: qtags,
93-
categories: qcategories,
94-
constraints: qconstraints,
95-
difficulty: qdifficulty,
96-
description: qdescription,
98+
console.log(`updating question ${questionId}: ${title}`);
99+
const question = await updateQuestion(questionId,{
100+
title: title,
101+
tags: tags,
102+
categories: categories,
103+
constraints: constraints,
104+
difficulty: difficulty,
105+
description: description,
106+
examples: examples,
97107
});
98-
const exampleRef = collection(docRef, "examples");
99-
qexamples.map((e: Example) => {
100-
const add = addDoc(exampleRef, e);
108+
res.status(200).send(question);
109+
} catch (err) {
110+
console.log(err);
111+
res.status(500).send(err);
112+
}
113+
}
114+
115+
export async function handleAddQuestion(req: Request, res: Response) {
116+
try {
117+
const {
118+
title,
119+
tags,
120+
categories,
121+
constraints,
122+
difficulty,
123+
description,
124+
examples,
125+
} = req.body;
126+
console.log(`adding question ${title}`);
127+
const question = await addQuestion({
128+
title: title,
129+
tags: tags,
130+
categories: categories,
131+
constraints: constraints,
132+
difficulty: difficulty,
133+
description: description,
134+
examples: examples,
101135
});
102-
// const addExample = setDoc(exampleRef, qexamples)
136+
res.status(200).send(question);
103137
} catch (err) {
104138
console.log(err);
105139
res.status(500).send(err);
Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,59 @@
11
import { initializeApp } from "firebase/app";
2-
import { getFirestore } 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);
66
export const db = getFirestore();
7+
8+
interface Question {
9+
title: string;
10+
tags: string[];
11+
categories: string[];
12+
constraints: string[];
13+
difficulty: string;
14+
description: string;
15+
examples: Example[];
16+
}
17+
18+
interface Example {
19+
text: string;
20+
image: string;
21+
}
22+
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+
33+
export async function updateQuestion(questionId: string, question: Question): Promise<Question> {
34+
try {
35+
let questionDoc: Omit<Question, "examples"> = question
36+
const docRef = doc(db, "questions", questionId);
37+
await setDoc(docRef, questionDoc);
38+
for (let i = 0; i < question.examples.length; i++) {
39+
const add = setDoc(doc(docRef, "examples", (i+1).toString()), question.examples[i]);
40+
}
41+
return Promise.resolve(question);
42+
} catch (error) {
43+
return Promise.reject(error);
44+
}
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+

UserService/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"skipLibCheck": true
1010
},
1111
"include": ["./**/*.ts"],
12-
"exclue": ["node_modules"]
12+
"exclude": ["node_modules"]
1313
}

frontend/src/api/questions/data.ts

Lines changed: 5 additions & 2 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 deleteQuestion = () =>
20-
questionhttpClient.delete("/questions/:questionId");
19+
export const updateQuestion = (questionId: string, params: QuestionDTO) =>
20+
questionhttpClient.put(`/questions/${questionId}`, params);
21+
22+
export const deleteQuestion = (questionId: string) =>
23+
questionhttpClient.delete(`/questions/${questionId}`);

frontend/src/api/questions/dto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Example } from "../../data/data.context";
33
export interface QuestionDTO {
44
title: string;
55
tags: string[];
6-
categories: string;
7-
constraints: string;
6+
categories: string[];
7+
constraints: string[];
88
difficulty: string;
99
description: string;
1010
examples: Example[];

0 commit comments

Comments
 (0)