backend_adaptive_learning/controllers/contentControllers/section.js

188 lines
4.6 KiB
JavaScript
Raw Normal View History

2024-09-13 13:03:35 +00:00
import response from "../../response.js";
import models from "../../models/index.js";
import fs from "fs";
import path from "path";
import {
clearFileBuffers,
saveFileToDisk,
} from "../../middlewares/uploadSection.js";
export const getSections = async (req, res) => {
try {
const sections = await models.Section.findAll();
response(200, sections, "Success", res);
} catch (error) {
console.log(error);
response(500, null, "Error retrieving sections data!", res);
}
};
export const getSectionById = async (req, res) => {
try {
const { id } = req.params;
const section = await models.Section.findByPk(id);
if (!section) {
return response(404, null, "Section not found", res);
}
response(200, section, "Success", res);
} catch (error) {
console.log(error);
response(500, null, "Internal Server Error", res);
}
};
export const createSection = async (req, res) => {
const { NAME_SECTION, DESCRIPTION_SECTION } = req.body;
const { THUMBNAIL } = req.filesToSave || {};
2024-09-13 13:03:35 +00:00
if (!NAME_SECTION) {
clearFileBuffers({ THUMBNAIL });
2024-09-13 13:03:35 +00:00
return response(400, null, "Section name is required", res);
}
if (!DESCRIPTION_SECTION) {
clearFileBuffers({ THUMBNAIL });
2024-09-13 13:03:35 +00:00
return response(400, null, "Description is required", res);
}
2024-10-01 08:03:44 +00:00
const transaction = await models.db.transaction();
2024-09-13 13:03:35 +00:00
try {
2024-10-01 08:03:44 +00:00
const existingSection = await models.Section.findOne({
where: { NAME_SECTION },
transaction,
2024-09-13 13:03:35 +00:00
});
2024-10-01 08:03:44 +00:00
if (existingSection) {
clearFileBuffers({ THUMBNAIL });
await transaction.rollback();
return response(400, null, "Section name already exists", res);
}
const newSection = await models.Section.create(
{
NAME_SECTION,
DESCRIPTION_SECTION,
THUMBNAIL: null,
},
{ transaction }
);
const thumbnailFilename = THUMBNAIL
? saveFileToDisk(THUMBNAIL, "THUMBNAIL", newSection.ID_SECTION)
2024-09-13 13:03:35 +00:00
: null;
newSection.THUMBNAIL = thumbnailFilename;
2024-10-01 08:03:44 +00:00
await newSection.save({ transaction });
await transaction.commit();
2024-09-13 13:03:35 +00:00
response(201, newSection, "Section created successfully", res);
} catch (error) {
console.log(error);
2024-10-01 08:03:44 +00:00
await transaction.rollback();
clearFileBuffers({ THUMBNAIL });
2024-10-01 08:03:44 +00:00
2024-09-13 13:03:35 +00:00
response(500, null, "Internal Server Error", res);
}
};
export const updateSectionById = async (req, res) => {
const { id } = req.params;
const { NAME_SECTION, DESCRIPTION_SECTION } = req.body;
const { THUMBNAIL } = req.filesToSave || {};
2024-09-13 13:03:35 +00:00
2024-10-01 08:03:44 +00:00
const transaction = await models.db.transaction();
2024-09-13 13:03:35 +00:00
try {
2024-10-01 08:03:44 +00:00
const section = await models.Section.findByPk(id, { transaction });
2024-09-13 13:03:35 +00:00
if (!section) {
clearFileBuffers({ THUMBNAIL });
2024-10-01 08:03:44 +00:00
await transaction.rollback();
2024-09-13 13:03:35 +00:00
return response(404, null, "Section not found", res);
}
2024-10-01 08:03:44 +00:00
if (NAME_SECTION && NAME_SECTION !== section.NAME_SECTION) {
const existingSection = await models.Section.findOne({
where: { NAME_SECTION },
transaction,
});
if (existingSection) {
clearFileBuffers({ THUMBNAIL });
await transaction.rollback();
return response(400, null, "Section name already exists", res);
}
section.NAME_SECTION = NAME_SECTION;
}
2024-09-13 13:03:35 +00:00
if (DESCRIPTION_SECTION) section.DESCRIPTION_SECTION = DESCRIPTION_SECTION;
if (THUMBNAIL) {
2024-09-13 13:03:35 +00:00
if (section.THUMBNAIL) {
const oldThumbnailPath = path.join(
"public/uploads/section",
section.THUMBNAIL
);
if (fs.existsSync(oldThumbnailPath)) {
fs.unlinkSync(oldThumbnailPath);
}
}
2024-10-01 08:03:44 +00:00
2024-09-13 13:03:35 +00:00
section.THUMBNAIL = saveFileToDisk(
THUMBNAIL,
"THUMBNAIL",
2024-09-13 13:03:35 +00:00
section.ID_SECTION
);
}
2024-10-01 08:03:44 +00:00
await section.save({ transaction });
await transaction.commit();
2024-09-13 13:03:35 +00:00
response(200, section, "Section updated successfully", res);
} catch (error) {
console.log(error);
2024-10-01 08:03:44 +00:00
await transaction.rollback();
clearFileBuffers({ THUMBNAIL });
2024-10-01 08:03:44 +00:00
2024-09-13 13:03:35 +00:00
response(500, null, "Internal Server Error", res);
}
};
export const deleteSectionById = async (req, res) => {
const { id } = req.params;
try {
const section = await models.Section.findByPk(id);
if (!section) {
return response(404, null, "Section not found", res);
}
if (section.THUMBNAIL) {
const thumbnailPath = path.join(
"public/uploads/section",
section.THUMBNAIL
);
if (fs.existsSync(thumbnailPath)) {
fs.unlinkSync(thumbnailPath);
}
}
await section.destroy();
response(200, null, "Section deleted successfully", res);
} catch (error) {
console.log(error);
response(500, null, "Internal Server Error", res);
}
};