Added reading profile endpoint
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const pool = require("../../db");
|
||||
const { strictInput } = require("../../middleware/strict_input");
|
||||
const { authenticateToken } = require("../../middleware/authenticate_token");
|
||||
|
||||
async function getProfile(req, res) {
|
||||
const id = req.params.id === "me" ? req.user.id : req.params.id;
|
||||
|
||||
try {
|
||||
const { rows } = await pool.query(
|
||||
"SELECT id, name, username FROM people WHERE id = $1",
|
||||
[id],
|
||||
);
|
||||
if (rows.length === 0) return res.status(404).send("Profile not found");
|
||||
return res.status(200).json(rows[0]);
|
||||
} catch (err) {
|
||||
console.error("profile query failed", {
|
||||
message: err.message,
|
||||
code: err.code,
|
||||
detail: err.detail,
|
||||
hint: err.hint,
|
||||
});
|
||||
return res.status(500).send("Profile could not be retrieved, request failed");
|
||||
}
|
||||
}
|
||||
|
||||
router.get("/me", authenticateToken, strictInput(), getProfile);
|
||||
router.get("/:id", authenticateToken, strictInput(), getProfile);
|
||||
|
||||
module.exports = router;
|
||||
@@ -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