Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read endpoints #11

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 98 additions & 5 deletions backend/question-service/src/controllers/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {
DUPLICATE_QUESTION_RESPONSE_MESSAGE,
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
QN_DESC_CHAR_LIMIT,
QN_CREATED_MESSAGE,
QN_NOT_FOUND_MESSAGE,
SERVER_ERROR_MESSAGE,
QN_RETRIEVED_MESSAGE,
PAGE_LIMIT_REQUIRED_MESSAGE,
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
} from "../utils/constants.ts";

import { upload } from "../../config/multer";
Expand Down Expand Up @@ -42,11 +48,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 @@ -89,7 +95,7 @@ export const updateQuestion = async (

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

Expand Down Expand Up @@ -117,7 +123,94 @@ export const updateQuestion = async (
question: updatedQuestion,
});
} catch (error) {
console.log(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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we set a default if the user doesn't specify? maybe fetch the first page containing 10 questions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better for the frontend to handle the error message returned and resend the correct request instead? cuz if not then the user click to go to pg 11 then they go back to pg 1 also a bit weird. also on that note im not sure if this error will actually happen cuz the frontend shld always be sending it correctly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruiqi7 what do u think? since u doing the frontend

Copy link

@ruiqi7 ruiqi7 Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes, the frontend will be sending it properly

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 filteredTotalPages = Math.ceil(filteredTotalQuestions / qnLimitInt);

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

res.status(200).json({
message: QN_RETRIEVED_MESSAGE,
pages: filteredTotalPages,
feliciagan marked this conversation as resolved.
Show resolved Hide resolved
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 });
}
};
6 changes: 6 additions & 0 deletions backend/question-service/src/routes/questionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
createQuestion,
createImageLink,
updateQuestion,
readQuestionsList,
readQuestionIndiv,
} from "../controllers/questionController.ts";

const router = express.Router();
Expand All @@ -13,4 +15,8 @@ router.post("/questions/images", createImageLink);

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

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

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

export default router;
14 changes: 14 additions & 0 deletions backend/question-service/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ 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_CREATED_MESSAGE = "Question created successfully.";

export const QN_NOT_FOUND_MESSAGE = "Question not found.";

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.";