|
| 1 | +"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 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +exports.handleAddQuestion = exports.handleDeleteQuestion = exports.handleGetQuestions = void 0; |
| 13 | +const question_service_1 = require("./question.service"); |
| 14 | +const firestore_1 = require("firebase/firestore"); |
| 15 | +function handleGetQuestions(req, res) { |
| 16 | + return __awaiter(this, void 0, void 0, function* () { |
| 17 | + try { |
| 18 | + // console.log(req.query.email); |
| 19 | + // const { email } = req.query; |
| 20 | + console.log(`getting all questions`); |
| 21 | + const query = yield (0, firestore_1.getDocs)((0, firestore_1.collection)(question_service_1.db, "questions")); |
| 22 | + const result = yield Promise.all(query.docs.map((d) => __awaiter(this, void 0, void 0, function* () { |
| 23 | + const q = d.data(); |
| 24 | + const examplesArray = yield getExamples(d.id); |
| 25 | + return { |
| 26 | + id: d.id, |
| 27 | + title: q.title, |
| 28 | + tags: q.tags, |
| 29 | + categories: q.categories, |
| 30 | + constraints: q.constraints, |
| 31 | + difficulty: q.difficulty, |
| 32 | + description: q.description, |
| 33 | + examples: examplesArray, |
| 34 | + }; |
| 35 | + }))); |
| 36 | + if (result.length > 0) { |
| 37 | + res.status(200).send(result); |
| 38 | + } |
| 39 | + else { |
| 40 | + res.status(500).send("no questions"); |
| 41 | + } |
| 42 | + } |
| 43 | + catch (error) { |
| 44 | + console.error(error); |
| 45 | + res.status(500).send(error); |
| 46 | + } |
| 47 | + }); |
| 48 | +} |
| 49 | +exports.handleGetQuestions = handleGetQuestions; |
| 50 | +const getExamples = (id) => __awaiter(void 0, void 0, void 0, function* () { |
| 51 | + const subCollRef = (0, firestore_1.collection)(question_service_1.db, "questions", id, "examples"); |
| 52 | + const examplesSnapshot = yield (0, firestore_1.getDocs)(subCollRef); |
| 53 | + const examplesResult = examplesSnapshot.docs.map((data) => { |
| 54 | + const exampleData = data.data(); |
| 55 | + return { |
| 56 | + text: exampleData.text, |
| 57 | + image: exampleData.img || "", // Use an empty string if image is missing |
| 58 | + }; |
| 59 | + }); |
| 60 | + // console.log(examplesSnapshot) |
| 61 | + return examplesResult; |
| 62 | +}); |
| 63 | +function handleDeleteQuestion(req, res) { |
| 64 | + return __awaiter(this, void 0, void 0, function* () { |
| 65 | + const questionId = req.params.questionId; |
| 66 | + try { |
| 67 | + const docRef = (0, firestore_1.doc)(question_service_1.db, "questions", questionId); |
| 68 | + const result = yield (0, firestore_1.deleteDoc)(docRef); |
| 69 | + } |
| 70 | + catch (err) { |
| 71 | + console.log(`error when deleting question with id ${questionId}` + err); |
| 72 | + res.status(500).send(err); |
| 73 | + } |
| 74 | + }); |
| 75 | +} |
| 76 | +exports.handleDeleteQuestion = handleDeleteQuestion; |
| 77 | +function handleAddQuestion(req, res) { |
| 78 | + return __awaiter(this, void 0, void 0, function* () { |
| 79 | + try { |
| 80 | + const { qtitle, qtags, qcategories, qconstraints, qdifficulty, qdescription, qexamples, } = req.body; |
| 81 | + const docRef = yield (0, firestore_1.addDoc)((0, firestore_1.collection)(question_service_1.db, "questions"), { |
| 82 | + title: qtitle, |
| 83 | + tags: qtags, |
| 84 | + categories: qcategories, |
| 85 | + constraints: qconstraints, |
| 86 | + difficulty: qdifficulty, |
| 87 | + description: qdescription, |
| 88 | + }); |
| 89 | + const exampleRef = (0, firestore_1.collection)(docRef, "examples"); |
| 90 | + qexamples.map((e) => { |
| 91 | + const add = (0, firestore_1.addDoc)(exampleRef, e); |
| 92 | + }); |
| 93 | + // const addExample = setDoc(exampleRef, qexamples) |
| 94 | + } |
| 95 | + catch (err) { |
| 96 | + console.log(err); |
| 97 | + res.status(500).send(err); |
| 98 | + } |
| 99 | + }); |
| 100 | +} |
| 101 | +exports.handleAddQuestion = handleAddQuestion; |
0 commit comments