Adding modules
This commit is contained in:
157
commands/bookmarkData.cjs
Normal file
157
commands/bookmarkData.cjs
Normal file
@@ -0,0 +1,157 @@
|
||||
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 postBookmark = 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 patchBookmark = 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 patchBookmarkOrder = 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 postBookmarkCategory = 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 patchBookmarkCategory = 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 patchBookmarkCategoryOrder = 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,
|
||||
postBookmark,
|
||||
patchBookmark,
|
||||
deleteBookmark,
|
||||
patchBookmarkOrder,
|
||||
getBookmarkCategories,
|
||||
postBookmarkCategory,
|
||||
patchBookmarkCategory,
|
||||
deleteBookmarkCategory,
|
||||
patchBookmarkCategoryOrder,
|
||||
};
|
||||
Reference in New Issue
Block a user