You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.1 KiB
93 lines
3.1 KiB
import jwt from "jsonwebtoken";
|
|
import {getClient} from "../utils/database.js";
|
|
|
|
|
|
export async function upload(req, res) {
|
|
const token = req.headers.authorization.split(' ')[1];
|
|
const claims = jwt.decode(token);
|
|
|
|
const comment = {
|
|
content: req.body.content,
|
|
video: req.body.video,
|
|
author: claims.id,
|
|
createdAt: new Date(Date.now()).toISOString(),
|
|
}
|
|
|
|
const logger = req.body.logger;
|
|
logger.action("try to post comment");
|
|
|
|
const client = await getClient();
|
|
const query = `INSERT INTO comments (content, author, video, created_at) VALUES ($1, $2, $3, $4) RETURNING id, created_at`;
|
|
const result = await client.query(query, [comment.content, comment.author, comment.video, comment.createdAt]);
|
|
logger.write("successfully post comment", 200);
|
|
|
|
const createdAt = result.rows[0].created_at;
|
|
comment.id = result.rows[0].id;
|
|
|
|
// Send back the comment
|
|
// Get the author's name and profile picture
|
|
const authorQuery = `SELECT username, picture FROM users WHERE id = $1`;
|
|
const authorResult = await client.query(authorQuery, [comment.author]);
|
|
const author = authorResult.rows[0];
|
|
|
|
const responseComment = {
|
|
id: comment.id,
|
|
content: comment.content,
|
|
video: comment.video,
|
|
username: author.username,
|
|
picture: author.picture,
|
|
createdAt: createdAt
|
|
}
|
|
|
|
client.release();
|
|
res.status(200).json(responseComment);
|
|
|
|
}
|
|
|
|
export async function getByVideo(req, res) {
|
|
const videoId = req.params.id;
|
|
const logger = req.body.logger;
|
|
logger.action("try to get comment from video " + videoId);
|
|
const client = await getClient();
|
|
const query = `SELECT * FROM comments WHERE video = $1`;
|
|
const result = await client.query(query, [videoId]);
|
|
logger.write("successfully get comment", 200);
|
|
client.release()
|
|
res.status(200).json(result.rows);
|
|
}
|
|
|
|
export async function getById(req, res) {
|
|
const id = req.params.id;
|
|
const logger = req.body.logger;
|
|
logger.action("try to get comment " + id);
|
|
const client = await getClient();
|
|
const query = `SELECT * FROM comments WHERE id = $1`;
|
|
const result = await client.query(query, [id]);
|
|
logger.write("successfully get comment", 200);
|
|
client.release();
|
|
res.status(200).json(result.rows[0]);
|
|
}
|
|
|
|
export async function update(req, res) {
|
|
const id = req.params.id;
|
|
const logger = req.body.logger;
|
|
logger.action("try to update comment " + id);
|
|
const client = await getClient();
|
|
const query = `UPDATE comments SET content = $1 WHERE id = $2`;
|
|
const result = await client.query(query, [req.body.content, id]);
|
|
logger.write("successfully update comment", 200);
|
|
client.release();
|
|
res.status(200).json(result.rows[0]);
|
|
}
|
|
|
|
export async function del(req, res) {
|
|
const id = req.params.id;
|
|
const logger = req.body.logger;
|
|
logger.action("try to delete comment " + id);
|
|
const client = await getClient();
|
|
const query = `DELETE FROM comments WHERE id = $1`;
|
|
const result = await client.query(query, [id]);
|
|
logger.write("successfully deleted comment", 200);
|
|
client.release();
|
|
res.status(200).json(result.rows[0]);
|
|
}
|