import response from "../../response.js"; import models from "../../models/index.js"; 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); response(500, null, "Internal Server Error", res); } }; export const createTopic = async (req, res) => { const { ID_SECTION, NAME_TOPIC, DESCRIPTION_TOPIC } = req.body; if (!ID_SECTION) { return response(400, null, "Section ID is required", res); } if (!NAME_TOPIC) { return response(400, null, "Topic name is required", res); } if (!DESCRIPTION_TOPIC) { return response(400, null, "Topic Description is required", res); } try { const section = await models.Section.findByPk(ID_SECTION); if (!section) { return response(404, null, "Section not found", res); } const newTopic = await models.Topic.create({ ID_SECTION, NAME_TOPIC, DESCRIPTION_TOPIC, }); 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; const { ID_SECTION, NAME_TOPIC, DESCRIPTION_TOPIC } = req.body; try { const topic = await models.Topic.findByPk(id); if (!topic) { return response(404, null, "Topic not found", res); } if (ID_SECTION) { const section = await models.Section.findByPk(ID_SECTION); if (!section) { return response(404, null, "Section not found", res); } topic.ID_SECTION = ID_SECTION; } if (NAME_TOPIC) { topic.NAME_TOPIC = NAME_TOPIC; } if (DESCRIPTION_TOPIC) { topic.DESCRIPTION_TOPIC = DESCRIPTION_TOPIC; } 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); } };