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.
239 lines
8.2 KiB
239 lines
8.2 KiB
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.release();
|
|
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 channels.*, u.username, u.picture, COUNT(s.id) as subscriptions
|
|
FROM channels
|
|
JOIN public.users u ON channels.owner = u.id
|
|
LEFT JOIN public.subscriptions s ON channels.id = s.channel
|
|
WHERE channels.id = $1
|
|
GROUP BY channels.id, name, description, channels.owner, u.username, u.picture
|
|
`;
|
|
|
|
const result = await client.query(query, [id]);
|
|
|
|
const videoQuery = `
|
|
SELECT
|
|
videos.id,
|
|
videos.title,
|
|
videos.description AS video_description,
|
|
videos.thumbnail,
|
|
videos.channel,
|
|
videos.visibility,
|
|
videos.file,
|
|
videos.slug,
|
|
videos.format,
|
|
videos.release_date,
|
|
channels.name AS name,
|
|
channels.description AS description,
|
|
users.picture AS profilePicture,
|
|
COUNT(h.id) AS views,
|
|
COUNT(likes.id) AS likes,
|
|
COUNT(c.id) AS comments
|
|
FROM public.videos
|
|
LEFT JOIN public.channels ON videos.channel = channels.id
|
|
LEFT JOIN public.users ON channels.OWNER = users.id
|
|
LEFT JOIN public.history h ON h.video = videos.id
|
|
LEFT JOIN public.likes ON likes.video = videos.id
|
|
LEFT JOIN public.comments c ON c.video = videos.id
|
|
WHERE videos.channel = $1 AND videos.visibility = 'public'
|
|
GROUP BY videos.id, channels.name, channels.description, users.username, users.picture
|
|
`;
|
|
|
|
const videoResult = await client.query(videoQuery, [id]);
|
|
|
|
const videoReturn = [];
|
|
|
|
for (const video of videoResult.rows) {
|
|
video.creator = {
|
|
name: video.name,
|
|
profilePicture: video.profilepicture,
|
|
description: video.video_description
|
|
};
|
|
delete video.name;
|
|
delete video.profilepicture;
|
|
delete video.video_description;
|
|
videoReturn.push(video);
|
|
}
|
|
|
|
result.rows[0].videos = videoReturn;
|
|
|
|
logger.write("Successfully get channel with id " + id, 200);
|
|
client.release();
|
|
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.release();
|
|
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.release();
|
|
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.release();
|
|
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.release();
|
|
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) {
|
|
const deleteQuery = `DELETE FROM subscriptions WHERE channel = $1 AND owner = $2`;
|
|
await client.query(deleteQuery, [id, userId]);
|
|
|
|
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.release();
|
|
res.status(200).json({message: 'Unsubscribed successfully', subscriptions: remainingSubscriptions});
|
|
} else {
|
|
const insertQuery = `INSERT INTO subscriptions (channel, owner) VALUES ($1, $2)`;
|
|
await client.query(insertQuery, [id, userId]);
|
|
|
|
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.release();
|
|
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.release();
|
|
res.status(200).json(result.rows[0]);
|
|
} catch (error) {
|
|
|
|
res.status(500).json({error: error.message});
|
|
}
|
|
|
|
}
|