const db = require("../../config/database") const getUsersPaged = async (req, res) => { try { const page = parseInt(req.query.page) || 2 const pageSize = parseInt(req.query.size) || 10 const offset = (page - 1) * pageSize const results = await executeQuery( `SELECT * FROM forma LIMIT ${pageSize} OFFSET ${offset}` ) return res.json({ content: results}) } catch (error) { console.error(error) return res.status(500).json({ success: 0, message: "Error fetching content from the contentbase", error: error.message, }) } } const getUsers = async (req, res) => { try { const results = await executeQuery("SELECT * FROM forma", []) return res.json(results) } catch (error) { console.error(error) return res.status(500).json({ success: 0, message: "Error fetching content from the contentbase", error: error.message, }) } } const getUsersByUserId = async (req, res) => { const id = req.params.id try { const results = await executeQuery( "SELECT * FROM forma WHERE id = ?", [id] ) if (!results || results.length === 0) { return res.json({ success: 0, message: "Record not found", }) } return res.json({ success: 1, content: results, }) } catch (error) { console.error(error) return res.status(500).json({ success: 0, message: "Error fetching content from the contentbase", error: error.message, }) } } const createUser = async (req, res) => { const body = req.body try { const params = [ body.namem, body.filetest, body.imagtest, ] // Replace undefined with null in parameters const sanitizedParams = params.map((param) => param !== undefined ? param : null ) await executeQuery( ` INSERT INTO forma SET namem = ?, filetest = ?, imagtest = ? `, sanitizedParams ) return res.json({ success: 1, content: "user created successfully", }) } catch (error) { console.error(error) return res.status(500).json({ success: 0, message: "Error creating user", error: error.message, }) } } const updateUsers = async (req, res) => { const body = req.body; const id = req.params.id; try { // Ensure that required parameters exist and replace undefined with null const params = [body.namem, body.filetest, body.imagtest, id, ]; const sanitizedParams = params.map((param) => (param !== undefined ? param : null)); const results = await executeQuery( ` UPDATE forma SET namem = ?, filetest = ?, imagtest = ? WHERE id = ? `, sanitizedParams ); if (!results) { return res.json({ success: 0, message: "Failed to update user", }); } return res.json({ success: 1, content: "Updated successfully", }); } catch (error) { console.error(error); return res.status(500).json({ success: 0, message: "Error updating user", error: error.message, }); } }; const deleteUsers = async (req, res) => { const id = req.params.id try { await executeQuery("DELETE FROM forma WHERE id = ?", [id]) return res.json({ success: 1, content: "user deleted successfully", }) } catch (error) { console.error(error) return res.status(500).json({ success: 0, message: "Error deleting user", error: error.message, }) } } module.exports = { getUsers, getUsersPaged, getUsersByUserId, createUser, updateUsers, deleteUsers, } async function executeQuery(sql, values) { const connection = await db.getConnection() try { const [rows] = await connection.execute(sql, values) return rows } finally { connection.release() } }