refactor: exercise and level model

This commit is contained in:
elangptra 2024-10-02 09:45:18 +07:00
parent eedafb036f
commit 90dbbd25c1
2 changed files with 20 additions and 76 deletions

View File

@ -186,7 +186,17 @@ export const getExerciseByLevelId = async (req, res) => {
try { try {
const { idLevel } = req.params; const { idLevel } = req.params;
const levelExists = await models.Level.findByPk(idLevel); const levelExists = await models.Level.findByPk(idLevel, {
include: [
{
model: models.Topic,
as: "levelTopic",
attributes: ["NAME_TOPIC"],
},
],
attributes: ["NAME_LEVEL"],
});
if (!levelExists) { if (!levelExists) {
return response(404, null, "Level not found", res); return response(404, null, "Level not found", res);
} }
@ -254,87 +264,19 @@ export const getExerciseByLevelId = async (req, res) => {
return exerciseData; return exerciseData;
}); });
response(200, formattedExercises, "Success", res); const responsePayload = {
NAME_TOPIC: levelExists.levelTopic.NAME_TOPIC,
NAME_LEVEL: levelExists.NAME_LEVEL,
EXERCISES: formattedExercises,
};
response(200, responsePayload, "Success", res);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
res.status(500).json({ message: "Internal Server Error" }); res.status(500).json({ message: "Internal Server Error" });
} }
}; };
// export const deleteExerciseById = async (req, res) => {
// const { id } = req.params;
// const transaction = await models.db.transaction();
// try {
// const exercise = await models.Exercise.findByPk(id, {
// include: [
// {
// model: models.MultipleChoices,
// as: "multipleChoices",
// },
// {
// model: models.MatchingPairs,
// as: "matchingPairs",
// },
// {
// model: models.TrueFalse,
// as: "trueFalse",
// },
// ],
// });
// if (!exercise) {
// await transaction.rollback();
// return response(404, null, "Exercise not found", res);
// }
// if (exercise.AUDIO) {
// const audioPath = path.join(
// "public/uploads/exercise/audio",
// exercise.AUDIO
// );
// if (fs.existsSync(audioPath)) fs.unlinkSync(audioPath);
// }
// if (exercise.IMAGE) {
// const imagePath = path.join(
// "public/uploads/exercise/image",
// exercise.IMAGE
// );
// if (fs.existsSync(imagePath)) fs.unlinkSync(imagePath);
// }
// const questionType = exercise.QUESTION_TYPE;
// if (questionType === "MCQ") {
// await models.MultipleChoices.destroy({
// where: { ID_ADMIN_EXERCISE: id },
// transaction,
// });
// } else if (questionType === "MPQ") {
// await models.MatchingPairs.destroy({
// where: { ID_ADMIN_EXERCISE: id },
// transaction,
// });
// } else if (questionType === "TFQ") {
// await models.TrueFalse.destroy({
// where: { ID_ADMIN_EXERCISE: id },
// transaction,
// });
// }
// await exercise.destroy({ transaction });
// await transaction.commit();
// response(200, null, "Exercise and related data deleted successfully", res);
// } catch (error) {
// console.log(error);
// await transaction.rollback();
// response(500, null, "Internal Server Error", res);
// }
// };
export const deleteExerciseById = async (req, res) => { export const deleteExerciseById = async (req, res) => {
const { id } = req.params; const { id } = req.params;
const transaction = await models.db.transaction(); const transaction = await models.db.transaction();

View File

@ -214,6 +214,8 @@ export const getLevelsByTopicId = async (req, res) => {
ID_STUDENT_LEARNING: lastCompletedLearning.ID_STUDENT_LEARNING, ID_STUDENT_LEARNING: lastCompletedLearning.ID_STUDENT_LEARNING,
ID_LEVEL: lastCompletedLearning.level.ID_LEVEL, ID_LEVEL: lastCompletedLearning.level.ID_LEVEL,
NAME_LEVEL: lastCompletedLearning.level.NAME_LEVEL, NAME_LEVEL: lastCompletedLearning.level.NAME_LEVEL,
SCORE: lastCompletedLearning.SCORE,
NEXT_LEARNING: lastCompletedLearning.NEXT_LEARNING,
FINISHED_AT: lastCompletedLearning.STUDENT_FINISH, FINISHED_AT: lastCompletedLearning.STUDENT_FINISH,
} }
: null, : null,