Added reading profile endpoint
This commit is contained in:
@@ -20,7 +20,7 @@ async function create_post(req, res) {
|
||||
const imageLink = imageLinkForFile(req.file);
|
||||
try {
|
||||
const { rows } = await pool.query(
|
||||
"INSERT INTO posts (title, text, author_id, image_link) VALUES ($1, $2, $3, $4) RETURNING id, title, text, author_id, image_link",
|
||||
"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]);
|
||||
|
||||
@@ -58,7 +58,7 @@ async function editPost(req, res) {
|
||||
let updated;
|
||||
try {
|
||||
updated = await pool.query(
|
||||
`UPDATE posts SET ${fields.join(", ")} WHERE id = $${values.length - 1} AND author_id = $${values.length} RETURNING id, title, text, author_id, image_link`,
|
||||
`WITH updated AS (UPDATE posts SET ${fields.join(", ")} WHERE id = $${values.length - 1} AND author_id = $${values.length} RETURNING id, title, text, author_id, image_link, created_at) SELECT updated.*, people.username AS author_username FROM updated LEFT JOIN people ON people.id = updated.author_id`,
|
||||
values,
|
||||
);
|
||||
} catch (err) {
|
||||
|
||||
@@ -9,8 +9,8 @@ const path = require("path");
|
||||
async function getPosts(req, res, ownOnly) {
|
||||
try {
|
||||
const query = ownOnly
|
||||
? "SELECT id, title, text, author_id, image_link FROM posts WHERE author_id = $1"
|
||||
: "SELECT id, title, text, author_id, image_link FROM posts";
|
||||
? "SELECT p.id, p.title, p.text, p.author_id, people.username AS author_username, p.image_link, p.created_at FROM posts p LEFT JOIN people ON people.id = p.author_id WHERE p.author_id = $1"
|
||||
: "SELECT p.id, p.title, p.text, p.author_id, people.username AS author_username, p.image_link, p.created_at FROM posts p LEFT JOIN people ON people.id = p.author_id";
|
||||
const values = ownOnly ? [req.user.id] : [];
|
||||
const { rows } = await pool.query(query, values);
|
||||
res.status(200).json(rows);
|
||||
|
||||
Reference in New Issue
Block a user