|
|
@ -121,15 +121,16 @@ export async function register(req, res) { |
|
|
|
|
|
|
|
|
const insertQuery = `INSERT INTO email_verification (email, token, expires_at) VALUES ($1, $2, $3)`; |
|
|
const insertQuery = `INSERT INTO email_verification (email, token, expires_at) VALUES ($1, $2, $3)`; |
|
|
await client.query(insertQuery, [user.email, token, expirationDate]); |
|
|
await client.query(insertQuery, [user.email, token, expirationDate]); |
|
|
client.end(); |
|
|
|
|
|
|
|
|
|
|
|
console.log("Successfully registered"); |
|
|
console.log("Successfully registered"); |
|
|
client.end(); |
|
|
|
|
|
logger.write("successfully registered", 200); |
|
|
logger.write("successfully registered", 200); |
|
|
res.status(200).send({ user: user }); |
|
|
res.status(200).send({ user: user }); |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log(err); |
|
|
console.log(err); |
|
|
|
|
|
logger?.write("failed to register user", 500); |
|
|
|
|
|
res.status(500).json({ error: "Internal server error" }); |
|
|
|
|
|
} finally { |
|
|
|
|
|
client.release(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -165,7 +166,7 @@ export async function verifyEmail(req, res) { |
|
|
logger.write("failed to verify email for " + email, 500); |
|
|
logger.write("failed to verify email for " + email, 500); |
|
|
res.status(500).json({ error: "Internal server error" }); |
|
|
res.status(500).json({ error: "Internal server error" }); |
|
|
} finally { |
|
|
} finally { |
|
|
client.end(); |
|
|
client.release(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -180,43 +181,49 @@ export async function login(req, res) { |
|
|
|
|
|
|
|
|
const client = await getClient(); |
|
|
const client = await getClient(); |
|
|
|
|
|
|
|
|
let query = `SELECT id, username, email, picture, password FROM users WHERE username = $1`; |
|
|
try { |
|
|
const result = await client.query(query, [user.username]); |
|
|
let query = `SELECT id, username, email, picture, password FROM users WHERE username = $1`; |
|
|
|
|
|
const result = await client.query(query, [user.username]); |
|
|
const userInBase = result.rows[0]; |
|
|
|
|
|
|
|
|
|
|
|
if (!userInBase) { |
|
|
const userInBase = result.rows[0]; |
|
|
logger.write("failed to login", 401) |
|
|
|
|
|
res.status(401).json({ error: "Invalid credentials" }); |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const isPasswordValid = await bcrypt.compare(req.body.password, userInBase.password); |
|
|
if (!userInBase) { |
|
|
|
|
|
logger.write("failed to login", 401) |
|
|
|
|
|
res.status(401).json({ error: "Invalid credentials" }); |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (!isPasswordValid) { |
|
|
const isPasswordValid = await bcrypt.compare(req.body.password, userInBase.password); |
|
|
logger.write("failed to login", 401) |
|
|
|
|
|
res.status(401).json({ error: "Invalid credentials" }); |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const payload = { |
|
|
if (!isPasswordValid) { |
|
|
id: userInBase.id, |
|
|
logger.write("failed to login", 401) |
|
|
username: userInBase.username, |
|
|
res.status(401).json({ error: "Invalid credentials" }); |
|
|
} |
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const token = jwt.sign(payload, process.env.JWT_SECRET); |
|
|
const payload = { |
|
|
|
|
|
id: userInBase.id, |
|
|
|
|
|
username: userInBase.username, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const userData = { |
|
|
const token = jwt.sign(payload, process.env.JWT_SECRET); |
|
|
id: userInBase.id, |
|
|
|
|
|
username: userInBase.username, |
|
|
|
|
|
email: userInBase.email, |
|
|
|
|
|
picture: userInBase.picture |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.write("Successfully logged in", 200); |
|
|
const userData = { |
|
|
client.end(); |
|
|
id: userInBase.id, |
|
|
res.status(200).json({ token: token, user: userData }); |
|
|
username: userInBase.username, |
|
|
|
|
|
email: userInBase.email, |
|
|
|
|
|
picture: userInBase.picture |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.write("Successfully logged in", 200); |
|
|
|
|
|
res.status(200).json({ token: token, user: userData }); |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
logger?.write("failed to login", 500); |
|
|
|
|
|
res.status(500).json({ error: "Internal server error" }); |
|
|
|
|
|
} finally { |
|
|
|
|
|
client.release(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export async function getById(req, res) { |
|
|
export async function getById(req, res) { |
|
|
@ -230,7 +237,7 @@ export async function getById(req, res) { |
|
|
const result = await client.query(query, [id]); |
|
|
const result = await client.query(query, [id]); |
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve user " + id + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve user " + id + " because it doesn't exist", 404); |
|
|
client.end() |
|
|
client.release() |
|
|
res.status(404).json({ error: "Not Found" }); |
|
|
res.status(404).json({ error: "Not Found" }); |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
@ -249,12 +256,12 @@ export async function getByUsername(req, res) { |
|
|
const result = await client.query(query, [username]); |
|
|
const result = await client.query(query, [username]); |
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve user " + username + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve user " + username + " because it doesn't exist", 404); |
|
|
client.end() |
|
|
client.release() |
|
|
res.status(404).json({ error: "Not Found" }); |
|
|
res.status(404).json({ error: "Not Found" }); |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
logger.write("successfully retrieved user " + username, 200); |
|
|
logger.write("successfully retrieved user " + username, 200); |
|
|
client.end(); |
|
|
client.release(); |
|
|
return res.status(200).json({ user: result.rows[0] }); |
|
|
return res.status(200).json({ user: result.rows[0] }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -282,7 +289,7 @@ export async function update(req, res) { |
|
|
const emailResult = await client.query(emailQuery, [user.email]); |
|
|
const emailResult = await client.query(emailQuery, [user.email]); |
|
|
if (emailResult.rows[0]) { |
|
|
if (emailResult.rows[0]) { |
|
|
logger.write("failed to update because email is already used", 400) |
|
|
logger.write("failed to update because email is already used", 400) |
|
|
client.end(); |
|
|
client.release(); |
|
|
res.status(400).json({ error: "Email already exists" }); |
|
|
res.status(400).json({ error: "Email already exists" }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -292,7 +299,7 @@ export async function update(req, res) { |
|
|
const usernameResult = await client.query(usernameQuery, [user.username]); |
|
|
const usernameResult = await client.query(usernameQuery, [user.username]); |
|
|
if (usernameResult.rows[0]) { |
|
|
if (usernameResult.rows[0]) { |
|
|
logger.write("failed to update because username is already used", 400) |
|
|
logger.write("failed to update because username is already used", 400) |
|
|
client.end(); |
|
|
client.release(); |
|
|
res.status(400).json({ error: "Username already exists" }); |
|
|
res.status(400).json({ error: "Username already exists" }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -329,11 +336,11 @@ export async function update(req, res) { |
|
|
const updateQuery = `UPDATE users SET email = $1, username = $2, password = $3, picture = $4 WHERE id = $5 RETURNING id, email, username, picture`; |
|
|
const updateQuery = `UPDATE users SET email = $1, username = $2, password = $3, picture = $4 WHERE id = $5 RETURNING id, email, username, picture`; |
|
|
const result = await client.query(updateQuery, [user.email, user.username, user.password, profilePicture, id]); |
|
|
const result = await client.query(updateQuery, [user.email, user.username, user.password, profilePicture, id]); |
|
|
logger.write("successfully updated user " + id, 200); |
|
|
logger.write("successfully updated user " + id, 200); |
|
|
client.end(); |
|
|
client.release(); |
|
|
res.status(200).json(result.rows[0]); |
|
|
res.status(200).json(result.rows[0]); |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log(err); |
|
|
console.log(err); |
|
|
client.end() |
|
|
client.release() |
|
|
res.status(500).json({ error: err }); |
|
|
res.status(500).json({ error: err }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -347,7 +354,7 @@ export async function deleteUser(req, res) { |
|
|
const query = `DELETE FROM users WHERE id = $1`; |
|
|
const query = `DELETE FROM users WHERE id = $1`; |
|
|
await client.query(query, [id]); |
|
|
await client.query(query, [id]); |
|
|
logger.write("successfully deleted user " + id); |
|
|
logger.write("successfully deleted user " + id); |
|
|
client.end(); |
|
|
client.release(); |
|
|
res.status(200).json({ message: 'User deleted' }); |
|
|
res.status(200).json({ message: 'User deleted' }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -362,13 +369,13 @@ export async function getChannel(req, res) { |
|
|
|
|
|
|
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve channel of user " + id + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve channel of user " + id + " because it doesn't exist", 404); |
|
|
client.end(); |
|
|
client.release(); |
|
|
res.status(404).json({ error: "Channel Not Found" }); |
|
|
res.status(404).json({ error: "Channel Not Found" }); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.write("successfully retrieved channel of user " + id, 200); |
|
|
logger.write("successfully retrieved channel of user " + id, 200); |
|
|
client.end(); |
|
|
client.release(); |
|
|
res.status(200).json({ channel: result.rows[0] }); |
|
|
res.status(200).json({ channel: result.rows[0] }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -415,12 +422,12 @@ export async function getHistory(req, res) { |
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve history of user " + id + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve history of user " + id + " because it doesn't exist", 404); |
|
|
res.status(404).json({ error: "History Not Found" }); |
|
|
res.status(404).json({ error: "History Not Found" }); |
|
|
client.end(); |
|
|
client.release(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.write("successfully retrieved history of user " + id, 200); |
|
|
logger.write("successfully retrieved history of user " + id, 200); |
|
|
client.end(); |
|
|
client.release(); |
|
|
res.status(200).json(videos); |
|
|
res.status(200).json(videos); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -439,11 +446,11 @@ export async function isSubscribed(req, res) { |
|
|
|
|
|
|
|
|
if (result.rows[0]) { |
|
|
if (result.rows[0]) { |
|
|
logger.write(`user ${userId} is subscribed to channel ${channelId}`, 200); |
|
|
logger.write(`user ${userId} is subscribed to channel ${channelId}`, 200); |
|
|
client.end(); |
|
|
client.release(); |
|
|
return res.status(200).json({ subscribed: true }); |
|
|
return res.status(200).json({ subscribed: true }); |
|
|
} else { |
|
|
} else { |
|
|
logger.write(`user ${userId} is not subscribed to channel ${channelId}`, 200); |
|
|
logger.write(`user ${userId} is not subscribed to channel ${channelId}`, 200); |
|
|
client.end(); |
|
|
client.release(); |
|
|
return res.status(200).json({ subscribed: false }); |
|
|
return res.status(200).json({ subscribed: false }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -459,11 +466,96 @@ export async function searchByUsername(req, res) { |
|
|
|
|
|
|
|
|
if (result.rows.length === 0) { |
|
|
if (result.rows.length === 0) { |
|
|
logger.write("no user found with username " + username, 404); |
|
|
logger.write("no user found with username " + username, 404); |
|
|
client.end(); |
|
|
client.release(); |
|
|
return res.status(404).json({ error: "User Not Found" }); |
|
|
return res.status(404).json({ error: "User Not Found" }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.write("successfully found user with username " + username, 200); |
|
|
logger.write("successfully found user with username " + username, 200); |
|
|
client.end(); |
|
|
client.release(); |
|
|
|
|
|
res.status(200).json(result.rows); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function getAllSubscriptions(req,res) { |
|
|
|
|
|
const userId = req.params.id; |
|
|
|
|
|
const client = await getClient(); |
|
|
|
|
|
const logger = req.body.logger; |
|
|
|
|
|
logger.action("try to retrieve all subscriptions of user " + userId); |
|
|
|
|
|
|
|
|
|
|
|
const query = ` |
|
|
|
|
|
SELECT |
|
|
|
|
|
subscriptions.id, |
|
|
|
|
|
channels.id AS channel_id, |
|
|
|
|
|
channels.name AS channel_name, |
|
|
|
|
|
users.picture |
|
|
|
|
|
|
|
|
|
|
|
FROM |
|
|
|
|
|
subscriptions |
|
|
|
|
|
LEFT JOIN channels ON subscriptions.channel = channels.id |
|
|
|
|
|
LEFT JOIN users ON channels.owner = users.id |
|
|
|
|
|
WHERE |
|
|
|
|
|
subscriptions.owner = $1 |
|
|
|
|
|
`;
|
|
|
|
|
|
const result = await client.query(query, [userId]); |
|
|
|
|
|
|
|
|
|
|
|
if (result.rows.length === 0) { |
|
|
|
|
|
logger.write("no subscriptions found for user " + userId, 404); |
|
|
|
|
|
client.release(); |
|
|
|
|
|
return res.status(404).json({ error: "No Subscriptions Found" }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.write("successfully retrieved all subscriptions of user " + userId, 200); |
|
|
|
|
|
client.release(); |
|
|
|
|
|
res.status(200).json(result.rows); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function getAllSubscriptionVideos(req, res) { |
|
|
|
|
|
const userId = req.params.id; |
|
|
|
|
|
const client = await getClient(); |
|
|
|
|
|
const logger = req.body.logger; |
|
|
|
|
|
logger.action("try to retrieve all subscriptions of user " + userId); |
|
|
|
|
|
|
|
|
|
|
|
const query = ` |
|
|
|
|
|
SELECT |
|
|
|
|
|
videos.id, |
|
|
|
|
|
videos.title, |
|
|
|
|
|
videos.thumbnail, |
|
|
|
|
|
channels.id AS channel, |
|
|
|
|
|
videos.visibility, |
|
|
|
|
|
videos.file, |
|
|
|
|
|
videos.format, |
|
|
|
|
|
videos.release_date, |
|
|
|
|
|
channels.id AS channel_id, |
|
|
|
|
|
channels.owner, |
|
|
|
|
|
COUNT(history.id) AS views, |
|
|
|
|
|
JSON_BUILD_OBJECT( |
|
|
|
|
|
'name', channels.name, |
|
|
|
|
|
'profilePicture', users.picture, |
|
|
|
|
|
'description', channels.description |
|
|
|
|
|
) AS creator |
|
|
|
|
|
|
|
|
|
|
|
FROM |
|
|
|
|
|
subscriptions |
|
|
|
|
|
LEFT JOIN channels ON subscriptions.channel = channels.id |
|
|
|
|
|
LEFT JOIN users ON channels.owner = users.id |
|
|
|
|
|
LEFT JOIN videos ON channels.id = videos.channel |
|
|
|
|
|
LEFT JOIN history ON videos.id = history.video |
|
|
|
|
|
|
|
|
|
|
|
WHERE |
|
|
|
|
|
subscriptions.owner = $1 |
|
|
|
|
|
GROUP BY |
|
|
|
|
|
videos.id, |
|
|
|
|
|
channels.id, |
|
|
|
|
|
users.id; |
|
|
|
|
|
`;
|
|
|
|
|
|
const result = await client.query(query, [userId]); |
|
|
|
|
|
|
|
|
|
|
|
if (result.rows.length === 0) { |
|
|
|
|
|
logger.write("no subscriptions found for user " + userId, 404); |
|
|
|
|
|
client.release(); |
|
|
|
|
|
return res.status(404).json({ error: "No Subscriptions Found" }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.write("successfully retrieved all subscriptions of user " + userId, 200); |
|
|
|
|
|
client.release(); |
|
|
res.status(200).json(result.rows); |
|
|
res.status(200).json(result.rows); |
|
|
} |
|
|
} |