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.
144 lines
5.1 KiB
144 lines
5.1 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 ('${name}', ${userId}) RETURNING id`;
|
|
|
|
try {
|
|
const result = await client.query(query);
|
|
logger.write("Playlist created with id " + result.rows[0].id, 200);
|
|
res.status(200).json({ id: result.rows[0].id });
|
|
} catch (error) {
|
|
logger.write("Error creating playlist: " + error.message, 500);
|
|
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 (${video}, ${id}) RETURNING id`;
|
|
try {
|
|
const result = await client.query(query);
|
|
logger.write("Video added to playlist with id " + id, 200);
|
|
res.status(200).json({id: result.rows[0].id});
|
|
} catch (error) {
|
|
logger.write("Error adding video to playlist: " + error.message, 500);
|
|
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 playlists WHERE owner = ${id}`;
|
|
|
|
try {
|
|
const result = await client.query(query);
|
|
if (result.rows.length === 0) {
|
|
logger.write("No playlists found for user with id " + id, 404);
|
|
res.status(404).json({ error: "No playlists found" });
|
|
return;
|
|
}
|
|
logger.write("Playlists retrieved for user with id " + id, 200);
|
|
res.status(200).json(result.rows);
|
|
} catch (error) {
|
|
logger.write("Error retrieving playlists: " + error.message, 500);
|
|
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 = ${id}`;
|
|
|
|
try {
|
|
const result = await client.query(query);
|
|
if (result.rows.length === 0) {
|
|
logger.write("No playlist found with id " + id, 404);
|
|
res.status(404).json({ error: "Playlist not found" });
|
|
return;
|
|
}
|
|
logger.write("Playlist retrieved with id " + id, 200);
|
|
res.status(200).json(result.rows[0]);
|
|
} catch (error) {
|
|
logger.write("Error retrieving playlist: " + error.message, 500);
|
|
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 = '${name}' WHERE id = ${id} RETURNING id`;
|
|
|
|
try {
|
|
const result = await client.query(query);
|
|
if (result.rows.length === 0) {
|
|
logger.write("No playlist found with id " + id, 404);
|
|
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);
|
|
res.status(200).json({ id: result.rows[0].id });
|
|
} catch (error) {
|
|
logger.write("Error updating playlist: " + error.message, 500);
|
|
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 = ${videoId} AND playlist = ${id} RETURNING id`;
|
|
|
|
try {
|
|
const result = await client.query(query);
|
|
if (result.rows.length === 0) {
|
|
logger.write("No video found in playlist with id " + id, 404);
|
|
res.status(404).json({ error: "Video not found in playlist" });
|
|
return;
|
|
}
|
|
logger.write("Video deleted from playlist with id " + id, 200);
|
|
res.status(200).json({ id: result.rows[0].id });
|
|
} catch (error) {
|
|
logger.write("Error deleting video from playlist: " + error.message, 500);
|
|
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 = ${id} RETURNING id`;
|
|
|
|
try {
|
|
logger.write("Playlist deleted", 200);
|
|
res.status(200).json({ "message": "playlist deleted" });
|
|
} catch (error) {
|
|
logger.write("Error deleting playlist: " + error.message, 500);
|
|
res.status(500).json({ error: "Internal server error" });
|
|
}
|
|
}
|