feat: pagination on learning history

This commit is contained in:
elangptra 2024-10-17 08:41:44 +07:00
parent 12905cab0e
commit 6944aa1eef
2 changed files with 70 additions and 7 deletions

View File

@ -130,7 +130,7 @@ export const createSection = async (req, res) => {
if (existingSection) {
clearFileBuffers({ THUMBNAIL });
await transaction.rollback();
return response(400, null, "Section name already exists", res);
return response(409, null, "Section name already exists", res);
}
const newSection = await models.Section.create(

View File

@ -241,6 +241,8 @@ export const learningScoreByStdLearningId = async (req, res) => {
};
export const learningHistory = async (req, res) => {
const { page = 1, limit = 10 } = req.query;
try {
if (!req.user) {
return response(401, null, "User not authenticated", res);
@ -283,7 +285,7 @@ export const learningHistory = async (req, res) => {
);
}
const result = await Promise.all(
const formattedLearnings = await Promise.all(
stdLearnings.map(async (learning) => {
let nextLevelName = null;
if (learning.NEXT_LEARNING) {
@ -304,11 +306,30 @@ export const learningHistory = async (req, res) => {
SECTION_NAME:
learning.level?.levelTopic?.topicSection?.NAME_SECTION ||
"No section",
IS_PASS: learning.IS_PASS,
};
})
);
response(200, result, "Success", res);
const paginatedLearnings = formattedLearnings.slice(
(page - 1) * limit,
page * limit
);
const totalPages = Math.ceil(formattedLearnings.length / limit);
const currentPage = parseInt(page);
response(
200,
{
history: paginatedLearnings,
currentPage,
totalPages,
totalItems: formattedLearnings.length,
},
"Learning history retrieved successfully",
res
);
} catch (error) {
console.error(error);
response(500, null, "Internal Server Error", res);
@ -316,6 +337,8 @@ export const learningHistory = async (req, res) => {
};
export const learningHistoryBySectionId = async (req, res) => {
const { page = 1, limit = 10 } = req.query;
try {
if (!req.user) {
return response(401, null, "User not authenticated", res);
@ -360,7 +383,7 @@ export const learningHistoryBySectionId = async (req, res) => {
);
}
const result = await Promise.all(
const formattedLearnings = await Promise.all(
stdLearnings.map(async (learning) => {
let nextLevelName = null;
if (learning.NEXT_LEARNING) {
@ -381,11 +404,30 @@ export const learningHistoryBySectionId = async (req, res) => {
SECTION_NAME:
learning.level?.levelTopic?.topicSection?.NAME_SECTION ||
"No section",
IS_PASS: learning.IS_PASS,
};
})
);
response(200, result, "Success", res);
const paginatedLearnings = formattedLearnings.slice(
(page - 1) * limit,
page * limit
);
const totalPages = Math.ceil(formattedLearnings.length / limit);
const currentPage = parseInt(page);
response(
200,
{
history: paginatedLearnings,
currentPage,
totalPages,
totalItems: formattedLearnings.length,
},
"Learning history retrieved successfully",
res
);
} catch (error) {
console.log(error);
response(500, null, "Internal Server Error", res);
@ -393,6 +435,8 @@ export const learningHistoryBySectionId = async (req, res) => {
};
export const learningHistoryByTopicId = async (req, res) => {
const { page = 1, limit = 10 } = req.query;
try {
if (!req.user) {
return response(401, null, "User not authenticated", res);
@ -441,7 +485,7 @@ export const learningHistoryByTopicId = async (req, res) => {
);
}
const result = await Promise.all(
const formattedLearnings = await Promise.all(
stdLearnings.map(async (learning) => {
let nextLevelName = null;
if (learning.NEXT_LEARNING) {
@ -462,11 +506,30 @@ export const learningHistoryByTopicId = async (req, res) => {
SECTION_NAME:
learning.level?.levelTopic?.topicSection?.NAME_SECTION ||
"No section",
IS_PASS: learning.IS_PASS,
};
})
);
response(200, result, "Success", res);
const paginatedLearnings = formattedLearnings.slice(
(page - 1) * limit,
page * limit
);
const totalPages = Math.ceil(formattedLearnings.length / limit);
const currentPage = parseInt(page);
response(
200,
{
history: paginatedLearnings,
currentPage,
totalPages,
totalItems: formattedLearnings.length,
},
"Learning history retrieved successfully",
res
);
} catch (error) {
console.error(error);
response(500, null, "Internal Server Error", res);