From 327ea6829783f2f54136d487182de819ad3a5814 Mon Sep 17 00:00:00 2001 From: Jatin Katyal Date: Sun, 16 Dec 2018 15:53:13 +0530 Subject: [PATCH 1/4] all urls api completed --- src/middlewares/pagination.ts | 15 ++++ src/routes/api/index.ts | 3 + src/routes/api/urls.ts | 45 +++++++++++ src/routes/pages/urls.ts | 13 +--- views/pages/urls/index.hbs | 136 +++++++++++++++++++++++++--------- 5 files changed, 167 insertions(+), 45 deletions(-) create mode 100644 src/middlewares/pagination.ts create mode 100644 src/routes/api/urls.ts diff --git a/src/middlewares/pagination.ts b/src/middlewares/pagination.ts new file mode 100644 index 0000000..ca020e9 --- /dev/null +++ b/src/middlewares/pagination.ts @@ -0,0 +1,15 @@ +export default function(req, res, next) { + const LIMIT = 20 + const PAGE = 1 + if (!req.query.page || req.query.page <= 0) { + req.query.page = PAGE + } + if (!req.query.limit || req.query.limit <= 0) { + req.query.limit = LIMIT + } + res.locals.pagination = { + page: req.query.page, + limit: req.query.limit, + } + next() +} diff --git a/src/routes/api/index.ts b/src/routes/api/index.ts index 0e84369..6e8eb02 100644 --- a/src/routes/api/index.ts +++ b/src/routes/api/index.ts @@ -1,3 +1,6 @@ import { Router } from 'express' +import { route as urlApis } from './urls' export const route = Router() + +route.use('/urls', urlApis) diff --git a/src/routes/api/urls.ts b/src/routes/api/urls.ts new file mode 100644 index 0000000..2fd6939 --- /dev/null +++ b/src/routes/api/urls.ts @@ -0,0 +1,45 @@ +import { ensureLoggedIn } from 'connect-ensure-login' +import { Router } from 'express' +import querystring from 'querystring' +import { getAllUrlsForUser, PageOptions } from '../../controllers/urls' +import paginationMiddleware from '../../middlewares/pagination' + +export const route = Router() + +// APIs only for logged in peeps ! +route.use(ensureLoggedIn('/login')) + +// Pagination Middleware +route.use(paginationMiddleware) + +route.get('/', async (req, res) => { + const page: PageOptions = { + offset: res.locals.pagination.limit * (res.locals.pagination.page - 1), + limit: res.locals.pagination.limit, + } + let getAll: boolean = false + if (req.query.all && req.query.all === 'true') { + req.query.all = true + getAll = true + } + const urlsAndPagination = await getAllUrlsForUser(req.user, page, getAll) + if (urlsAndPagination.pagination.hasNext) { + urlsAndPagination.pagination.nextUrl = + '?' + + querystring.stringify({ + ...req.query, + page: urlsAndPagination.pagination.page + 1, + }) + } + if (urlsAndPagination.pagination.hasPrev) { + urlsAndPagination.pagination.prevUrl = + '?' + + querystring.stringify({ + ...req.query, + page: urlsAndPagination.pagination.page - 1, + }) + } + + res.header('Content-Type', 'application/json') + res.send(JSON.stringify(urlsAndPagination)) +}) diff --git a/src/routes/pages/urls.ts b/src/routes/pages/urls.ts index a24fe58..b183e31 100644 --- a/src/routes/pages/urls.ts +++ b/src/routes/pages/urls.ts @@ -14,6 +14,7 @@ import { updateUrl, URLOptions, } from '../../controllers/urls' +import paginationMiddleware from '../../middlewares/pagination' import { optsFromGroupedShortcode } from '../../utils/shortener' export const route = Router() @@ -22,17 +23,7 @@ export const route = Router() route.use(ensureLoggedIn('/login')) // Pagination middleware -route.use((req, res, next) => { - const LIMIT = 20 - if (!req.query.page || req.query.page < 0) { - req.query.page = 1 - } - res.locals.pagination = { - page: req.query.page, - limit: LIMIT, - } - next() -}) +route.use(paginationMiddleware) route.get('/', async (req, res) => { const page: PageOptions = { diff --git a/views/pages/urls/index.hbs b/views/pages/urls/index.hbs index bc22ceb..17d715a 100644 --- a/views/pages/urls/index.hbs +++ b/views/pages/urls/index.hbs @@ -3,25 +3,25 @@ {{> error-snippet}} - +
@@ -31,28 +31,96 @@ - {{#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 From b4760b9c1c749132aacf4cb3883d7ecfa3cda6fb Mon Sep 17 00:00:00 2001 From: Jatin Katyal Date: Sun, 16 Dec 2018 18:35:52 +0530 Subject: [PATCH 2/4] 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 From 9764464a053d192b15ff64bdb177a1e912c5fa62 Mon Sep 17 00:00:00 2001 From: Jatin Katyal Date: Fri, 21 Dec 2018 16:27:01 +0530 Subject: [PATCH 3/4] update and create api --- src/routes/api/urls.ts | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/routes/api/urls.ts b/src/routes/api/urls.ts index 296ac77..1cfbcea 100644 --- a/src/routes/api/urls.ts +++ b/src/routes/api/urls.ts @@ -4,10 +4,13 @@ import querystring from 'querystring' import Raven from 'raven' import { findGroupById, findGroupByPrefix } from '../../controllers/groups' import { + createUrl, findUrlByCodeInt, findUrlByShortcode, getAllUrlsForUser, PageOptions, + updateUrl, + URLOptions, } from '../../controllers/urls' import paginationMiddleware from '../../middlewares/pagination' import { optsFromGroupedShortcode } from '../../utils/shortener' @@ -57,6 +60,27 @@ route.get('/', async (req, res) => { res.send(JSON.stringify(urlsAndPagination)) }) +route.post('/', async (req, res) => { + try { + const url = await createUrl( + { + longUrl: req.body.longUrl, + shortCode: req.body.shortCode, + private: req.body.private, + }, + req.user, + ) + if (!url) { + throw new Error('Error creating shortlink. Try again') + } + + res.send(url) + } catch (e) { + Raven.captureException(e) + res.send(e) + } +}) + route.get('/:url', async (req, res) => { try { const url = await findUrlByShortcode(req.params.url) @@ -68,6 +92,20 @@ route.get('/:url', async (req, res) => { } }) +route.post('/:url', async (req, res) => { + try { + const newUrl: URLOptions = { + longUrl: req.body.longUrl, + private: req.body.private, + } + const urlOpts = await updateUrl(req.params.url, newUrl, req.user) + res.send(JSON.stringify(urlOpts)) + } catch (e) { + Raven.captureException(e) + res.send(e) + } +}) + route.get('/:group/:url', async (req, res) => { try { const group = await findGroupByPrefix(req.params.group) @@ -86,3 +124,22 @@ route.get('/:group/:url', async (req, res) => { res.send(e) } }) + +route.post('/:group/:url', async (req, res) => { + try { + const newUrl: URLOptions = { + longUrl: req.body.longUrl, + private: req.body.private, + } + const group = await findGroupByPrefix(req.params.group) + if (!group) { + throw new Error('Group prefic does not exist') + } + const urlOpts = await updateUrl(req.params.url, newUrl, req.user, group) + + res.send(JSON.stringify(urlOpts)) + } catch (e) { + Raven.captureException(e) + res.send(e) + } +}) From 0f62d776ec78c034997ed96a0bd988d0c9360b65 Mon Sep 17 00:00:00 2001 From: Jatin Katyal Date: Sun, 10 Feb 2019 19:32:37 +0530 Subject: [PATCH 4/4] using res json --- src/routes/api/urls.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/routes/api/urls.ts b/src/routes/api/urls.ts index 1cfbcea..dcbc948 100644 --- a/src/routes/api/urls.ts +++ b/src/routes/api/urls.ts @@ -23,12 +23,6 @@ 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), @@ -57,7 +51,7 @@ route.get('/', async (req, res) => { }) } - res.send(JSON.stringify(urlsAndPagination)) + res.json(urlsAndPagination) }) route.post('/', async (req, res) => { @@ -74,7 +68,7 @@ route.post('/', async (req, res) => { throw new Error('Error creating shortlink. Try again') } - res.send(url) + res.json(url) } catch (e) { Raven.captureException(e) res.send(e) @@ -85,7 +79,7 @@ route.get('/:url', async (req, res) => { try { const url = await findUrlByShortcode(req.params.url) - res.send(JSON.stringify(url)) + res.json(url) } catch (e) { Raven.captureException(e) res.send(e) @@ -99,7 +93,7 @@ route.post('/:url', async (req, res) => { private: req.body.private, } const urlOpts = await updateUrl(req.params.url, newUrl, req.user) - res.send(JSON.stringify(urlOpts)) + res.json(urlOpts) } catch (e) { Raven.captureException(e) res.send(e) @@ -118,7 +112,7 @@ route.get('/:group/:url', async (req, res) => { throw new Error('Shortcode does not exist') } - res.send(JSON.stringify(url)) + res.json(url) } catch (e) { Raven.captureException(e) res.send(e) @@ -137,7 +131,7 @@ route.post('/:group/:url', async (req, res) => { } const urlOpts = await updateUrl(req.params.url, newUrl, req.user, group) - res.send(JSON.stringify(urlOpts)) + res.json(urlOpts) } catch (e) { Raven.captureException(e) res.send(e)