Skip to content

Commit

Permalink
Implemented follow/unfollow in webapp. Implemented deleteRows helper …
Browse files Browse the repository at this point in the history
…method in webapp.
  • Loading branch information
emir authored and kindaninja committed Feb 26, 2020
1 parent 7f202cf commit e4e9eeb
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions webapp/minitwit.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,30 @@ app.get('/:username', async function(req, res) {
});
});

app.get('/:username/follow', async function(req, res) {
const { username } = req.params;
if (!req.session.user_id)
res.status(401).send();
let whom = await selectOne('SELECT * FROM user WHERE username = ?',[username]);
if (!whom)
res.status(404).send();
await insertOne('INSERT INTO follower (who_id, whom_id) VALUES (?, ?)', [req.session.user_id, whom.user_id])

return res.redirect('/' + username);
});

app.get('/:username/unfollow', async function(req, res) {
const { username } = req.params;
if (!req.session.user_id)
res.status(401).send();
let whom = await selectOne('SELECT * FROM user WHERE username = ?',[username]);
if (!whom)
res.status(404).send();
await deleteRows('DELETE FROM follower WHERE who_id = ? AND whom_id = ?', [req.session.user_id, whom.user_id]);

return res.redirect('/' + username);
});

// Start application
app.listen(8080);
console.log('MiniTwit is running on port 8080..');
Expand Down Expand Up @@ -275,6 +299,20 @@ function insertOne(query, params) {
});
}

// Delete row(s)
function deleteRows(query, params) {
return new Promise((resolve, reject) => {
db.run(query, params, function(err) {
if (err) {
console.error(err.message);
reject();
}
// Return the deleted
resolve(this.changes);
});
});
}

// Lame password hash function
// TODO Use proper hashing library, bcrypt maybe?
String.prototype.lameHash = function() {
Expand Down

0 comments on commit e4e9eeb

Please sign in to comment.