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.
 
 
 
 

138 lines
4.7 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 ('${channel.name}', '${channel.description}', ${channel.owner})`;
await client.query(query);
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 = ${id}`;
const result = await client.query(query);
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 = ${channel.id}`;
const subResult = await client.query(subQuery);
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 = ${req.params.id}`;
const channelResult = await client.query(channelQuery);
const channelInBase = channelResult.rows[0];
if (channelInBase.name !== channel.name) {
const nameQuery = `SELECT name FROM channels WHERE name = '${channel.name}'`;
const nameResult = await client.query(nameQuery);
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 = '${channel.name}', description = '${channel.description}' WHERE id = ${id}`;
await client.query(updateQuery);
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 = ${id}`;
await client.query(query);
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 = ${id} AND owner = ${userId}`;
const result = await client.query(query);
if (result.rows.length > 0) {
// Unsubscribe
const deleteQuery = `DELETE FROM subscriptions WHERE channel = ${id} AND owner = ${userId}`;
await client.query(deleteQuery);
logger.write("Successfully unsubscribed from channel", 200);
res.status(200).json({message: 'Unsubscribed successfully'});
} else {
// Subscribe
const insertQuery = `INSERT INTO subscriptions (channel, owner) VALUES (${id}, ${userId})`;
await client.query(insertQuery);
logger.write("Successfully subscribed to channel", 200);
res.status(200).json({message: 'Subscribed successfully'});
}
}