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.
69 lines
2.4 KiB
69 lines
2.4 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 ('${comment.content}', ${comment.author}, ${comment.video}, '${comment.createdAt}')`;
|
|
await client.query(query);
|
|
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 = ${videoId}`;
|
|
const result = await client.query(query);
|
|
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 = ${id}`;
|
|
const result = await client.query(query);
|
|
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 = '${req.body.content}'`;
|
|
const result = await client.query(query);
|
|
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 = ${id}`;
|
|
const result = await client.query(query);
|
|
logger.write("successfully deleted comment", 200);
|
|
res.status(200).json(result.rows[0]);
|
|
}
|