New Features
- Add /stats/all to get ALL urls
This commit is contained in:
parent
ebb05c943a
commit
43c0becdb9
26
index.js
26
index.js
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue