168 lines
4.3 KiB
JavaScript
168 lines
4.3 KiB
JavaScript
import response from "../response.js";
|
|
import models from "../models/index.js";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
import {
|
|
clearFileBuffers,
|
|
saveFileToDisk,
|
|
} from "../middlewares/uploadSubject.js";
|
|
|
|
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 } = req.body;
|
|
|
|
// Files to be saved if everything else is okay
|
|
const { icon, thumbnail } = req.filesToSave || {};
|
|
|
|
// Validate name
|
|
if (!name) {
|
|
clearFileBuffers({ icon, thumbnail });
|
|
return response(400, null, "Name is required", res);
|
|
}
|
|
|
|
// Validate description
|
|
if (!description) {
|
|
clearFileBuffers({ icon, thumbnail });
|
|
return response(400, null, "Description is required", res);
|
|
}
|
|
|
|
try {
|
|
const iconFilename = icon
|
|
? saveFileToDisk(icon, `${name}-icon`, name)
|
|
: null;
|
|
const thumbnailFilename = thumbnail
|
|
? saveFileToDisk(thumbnail, `${name}-thumbnail`, name)
|
|
: null;
|
|
|
|
const newSubject = await models.Subject.create({
|
|
name,
|
|
description,
|
|
icon: iconFilename,
|
|
thumbnail: thumbnailFilename,
|
|
});
|
|
|
|
response(201, newSubject, "Subject created successfully", res);
|
|
} catch (error) {
|
|
console.log(error);
|
|
clearFileBuffers({ icon, thumbnail });
|
|
res.status(500).json({ message: "Internal Server Error" });
|
|
}
|
|
};
|
|
|
|
export const updateSubjectById = async (req, res) => {
|
|
const { id } = req.params;
|
|
const { name, description } = req.body;
|
|
|
|
// Files to be saved if everything else is okay
|
|
const { icon, thumbnail } = req.filesToSave || {};
|
|
|
|
try {
|
|
const subject = await models.Subject.findByPk(id);
|
|
|
|
if (!subject) {
|
|
clearFileBuffers({ icon, thumbnail });
|
|
return response(404, null, "Subject not found", res);
|
|
}
|
|
|
|
// Update subject fields
|
|
if (name) subject.name = name;
|
|
if (description) subject.description = description;
|
|
|
|
// Handle icon update
|
|
if (icon) {
|
|
if (subject.icon) {
|
|
const oldIconPath = path.join("public/uploads/subject", subject.icon);
|
|
if (fs.existsSync(oldIconPath)) {
|
|
fs.unlinkSync(oldIconPath);
|
|
}
|
|
}
|
|
subject.icon = saveFileToDisk(icon, `${name}-icon`, name);
|
|
}
|
|
|
|
// Handle thumbnail update
|
|
if (thumbnail) {
|
|
if (subject.thumbnail) {
|
|
const oldThumbnailPath = path.join(
|
|
"public/uploads/subject",
|
|
subject.thumbnail
|
|
);
|
|
if (fs.existsSync(oldThumbnailPath)) {
|
|
fs.unlinkSync(oldThumbnailPath);
|
|
}
|
|
}
|
|
subject.thumbnail = saveFileToDisk(thumbnail, `${name}-thumbnail`, name);
|
|
}
|
|
|
|
await subject.save();
|
|
|
|
response(200, subject, "Subject updated successfully", res);
|
|
} catch (error) {
|
|
console.log(error);
|
|
clearFileBuffers({ icon, thumbnail });
|
|
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 icon if it exists
|
|
if (subject.icon) {
|
|
const iconPath = path.join("public/uploads/subject", subject.icon);
|
|
if (fs.existsSync(iconPath)) {
|
|
fs.unlinkSync(iconPath);
|
|
}
|
|
}
|
|
|
|
// Remove associated thumbnail if it exists
|
|
if (subject.thumbnail) {
|
|
const thumbnailPath = path.join(
|
|
"public/uploads/subject",
|
|
subject.thumbnail
|
|
);
|
|
if (fs.existsSync(thumbnailPath)) {
|
|
fs.unlinkSync(thumbnailPath);
|
|
}
|
|
}
|
|
|
|
await subject.destroy();
|
|
|
|
response(200, null, "Subject deleted successfully", res);
|
|
} catch (error) {
|
|
console.log(error);
|
|
response(500, null, "Internal Server Error", res);
|
|
}
|
|
};
|