diff --git a/controllers/contentControllers/section.js b/controllers/contentControllers/section.js index 57a2a75..10f652a 100644 --- a/controllers/contentControllers/section.js +++ b/controllers/contentControllers/section.js @@ -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( diff --git a/controllers/learningControllers/stdLearning.js b/controllers/learningControllers/stdLearning.js index a36e538..c9edd04 100644 --- a/controllers/learningControllers/stdLearning.js +++ b/controllers/learningControllers/stdLearning.js @@ -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);