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 || {}; if (!NAME_SECTION) { clearFileBuffers({ THUMBNAIL }); return response(400, null, "Section name is required", res); } if (!DESCRIPTION_SECTION) { clearFileBuffers({ THUMBNAIL }); return response(400, null, "Description is required", res); } try { const newSection = await models.Section.create({ NAME_SECTION, DESCRIPTION_SECTION, THUMBNAIL: null, }); const thumbnailFilename = THUMBNAIL ? saveFileToDisk(THUMBNAIL, "THUMBNAIL", newSection.ID_SECTION) : null; newSection.THUMBNAIL = thumbnailFilename; await newSection.save(); response(201, newSection, "Section created successfully", res); } catch (error) { console.log(error); clearFileBuffers({ THUMBNAIL }); 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 || {}; try { const section = await models.Section.findByPk(id); if (!section) { clearFileBuffers({ THUMBNAIL }); return response(404, null, "Section not found", res); } if (NAME_SECTION) section.NAME_SECTION = NAME_SECTION; if (DESCRIPTION_SECTION) section.DESCRIPTION_SECTION = DESCRIPTION_SECTION; if (THUMBNAIL) { if (section.THUMBNAIL) { const oldThumbnailPath = path.join( "public/uploads/section", section.THUMBNAIL ); if (fs.existsSync(oldThumbnailPath)) { fs.unlinkSync(oldThumbnailPath); } } section.THUMBNAIL = saveFileToDisk( THUMBNAIL, "THUMBNAIL", section.ID_SECTION ); } await section.save(); response(200, section, "Section updated successfully", res); } catch (error) { console.log(error); clearFileBuffers({ THUMBNAIL }); 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); } };