Skip to content

Commit 8f6c5d9

Browse files
committed
Restore backend question controller
1 parent 519295c commit 8f6c5d9

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

backend/question-service/src/controllers/questionController.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from "express";
22
import Question, { IQuestion } from "../models/Question.ts";
3-
import { checkIsExistingQuestion } from "../utils/utils.ts";
3+
import { checkIsExistingQuestion, sortAlphabetically } from "../utils/utils.ts";
44
import {
55
DUPLICATE_QUESTION_MESSAGE,
66
QN_DESC_EXCEED_CHAR_LIMIT_MESSAGE,
@@ -13,6 +13,8 @@ import {
1313
PAGE_LIMIT_REQUIRED_MESSAGE,
1414
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
1515
CATEGORIES_RETRIEVED_MESSAGE,
16+
MONGO_OBJ_ID_FORMAT,
17+
MONGO_OBJ_ID_MALFORMED_MESSAGE,
1618
} from "../utils/constants.ts";
1719

1820
import { upload } from "../../config/multer";
@@ -78,6 +80,7 @@ export const createImageLink = async (
7880

7981
const uploadPromises = files.map((file) => uploadFileToFirebase(file));
8082
const imageUrls = await Promise.all(uploadPromises);
83+
console.log(imageUrls);
8184
res
8285
.status(200)
8386
.json({ message: "Images uploaded successfully", imageUrls });
@@ -95,7 +98,13 @@ export const updateQuestion = async (
9598
const { id } = req.params;
9699
const { title, description } = req.body;
97100

101+
if (!id.match(MONGO_OBJ_ID_FORMAT)) {
102+
res.status(400).json({ message: MONGO_OBJ_ID_MALFORMED_MESSAGE });
103+
return;
104+
}
105+
98106
const currentQuestion = await Question.findById(id);
107+
99108
if (!currentQuestion) {
100109
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
101110
return;
@@ -203,7 +212,12 @@ export const readQuestionsList = async (
203212
res.status(200).json({
204213
message: QN_RETRIEVED_MESSAGE,
205214
questionCount: filteredQuestionCount,
206-
questions: filteredQuestions.map(formatQuestionResponse),
215+
questions: filteredQuestions
216+
.map(formatQuestionResponse)
217+
.map((question) => ({
218+
...question,
219+
categories: sortAlphabetically(question.categories),
220+
})),
207221
});
208222
} catch (error) {
209223
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
@@ -241,7 +255,7 @@ export const readCategories = async (
241255

242256
res.status(200).json({
243257
message: CATEGORIES_RETRIEVED_MESSAGE,
244-
categories: uniqueCats,
258+
categories: sortAlphabetically(uniqueCats),
245259
});
246260
} catch (error) {
247261
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });

0 commit comments

Comments
 (0)