New Features

- Add /stats/all to get ALL urls
This commit is contained in:
Christopher Cookman 2024-08-16 01:11:53 -06:00
parent ebb05c943a
commit 43c0becdb9
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -47,6 +47,29 @@ app.get('/stats/:shortUrl', (req, res) => { // Stats
}
const shortUrl = req.params.shortUrl;
if (shortUrl === 'all') {
// Get all URLs and refs. nest refs inside urls
db.all('SELECT * FROM urls', (err, rows) => {
if (err) {
console.error(err);
return res.sendStatus(500);
}
rows.forEach((row, i) => {
db.all('SELECT * FROM refs WHERE shortUrl = ?', [row.shortUrl], (err, refs) => {
if (err) {
console.error(err);
return res.sendStatus(500);
}
rows[i].refs = refs;
if (i === rows.length - 1) {
res.json(rows);
}
});
});
});
} else {
db.get('SELECT * FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => {
if (err) {
console.error(err);
@ -62,9 +85,10 @@ app.get('/stats/:shortUrl', (req, res) => { // Stats
});
}
else {
res.sendStatus(404);
res.send("Code not found").status(404);
}
});
}
});
app.post('/shorten', (req, res) => { // Shorten URL