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)`; await client.query(query, [comment.content, comment.author, comment.video, comment.createdAt]); logger.write("successfully post comment", 200); res.status(200).send("successfully post comment"); } 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); 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); 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); 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); res.status(200).json(result.rows[0]); }