Added liking and security measures
This commit is contained in:
@@ -8,6 +8,7 @@ const {
|
||||
imageLinkForFile,
|
||||
removeUploadedFile,
|
||||
} = require("../../middleware/post_upload");
|
||||
const driver = require("../../graph_db");
|
||||
|
||||
async function create_post(req, res) {
|
||||
try {
|
||||
@@ -23,7 +24,22 @@ async function create_post(req, res) {
|
||||
"WITH created AS (INSERT INTO posts (title, text, author_id, image_link) VALUES ($1, $2, $3, $4) RETURNING id, title, text, author_id, image_link, created_at) SELECT created.*, people.username AS author_username FROM created LEFT JOIN people ON people.id = created.author_id",
|
||||
[title, text, author_id, imageLink],
|
||||
);
|
||||
res.status(201).json(rows[0]);
|
||||
const post = rows[0];
|
||||
try {
|
||||
await driver.executeQuery(
|
||||
"CREATE (p:Post {db_id: $db_id, author_id: $author_id, created_at: datetime()})",
|
||||
{ db_id: Number(post.id), author_id: Number(post.author_id) },
|
||||
{ database: process.env.GRAPH_DB_NAME },
|
||||
);
|
||||
} catch (graphErr) {
|
||||
await pool.query("DELETE FROM posts WHERE id = $1", [post.id]).catch(() => {});
|
||||
throw graphErr;
|
||||
}
|
||||
res.status(201).json({
|
||||
...post,
|
||||
like_count: 0,
|
||||
liked_by_me: false,
|
||||
});
|
||||
} catch (err) {
|
||||
await removeUploadedFile(req.file);
|
||||
throw err;
|
||||
|
||||
Reference in New Issue
Block a user