158 lines
4.4 KiB
JavaScript
158 lines
4.4 KiB
JavaScript
const { pool } = require("../triggerBackend.cjs");
|
|
const escapeString = require("sql-escape-string");
|
|
|
|
const getBookmarks = async (_req, res) => {
|
|
const conn = await pool.getConnection();
|
|
const query = "SELECT * FROM trigger_database.bookmarks;";
|
|
conn.query(query).then((rows) => {
|
|
res.status(200).send(rows);
|
|
});
|
|
conn.end();
|
|
};
|
|
|
|
const createBookmark = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
const query = `INSERT INTO trigger_database.bookmarks (name, url, category, \`index\`, enabled) VALUES (${escapeString(req.body.name)}, ${escapeString(req.body.url)}, ${escapeString(req.body.category)}, ${req.body.index}, ${req.body.enabled});`;
|
|
conn
|
|
.query(query)
|
|
.then((_rows) => {
|
|
res.status(200).send({ status: "success" });
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({ error: err });
|
|
});
|
|
conn.end();
|
|
};
|
|
|
|
const updateBookmark = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
const query = `UPDATE trigger_database.bookmarks SET name = ${escapeString(req.body.name)}, url = ${escapeString(req.body.url)}, category = ${escapeString(req.body.category)}, \`index\` = ${req.body.index}, enabled = ${req.body.enabled} WHERE id = ${req.body.id}`;
|
|
conn
|
|
.query(query)
|
|
.then((_rows) => {
|
|
res.status(200).send({ status: "success" });
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({ error: err });
|
|
});
|
|
conn.end();
|
|
};
|
|
|
|
const deleteBookmark = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
if (req.body.length > 1) {
|
|
req.body.map(async (link) => {
|
|
return conn
|
|
.query("DELETE FROM trigger_database.bookmarks where id IN (?)", [
|
|
link.id,
|
|
])
|
|
.then(() => {
|
|
res.status(200);
|
|
});
|
|
});
|
|
res.send({ status: "success" });
|
|
} else {
|
|
const query = `DELETE FROM trigger_database.bookmarks WHERE id = ${req.body.id}`;
|
|
conn
|
|
.query(query)
|
|
.then(() => {
|
|
res.status(200).send({ status: "success" });
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({ error: err });
|
|
});
|
|
}
|
|
conn.end();
|
|
};
|
|
|
|
const updateBookmarkOrder = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
req.body.map(async (link) => {
|
|
return conn
|
|
.query(
|
|
"UPDATE trigger_database.bookmarks SET `index` = ? where id IN (?)",
|
|
[link.index, link.id],
|
|
)
|
|
.then(() => {
|
|
res.status(200);
|
|
});
|
|
});
|
|
res.send({ status: "success" });
|
|
conn.end();
|
|
};
|
|
|
|
const getBookmarkCategories = async (_req, res) => {
|
|
const conn = await pool.getConnection();
|
|
const query = "SELECT * from trigger_database.bookmarkCategories;";
|
|
conn.query(query).then((rows) => {
|
|
res.status(200).send(rows);
|
|
});
|
|
conn.end();
|
|
};
|
|
|
|
const createBookmarkCategory = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
const query = `INSERT INTO trigger_database.bookmarkCategories (categoryName, categoryIndex, filterName) VALUES (${escapeString(req.body.categoryName)},${req.body.categoryIndex || 0},${escapeString(req.body.filterName)})`;
|
|
conn.query(query).then((_rows) => {
|
|
res.status(200).send({ status: "success" });
|
|
});
|
|
conn.end();
|
|
};
|
|
|
|
const updateBookmarkCategory = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
const query = `UPDATE trigger_database.bookmarkCategories SET categoryName = ${escapeString(req.body.categoryName)}, filterName = ${escapeString(req.body.filterName)}, enabled = ${req.body.enabled} WHERE id = ${req.body.id};`;
|
|
conn
|
|
.query(query)
|
|
.then((_rows) => {
|
|
res.status(200).send({ status: "success" });
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({ error: err });
|
|
});
|
|
conn.end();
|
|
};
|
|
|
|
const deleteBookmarkCategory = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
const query = `DELETE FROM trigger_database.bookmarkCategories WHERE id = ${req.body.id}`;
|
|
conn
|
|
.query(query)
|
|
.then((_rows) => {
|
|
res.status(200).send({ status: "success" });
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({ error: err });
|
|
});
|
|
conn.end();
|
|
};
|
|
|
|
const updateBookmarkCategoryOrder = async (req, res) => {
|
|
const conn = await pool.getConnection();
|
|
req.body.map(async (category) => {
|
|
return conn
|
|
.query(
|
|
"UPDATE trigger_database.bookmarkCategories SET categoryIndex = ? where id IN (?)",
|
|
[category.index, category.id],
|
|
)
|
|
.then((_rows) => {
|
|
res.status(200);
|
|
});
|
|
});
|
|
res.send({ status: "success" });
|
|
conn.end();
|
|
};
|
|
|
|
module.exports = {
|
|
getBookmarks,
|
|
createBookmark,
|
|
updateBookmark,
|
|
deleteBookmark,
|
|
updateBookmarkOrder,
|
|
getBookmarkCategories,
|
|
createBookmarkCategory,
|
|
updateBookmarkCategory,
|
|
deleteBookmarkCategory,
|
|
updateBookmarkCategoryOrder,
|
|
};
|