import {getClient} from "../utils/database.js"; import {query} from "express-validator"; export async function create(req, res) { const channel = { name: req.body.name, description: req.body.description, owner: req.body.owner, } const client = await getClient(); const query = `INSERT INTO channels (name, description, owner) VALUES ($1, $2, $3)`; await client.query(query, [channel.name, channel.description, channel.owner]); const logger = req.body.logger; logger.action("try to create new channel with owner " + channel.owner + " and name " + channel.name); logger.write("Successfully created new channel with name " + channel.name, 200); client.end(); res.status(200).json(channel); } export async function getById(req, res) { const id = req.params.id; const logger = req.body.logger; logger.action("try to get channel with id " + id); const client = await getClient(); const query = ` SELECT * FROM channels WHERE channels.id = $1 `; const result = await client.query(query, [id]); const videoQuery = ` SELECT v.*, COUNT(h.video) as views, COUNT(l.id) as likes, COUNT(c.id) as comments FROM videos v LEFT JOIN history h ON v.id = h.video LEFT JOIN likes l ON v.id = l.video LEFT JOIN comments c ON v.id = c.video WHERE v.channel = $1 GROUP BY v.id, title, thumbnail, description, channel, visibility, file, slug, format, release_date `; const videoResult = await client.query(videoQuery, [id]); result.rows[0].videos = videoResult.rows; logger.write("Successfully get channel with id " + id, 200); client.end(); res.status(200).json(result.rows[0]); } export async function getAll(req, res) { const logger = req.body.logger; logger.action("try to get all channels"); const client = await getClient(); const query = `SELECT * FROM channels`; const channels = await client.query(query); let result = [] for (const channelNbr in channels.rows) { const channel = channels.rows[channelNbr]; const subQuery = `SELECT * FROM subscriptions WHERE channel = $1`; const subResult = await client.query(subQuery, [channel.id]); if (subResult.rows.length > 0) { channel.subscriptions = subResult.rows.length; result.push(channel); } else { channel.subscriptions = 0; result.push(channel); } } result.sort((a, b) => { var keyA = a.subscriptions; var keyB = b.subscriptions; if (keyA > keyB) return 1; if (keyA < keyB) return -1; }) logger.write("Successfully get all channels", 200); client.end(); res.status(200).json(result); } export async function update(req, res) { const id = req.params.id; const channel = { name: req.body.name, description: req.body.description, } const client = await getClient(); const logger = req.body.logger; logger.action("try to update channel with id " + req.params.id); const channelQuery = `SELECT * FROM channels WHERE id = $1`; const channelResult = await client.query(channelQuery, [req.params.id]); const channelInBase = channelResult.rows[0]; if (channelInBase.name !== channel.name) { const nameQuery = `SELECT name FROM channels WHERE name = $1`; const nameResult = await client.query(nameQuery, [channel.name]); if (nameResult.rows.length > 0) { logger.write("failed to update channel because name already taken", 400); client.end(); res.status(400).json({error: 'Name already used'}); return } } const updateQuery = `UPDATE channels SET name = $1, description = $2 WHERE id = $3`; await client.query(updateQuery, [channel.name, channel.description, id]); logger.write("Successfully updated channel", 200); client.end(); res.status(200).json(channel); } export async function del(req, res) { const id = req.params.id; const logger = req.body.logger; logger.action("try to delete channel with id " + id); const client = await getClient(); const query = `DELETE FROM channels WHERE id = $1`; await client.query(query, [id]); logger.write("Successfully deleted channel", 200); client.end(); res.status(200).json({message: 'Successfully deleted'}); } export async function toggleSubscription(req, res) { const id = req.params.id; const userId = req.body.userId; const logger = req.body.logger; logger.action("try to toggle subscription for channel with id " + id); const client = await getClient(); const query = `SELECT * FROM subscriptions WHERE channel = $1 AND owner = $2`; const result = await client.query(query, [id, userId]); if (result.rows.length > 0) { // Unsubscribe const deleteQuery = `DELETE FROM subscriptions WHERE channel = $1 AND owner = $2`; await client.query(deleteQuery, [id, userId]); // Send back the number of remaining subscriptions const countQuery = `SELECT COUNT(*) FROM subscriptions WHERE channel = $1`; const countResult = await client.query(countQuery, [id]); const remainingSubscriptions = countResult.rows[0].count; logger.write("Successfully unsubscribed from channel", 200); client.end(); res.status(200).json({message: 'Unsubscribed successfully', subscriptions: remainingSubscriptions}); } else { // Subscribe const insertQuery = `INSERT INTO subscriptions (channel, owner) VALUES ($1, $2)`; await client.query(insertQuery, [id, userId]); // Send back the number of subscriptions after subscribing const countQuery = `SELECT COUNT(*) FROM subscriptions WHERE channel = $1`; const countResult = await client.query(countQuery, [id]); const totalSubscriptions = countResult.rows[0].count; logger.write("Successfully subscribed to channel", 200); client.end(); res.status(200).json({message: 'Subscribed successfully', subscriptions: totalSubscriptions}); } } export async function getStats(req, res) { try { const id = req.params.id; const logger = req.body.logger; logger.action("try to get stats"); const request = ` SELECT (SELECT COUNT(*) FROM subscriptions WHERE channel = $1) as subscribers, (SELECT COUNT(*) FROM history h JOIN videos v ON h.video = v.id WHERE v.channel = $1) as views FROM channels LEFT JOIN public.subscriptions s on channels.id = s.channel LEFT JOIN public.videos v on channels.id = v.channel LEFT JOIN public.history h on v.id = h.video WHERE channels.id = $1 `; const client = await getClient(); const result = await client.query(request, [id]); logger.write("Successfully get stats", 200); client.end(); res.status(200).json(result.rows[0]); } catch (error) { console.log(error); res.status(500).json({error: error.message}); } }