refactor: exercise and level model
This commit is contained in:
parent
eedafb036f
commit
90dbbd25c1
|
|
@ -186,7 +186,17 @@ export const getExerciseByLevelId = async (req, res) => {
|
|||
try {
|
||||
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) {
|
||||
return response(404, null, "Level not found", res);
|
||||
}
|
||||
|
|
@ -254,87 +264,19 @@ export const getExerciseByLevelId = async (req, res) => {
|
|||
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) {
|
||||
console.log(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) => {
|
||||
const { id } = req.params;
|
||||
const transaction = await models.db.transaction();
|
||||
|
|
|
|||
|
|
@ -214,6 +214,8 @@ export const getLevelsByTopicId = async (req, res) => {
|
|||
ID_STUDENT_LEARNING: lastCompletedLearning.ID_STUDENT_LEARNING,
|
||||
ID_LEVEL: lastCompletedLearning.level.ID_LEVEL,
|
||||
NAME_LEVEL: lastCompletedLearning.level.NAME_LEVEL,
|
||||
SCORE: lastCompletedLearning.SCORE,
|
||||
NEXT_LEARNING: lastCompletedLearning.NEXT_LEARNING,
|
||||
FINISHED_AT: lastCompletedLearning.STUDENT_FINISH,
|
||||
}
|
||||
: null,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user