2024-09-13 13:03:35 +00:00
|
|
|
import response from "../../response.js";
|
|
|
|
|
import models from "../../models/index.js";
|
2024-08-12 02:44:06 +00:00
|
|
|
|
|
|
|
|
export const getTopics = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const topics = await models.Topic.findAll();
|
|
|
|
|
response(200, topics, "Success", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
response(500, null, "Error retrieving topics data!", res);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getTopicById = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
const topic = await models.Topic.findByPk(id);
|
|
|
|
|
|
|
|
|
|
if (!topic) {
|
|
|
|
|
return response(404, null, "Topic not found", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response(200, topic, "Success", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2024-09-13 13:03:35 +00:00
|
|
|
response(500, null, "Internal Server Error", res);
|
2024-08-12 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-19 10:04:18 +00:00
|
|
|
export const getTopicBySectionId = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { sectionId } = req.params;
|
|
|
|
|
|
|
|
|
|
const sectionExists = await models.Section.findByPk(sectionId);
|
|
|
|
|
if (!sectionExists) {
|
|
|
|
|
return response(404, null, "Section not found", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const topics = await models.Topic.findAll({
|
|
|
|
|
where: { ID_SECTION: sectionId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!topics || topics.length === 0) {
|
|
|
|
|
return response(404, null, "No topics found for this section", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response(200, topics, "Success", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
response(500, null, "Internal Server Error", res);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-12 02:44:06 +00:00
|
|
|
export const createTopic = async (req, res) => {
|
2024-09-19 10:04:18 +00:00
|
|
|
const { ID_SECTION, NAME_TOPIC, DESCRIPTION_TOPIC, OBJECTIVES } = req.body;
|
2024-09-13 13:03:35 +00:00
|
|
|
|
|
|
|
|
if (!ID_SECTION) {
|
|
|
|
|
return response(400, null, "Section ID is required", res);
|
|
|
|
|
}
|
2024-08-12 02:44:06 +00:00
|
|
|
|
2024-09-13 13:03:35 +00:00
|
|
|
if (!NAME_TOPIC) {
|
|
|
|
|
return response(400, null, "Topic name is required", res);
|
2024-08-12 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-13 13:03:35 +00:00
|
|
|
if (!DESCRIPTION_TOPIC) {
|
2024-09-19 10:04:18 +00:00
|
|
|
return response(400, null, "Topic description is required", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!OBJECTIVES) {
|
|
|
|
|
return response(400, null, "Topic objectives are required", res);
|
2024-08-12 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2024-09-13 13:03:35 +00:00
|
|
|
const section = await models.Section.findByPk(ID_SECTION);
|
|
|
|
|
if (!section) {
|
|
|
|
|
return response(404, null, "Section not found", res);
|
2024-08-12 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newTopic = await models.Topic.create({
|
2024-09-13 13:03:35 +00:00
|
|
|
ID_SECTION,
|
|
|
|
|
NAME_TOPIC,
|
|
|
|
|
DESCRIPTION_TOPIC,
|
2024-09-19 10:04:18 +00:00
|
|
|
OBJECTIVES,
|
2024-08-12 02:44:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
response(201, newTopic, "Topic created successfully", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
response(500, null, "Internal Server Error", res);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const updateTopicById = async (req, res) => {
|
|
|
|
|
const { id } = req.params;
|
2024-09-19 10:04:18 +00:00
|
|
|
const { ID_SECTION, NAME_TOPIC, DESCRIPTION_TOPIC, OBJECTIVES } = req.body;
|
2024-08-12 02:44:06 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const topic = await models.Topic.findByPk(id);
|
|
|
|
|
|
|
|
|
|
if (!topic) {
|
|
|
|
|
return response(404, null, "Topic not found", res);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 13:03:35 +00:00
|
|
|
if (ID_SECTION) {
|
|
|
|
|
const section = await models.Section.findByPk(ID_SECTION);
|
|
|
|
|
if (!section) {
|
|
|
|
|
return response(404, null, "Section not found", res);
|
2024-08-12 02:44:06 +00:00
|
|
|
}
|
2024-09-13 13:03:35 +00:00
|
|
|
topic.ID_SECTION = ID_SECTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (NAME_TOPIC) {
|
|
|
|
|
topic.NAME_TOPIC = NAME_TOPIC;
|
2024-08-12 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-13 13:03:35 +00:00
|
|
|
if (DESCRIPTION_TOPIC) {
|
|
|
|
|
topic.DESCRIPTION_TOPIC = DESCRIPTION_TOPIC;
|
2024-08-12 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-19 10:04:18 +00:00
|
|
|
if (OBJECTIVES) {
|
|
|
|
|
topic.OBJECTIVES = OBJECTIVES;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 02:44:06 +00:00
|
|
|
await topic.save();
|
|
|
|
|
|
|
|
|
|
response(200, topic, "Topic updated successfully", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
response(500, null, "Internal Server Error", res);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const deleteTopicById = async (req, res) => {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const topic = await models.Topic.findByPk(id);
|
|
|
|
|
|
|
|
|
|
if (!topic) {
|
|
|
|
|
return response(404, null, "Topic not found", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await topic.destroy();
|
|
|
|
|
|
|
|
|
|
response(200, null, "Topic deleted successfully", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
response(500, null, "Internal Server Error", res);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-09-19 10:04:18 +00:00
|
|
|
|
|
|
|
|
export const getCompletedTopics = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const user = req.user;
|
|
|
|
|
const userId = user.ID;
|
|
|
|
|
|
|
|
|
|
const completedLevels = await models.StdLearning.findAll({
|
|
|
|
|
where: {
|
|
|
|
|
ID: userId,
|
|
|
|
|
IS_PASS: 1,
|
|
|
|
|
},
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: models.Level,
|
|
|
|
|
as: "level",
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: models.Topic,
|
|
|
|
|
as: "levelTopic",
|
|
|
|
|
attributes: [
|
|
|
|
|
"ID_TOPIC",
|
|
|
|
|
"NAME_TOPIC",
|
|
|
|
|
"DESCRIPTION_TOPIC",
|
|
|
|
|
"OBJECTIVES",
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!completedLevels.length) {
|
|
|
|
|
return response(404, null, "No completed topics found", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const completedTopics = [];
|
|
|
|
|
for (const levelRecord of completedLevels) {
|
|
|
|
|
const level = levelRecord.level;
|
|
|
|
|
|
|
|
|
|
const level6 = await models.Level.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
NAME_LEVEL: "Level 6",
|
|
|
|
|
ID_TOPIC: level.ID_TOPIC,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (level6 && level.ID_LEVEL === level6.ID_LEVEL) {
|
|
|
|
|
const topic = level.levelTopic;
|
|
|
|
|
completedTopics.push({
|
|
|
|
|
ID_TOPIC: topic.ID_TOPIC,
|
|
|
|
|
NAME_TOPIC: topic.NAME_TOPIC,
|
|
|
|
|
DESCRIPTION_TOPIC: topic.DESCRIPTION_TOPIC,
|
|
|
|
|
OBJECTIVES: topic.OBJECTIVES,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!completedTopics.length) {
|
|
|
|
|
return response(404, null, "No completed topics for Level 6 found", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response(
|
|
|
|
|
200,
|
|
|
|
|
completedTopics,
|
|
|
|
|
"Completed topics fetched successfully",
|
|
|
|
|
res
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
response(500, null, "Internal Server Error", res);
|
|
|
|
|
}
|
|
|
|
|
};
|