API Endpoint to get all short urls
This commit is contained in:
		| @@ -221,7 +221,7 @@ fastify.after(() => { | |||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     //Get url api route |     //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; |         const shortcode = req.params.shortcode; | ||||||
|  |  | ||||||
|         //This should never happen but better safe than 500 |         //This should never happen but better safe than 500 | ||||||
| @@ -237,6 +237,23 @@ fastify.after(() => { | |||||||
|         return true; |         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; | ||||||
|  |     }); | ||||||
|  |  | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user