import response from "../response.js"; import models from "../models/index.js"; import fs from "fs"; import path from "path"; export const getSubjects = async (req, res) => { try { const subjects = await models.Subject.findAll(); response(200, subjects, "Success", res); } catch (error) { console.log(error); response(500, null, "Error retrieving subjects data!", res); } }; export const getSubjectById = async (req, res) => { try { const { id } = req.params; const subject = await models.Subject.findByPk(id); if (!subject) { return response(404, null, "Subject not found", res); } response(200, subject, "Success", res); } catch (error) { console.log(error); res.status(500).json({ message: "Internal Server Error" }); } }; export const createSubject = async (req, res) => { const { name, description, icon, thumbnail } = req.body; // Validate name if (!name) { return response(400, null, "Name is required", res); } // Validate description if (!description) { return response(400, null, "Description is required", res); } try { const newSubject = await models.Subject.create({ name, description, icon, thumbnail, }); response(201, newSubject, "Subject created successfully", res); } catch (error) { console.log(error); res.status(500).json({ message: "Internal Server Error" }); } }; export const updateSubjectById = async (req, res) => { const { id } = req.params; const { name, description } = req.body; const icon = req.body.icon; const thumbnail = req.body.thumbnail; try { const subject = await models.Subject.findByPk(id); if (!subject) { return response(404, null, "Subject not found", res); } // Update subject fields if (name) subject.name = name; if (description) subject.description = description; if (icon) { // Remove old icon if it exists if ( subject.icon && fs.existsSync(path.join("public/uploads", subject.icon)) ) { fs.unlinkSync(path.join("public/uploads", subject.icon)); } subject.icon = icon; } if (thumbnail) { // Remove old thumbnail if it exists if ( subject.thumbnail && fs.existsSync(path.join("public/uploads", subject.thumbnail)) ) { fs.unlinkSync(path.join("public/uploads", subject.thumbnail)); } subject.thumbnail = thumbnail; } await subject.save(); response(200, subject, "Subject updated successfully", res); } catch (error) { console.log(error); response(500, null, "Internal Server Error", res); } }; export const deleteSubjectById = async (req, res) => { const { id } = req.params; try { const subject = await models.Subject.findByPk(id); if (!subject) { return response(404, null, "Subject not found", res); } // Remove associated files if they exist if ( subject.icon && fs.existsSync(path.join("public/uploads", subject.icon)) ) { fs.unlinkSync(path.join("public/uploads", subject.icon)); } if ( subject.thumbnail && fs.existsSync(path.join("public/uploads", subject.thumbnail)) ) { fs.unlinkSync(path.join("public/uploads", subject.thumbnail)); } await subject.destroy(); response(200, null, "Subject deleted successfully", res); } catch (error) { console.log(error); response(500, null, "Internal Server Error", res); } };