Skip to content

Commit 066a5c2

Browse files
committed
Merge branch 'master' into 62-add-functionality-for-maintainer
2 parents 35cff02 + 3ced66d commit 066a5c2

35 files changed

+1197
-78
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/

AuthService/src/auth/auth.controller.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response } from "express";
2-
import { signUp, login, logout } from "./auth.service";
2+
import { signUp, login, logout, removeUser } from "./auth.service";
33

44
export async function handleSignUp(req: Request, res: Response) {
55
try {
@@ -35,3 +35,14 @@ export async function handleLogout(req: Request, res: Response) {
3535
res.status(500).send(error);
3636
}
3737
}
38+
39+
export async function handleDelete(req: Request, res: Response) {
40+
try {
41+
console.log(`deleting user`);
42+
await removeUser();
43+
res.status(200).send();
44+
} catch (error) {
45+
console.error(error);
46+
res.status(500).send(error);
47+
}
48+
}

AuthService/src/auth/auth.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
signInWithEmailAndPassword,
66
signOut,
77
UserCredential,
8+
deleteUser,
89
} from "firebase/auth";
910
import { firebaseConfig } from "../firebase/firebase.config";
1011

@@ -51,3 +52,14 @@ export async function logout(): Promise<void> {
5152
return Promise.reject(error);
5253
}
5354
}
55+
56+
export async function removeUser(): Promise<void> {
57+
try {
58+
if (auth.currentUser) {
59+
await deleteUser(auth.currentUser);
60+
}
61+
return Promise.resolve();
62+
} catch (error) {
63+
return Promise.reject(error);
64+
}
65+
}

AuthService/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
handleLogin,
55
handleLogout,
66
handleSignUp,
7+
handleDelete
78
} from "./auth/auth.controller";
89
// import { Socket, Server } from 'socket.io';
910
// import { initializeApp } from 'firebase/app';
@@ -24,6 +25,7 @@ app.get("/", (req, res) => {
2425
app.post("/signup", handleSignUp);
2526
app.post("/login", handleLogin);
2627
app.delete("/logout", handleLogout);
28+
app.delete("/delete", handleDelete);
2729

2830

2931

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);

0 commit comments

Comments
 (0)