API Endpoint to get all short urls

This commit is contained in:
Nicolai Ort 2021-08-14 11:50:29 +02:00
parent 91b25c22ca
commit fa01864d0d
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
1 changed files with 18 additions and 1 deletions

View File

@ -221,7 +221,7 @@ fastify.after(() => {
});
//Get url api route
fastify.delete('/api/:shortcode', async (req, res) => {
fastify.delete('/api/:shortcode', { onRequest: fastify.basicAuth }, async (req, res) => {
const shortcode = req.params.shortcode;
//This should never happen but better safe than 500
@ -237,6 +237,23 @@ fastify.after(() => {
return true;
});
//Get all urls api route
fastify.get('/api', { onRequest: fastify.basicAuth }, async (req, res) => {
urls = await knex.select('target', 'shortcode')
.from('urls');
for (let url of urls) {
url.url = `${config.getBaseUrl()}/${url.shortcode}`
if(req.query.showVisits){
url.visits = (await knex.select('timestamp')
.from('visits')
.where('shortcode', '=', url.shortcode)).length;
}
}
return urls;
});
});