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.
 
 
 
 

149 lines
5.4 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);
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 id = $1`;
const result = await client.query(query, [id]);
logger.write("Successfully get channel with id " + id, 200);
res.status(200).json(result);
}
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);
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);
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);
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);
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);
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);
res.status(200).json({message: 'Subscribed successfully', subscriptions: totalSubscriptions});
}
}