Skip to content

Commit

Permalink
Merge pull request #11 from feliciagan/be_question_read
Browse files Browse the repository at this point in the history
Add read endpoints
  • Loading branch information
ruiqi7 authored Sep 24, 2024
2 parents e4fbc17 + da0938f commit d798d3e
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 18 deletions.
133 changes: 123 additions & 10 deletions backend/question-service/src/controllers/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import {
DUPLICATE_QUESTION_RESPONSE_MESSAGE,
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
QN_DESC_CHAR_LIMIT,
QN_NOT_FOUND,
QN_DELETED,
SERVER_ERROR,
QN_CREATED_MESSAGE,
QN_NOT_FOUND_MESSAGE,
QN_DELETED_MESSAGE,
SERVER_ERROR_MESSAGE,
QN_RETRIEVED_MESSAGE,
PAGE_LIMIT_REQUIRED_MESSAGE,
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
CATEGORIES_NOT_FOUND_MESSAGE,
CATEGORIES_RETRIEVED_MESSAGE,
} from "../utils/constants.ts";

import { upload } from "../../config/multer";
Expand Down Expand Up @@ -45,11 +51,11 @@ export const createQuestion = async (
await newQuestion.save();

res.status(201).json({
message: "Question created successfully",
message: QN_CREATED_MESSAGE,
question: newQuestion,
});
} catch (error) {
res.status(500).json({ message: SERVER_ERROR, error });
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
}
};

Expand Down Expand Up @@ -92,7 +98,7 @@ export const updateQuestion = async (

const currentQuestion = await Question.findById(id);
if (!currentQuestion) {
res.status(404).json({ message: QN_NOT_FOUND });
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
return;
}

Expand Down Expand Up @@ -120,7 +126,7 @@ export const updateQuestion = async (
question: updatedQuestion,
});
} catch (error) {
res.status(500).json({ message: SERVER_ERROR, error });
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
}
};

Expand All @@ -132,13 +138,120 @@ export const deleteQuestion = async (
const { id } = req.params;
const currentQuestion = await Question.findById(id);
if (!currentQuestion) {
res.status(400).json({ message: QN_NOT_FOUND });
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
return;
}

await Question.findByIdAndDelete(id);
res.status(200).json({ message: QN_DELETED });
res.status(200).json({ message: QN_DELETED_MESSAGE });
} catch (error) {
res.status(500).json({ message: SERVER_ERROR, error });
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
}
};

interface QnListSearchFilterParams {
page: string;
qnLimit: string;
title?: string;
complexities?: string | string[];
categories?: string | string[];
}

export const readQuestionsList = async (
req: Request<unknown, unknown, unknown, QnListSearchFilterParams>,
res: Response,
): Promise<void> => {
try {
const { page, qnLimit, title, complexities, categories } = req.query;

if (!page || !qnLimit) {
res.status(400).json({ message: PAGE_LIMIT_REQUIRED_MESSAGE });
return;
}

const pageInt = parseInt(page, 10);
const qnLimitInt = parseInt(qnLimit, 10);

if (pageInt < 1 || qnLimitInt < 1) {
res.status(400).json({ message: PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE });
return;
}

const query: any = {};

if (title) {
query.title = { $regex: new RegExp(title, "i") };
}

if (complexities) {
query.complexity = {
$in: Array.isArray(complexities) ? complexities : [complexities],
};
}

if (categories) {
query.category = {
$in: Array.isArray(categories) ? categories : [categories],
};
}

const filteredTotalQuestions = await Question.countDocuments(query);
if (filteredTotalQuestions == 0) {
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
return;
}

const filteredQuestions = await Question.find(query)
.skip((pageInt - 1) * qnLimitInt)
.limit(qnLimitInt);

res.status(200).json({
message: QN_RETRIEVED_MESSAGE,
totalQns: filteredTotalQuestions,
questions: filteredQuestions,
});
} catch (error) {
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
}
};

export const readQuestionIndiv = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const { id } = req.params;

const questionDetails = await Question.findById(id);
if (!questionDetails) {
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
return;
}

res.status(200).json({
message: QN_RETRIEVED_MESSAGE,
question: questionDetails,
});
} catch (error) {
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
}
};

export const readCategories = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const uniqueCats = await Question.distinct("category");
if (!uniqueCats || uniqueCats.length == 0) {
res.status(404).json({ message: CATEGORIES_NOT_FOUND_MESSAGE });
}

res.status(200).json({
message: CATEGORIES_RETRIEVED_MESSAGE,
categories: uniqueCats,
});
} catch (error) {
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
}
};
9 changes: 9 additions & 0 deletions backend/question-service/src/routes/questionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
deleteQuestion,
createImageLink,
updateQuestion,
readQuestionsList,
readQuestionIndiv,
readCategories,
} from "../controllers/questionController.ts";

const router = express.Router();
Expand All @@ -14,6 +17,12 @@ router.post("/questions/images", createImageLink);

router.put("/questions/:id", updateQuestion);

router.get("/questions/categories", readCategories);

router.get("/questions", readQuestionsList);

router.get("/questions/:id", readQuestionIndiv);

router.delete("/questions/:id", deleteQuestion);

export default router;
21 changes: 18 additions & 3 deletions backend/question-service/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ export const QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE =
export const DUPLICATE_QUESTION_RESPONSE_MESSAGE =
"Duplicate question: A question with the same title already exists.";

export const QN_NOT_FOUND = "Question not found.";
export const QN_CREATED_MESSAGE = "Question created successfully.";

export const QN_DELETED = "Question deleted successfully.";
export const QN_NOT_FOUND_MESSAGE = "Question not found.";

export const SERVER_ERROR = "Server error.";
export const QN_DELETED_MESSAGE = "Question deleted successfully.";

export const SERVER_ERROR_MESSAGE = "Server error.";

export const QN_RETRIEVED_MESSAGE = "Question retrieved successfully.";

export const PAGE_LIMIT_REQUIRED_MESSAGE =
"Page number and question limit per page should be provided.";

export const PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE =
"Page number and question limit per page should be positive integers.";

export const CATEGORIES_NOT_FOUND_MESSAGE = "No categories found.";

export const CATEGORIES_RETRIEVED_MESSAGE =
"Categories retrieved successfully.";
Loading

0 comments on commit d798d3e

Please sign in to comment.