From b4760b9c1c749132aacf4cb3883d7ecfa3cda6fb Mon Sep 17 00:00:00 2001 From: Jatin Katyal Date: Sun, 16 Dec 2018 18:35:52 +0530 Subject: [PATCH] APIs complete Signed-off-by: Jatin Katyal --- src/routes/api/urls.ts | 47 ++++++++++++- views/pages/urls/index.hbs | 136 ++++++++++--------------------------- 2 files changed, 79 insertions(+), 104 deletions(-) diff --git a/src/routes/api/urls.ts b/src/routes/api/urls.ts index 2fd6939..296ac77 100644 --- a/src/routes/api/urls.ts +++ b/src/routes/api/urls.ts @@ -1,8 +1,16 @@ import { ensureLoggedIn } from 'connect-ensure-login' import { Router } from 'express' import querystring from 'querystring' -import { getAllUrlsForUser, PageOptions } from '../../controllers/urls' +import Raven from 'raven' +import { findGroupById, findGroupByPrefix } from '../../controllers/groups' +import { + findUrlByCodeInt, + findUrlByShortcode, + getAllUrlsForUser, + PageOptions, +} from '../../controllers/urls' import paginationMiddleware from '../../middlewares/pagination' +import { optsFromGroupedShortcode } from '../../utils/shortener' export const route = Router() @@ -12,6 +20,12 @@ route.use(ensureLoggedIn('/login')) // Pagination Middleware route.use(paginationMiddleware) +// All are json responses +route.use((req, res, next) => { + res.header('Content-Type', 'application/json') + next() +}) + route.get('/', async (req, res) => { const page: PageOptions = { offset: res.locals.pagination.limit * (res.locals.pagination.page - 1), @@ -40,6 +54,35 @@ route.get('/', async (req, res) => { }) } - res.header('Content-Type', 'application/json') res.send(JSON.stringify(urlsAndPagination)) }) + +route.get('/:url', async (req, res) => { + try { + const url = await findUrlByShortcode(req.params.url) + + res.send(JSON.stringify(url)) + } catch (e) { + Raven.captureException(e) + res.send(e) + } +}) + +route.get('/:group/:url', async (req, res) => { + try { + const group = await findGroupByPrefix(req.params.group) + if (!group) { + throw new Error('Group prefix does not exist') + } + const opts = optsFromGroupedShortcode(group, req.params.url) + const url = await findUrlByCodeInt(opts.codeInt) + if (!url) { + throw new Error('Shortcode does not exist') + } + + res.send(JSON.stringify(url)) + } catch (e) { + Raven.captureException(e) + res.send(e) + } +}) diff --git a/views/pages/urls/index.hbs b/views/pages/urls/index.hbs index 17d715a..bc22ceb 100644 --- a/views/pages/urls/index.hbs +++ b/views/pages/urls/index.hbs @@ -3,25 +3,25 @@ {{> error-snippet}} - +
@@ -31,96 +31,28 @@ - - - - - - + {{#each urls}} + + + + + + + {{/each}} -
S.No.
- - - - -
+ {{ inc @index }} + + + {{ this.longUrl }} + + {{#if this.private }} + (Private 🤫) + {{/if}} + + + cb.lk/{{ this.codeActual }} + + + {{this.hits}} +
- - \ No newline at end of file + \ No newline at end of file