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.
 
 
 
 

174 lines
5.9 KiB

import {getClient} from "../utils/database.js";
import jwt from "jsonwebtoken";
export async function create(req, res) {
const { name } = req.body;
const token = req.headers.authorization.split(' ')[1];
const userId = jwt.decode(token)["id"];
const logger = req.body.logger;
const client = await getClient();
const query = `INSERT INTO playlists (name, owner) VALUES ($1, $2) RETURNING id`;
try {
const result = await client.query(query, [name, userId]);
logger.write("Playlist created with id " + result.rows[0].id, 200);
client.end()
res.status(200).json({ id: result.rows[0].id });
} catch (error) {
logger.write("Error creating playlist: " + error.message, 500);
client.end();
res.status(500).json({ error: "Internal server error" });
}
}
export async function addVideo(req, res) {
const { id } = req.params;
const { video } = req.body;
const logger = req.body.logger;
const client = await getClient();
const query = `INSERT INTO playlist_elements (video, playlist) VALUES ($1, $2) RETURNING id`;
try {
const result = await client.query(query, [video, id]);
logger.write("Video added to playlist with id " + id, 200);
client.end();
res.status(200).json({id: result.rows[0].id});
} catch (error) {
logger.write("Error adding video to playlist: " + error.message, 500);
client.end();
res.status(500).json({error: "Internal server error"});
}
}
export async function getByUser(req, res) {
const { id } = req.params;
const logger = req.body.logger;
const client = await getClient();
const query = `
SELECT *
FROM (
SELECT playlists.id, playlists.name, playlists.owner, v.thumbnail as thumbnail,
ROW_NUMBER() OVER (PARTITION BY playlists.id ORDER BY pt.id DESC) as rn
FROM playlists
LEFT JOIN playlist_elements pt on playlist = playlists.id
LEFT JOIN videos v on pt.video = v.id
WHERE owner = $1
) ranked
WHERE rn = 1 OR rn IS NULL
`;
try {
const result = await client.query(query, [id]);
if (result.rows.length === 0) {
logger.write("No playlists found for user with id " + id, 404);
client.end();
res.status(404).json({ error: "No playlists found" });
return;
}
logger.write("Playlists retrieved for user with id " + id, 200);
client.end();
res.status(200).json(result.rows);
} catch (error) {
logger.write("Error retrieving playlists: " + error.message, 500);
client.end();
res.status(500).json({ error: "Internal server error" });
}
}
export async function getById(req, res) {
const { id } = req.params;
const logger = req.body.logger;
const client = await getClient();
const query = `SELECT * FROM playlists WHERE id = $1`;
try {
const result = await client.query(query, [id]);
if (result.rows.length === 0) {
logger.write("No playlist found with id " + id, 404);
client.end();
res.status(404).json({ error: "Playlist not found" });
return;
}
logger.write("Playlist retrieved with id " + id, 200);
client.end();
res.status(200).json(result.rows[0]);
} catch (error) {
logger.write("Error retrieving playlist: " + error.message, 500);
client.end();
res.status(500).json({ error: "Internal server error" });
}
}
export async function update(req, res) {
const { id } = req.params;
const { name } = req.body;
const logger = req.body.logger;
const client = await getClient();
const query = `UPDATE playlists SET name = $1 WHERE id = $2 RETURNING id`;
try {
const result = await client.query(query, [name, id]);
if (result.rows.length === 0) {
logger.write("No playlist found with id " + id, 404);
client.end();
res.status(404).json({ error: "Playlist not found", result: result.rows, query: query });
return;
}
logger.write("Playlist updated with id " + result.rows[0].id, 200);
client.end();
res.status(200).json({ id: result.rows[0].id });
} catch (error) {
logger.write("Error updating playlist: " + error.message, 500);
client.end();
res.status(500).json({ error: "Internal server error" });
}
}
export async function deleteVideo(req, res) {
const { id, videoId } = req.params;
const logger = req.body.logger;
const client = await getClient();
const query = `DELETE FROM playlist_elements WHERE video = $1 AND playlist = $2 RETURNING id`;
try {
const result = await client.query(query, [videoId, id]);
if (result.rows.length === 0) {
logger.write("No video found in playlist with id " + id, 404);
client.end();
res.status(404).json({ error: "Video not found in playlist" });
return;
}
logger.write("Video deleted from playlist with id " + id, 200);
client.end();
res.status(200).json({ id: result.rows[0].id });
} catch (error) {
logger.write("Error deleting video from playlist: " + error.message, 500);
client.end();
res.status(500).json({ error: "Internal server error" });
}
}
export async function del(req, res) {
const { id } = req.params;
const logger = req.body.logger;
const client = await getClient();
const query = `DELETE FROM playlists WHERE id = $1 RETURNING id`;
try {
const result = await client.query(query, [id]);
logger.write("Playlist deleted", 200);
client.end()
res.status(200).json({ "message": "playlist deleted" });
} catch (error) {
logger.write("Error deleting playlist: " + error.message, 500);
client.end();
res.status(500).json({ error: "Internal server error" });
}
}