Skip to content

Commit

Permalink
APIs complete
Browse files Browse the repository at this point in the history
Signed-off-by: Jatin Katyal <jatin.katyal13@gmail.com>
  • Loading branch information
jatinkatyal13 committed Dec 21, 2018
1 parent 327ea68 commit b4760b9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 104 deletions.
47 changes: 45 additions & 2 deletions src/routes/api/urls.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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),
Expand Down Expand Up @@ -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)
}
})
136 changes: 34 additions & 102 deletions views/pages/urls/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
{{> error-snippet}}

<nav aria-label="Page navigation">
<ul class="pagination justify-content-end" id="pagination">
<ul class="pagination justify-content-end">
{{#ifeq user.role 'admin'}}
<li class="page-item">
<button onclick="getAllUrls()" class="page-link">All</button>
<a href="?all=true" class="page-link">All</a>
</li>
{{/ifeq}}
<li class="page-item" id="prev">
<button class="page-link" tabindex="-1" onclick="prevPage()">Previous</button>
<li class="page-item {{#unless pagination.hasPrev}}disabled{{/unless}}">
<a class="page-link" href="{{ pagination.prevUrl }}" tabindex="-1">Previous</a>
</li>
<li class="page-item" id="pageItem">
<button class="page-link"></button>
</li>
<li class="page-item" id="next">
<button class="page-link" onclick="nextPage()">Next</button>
{{#each pageList}}
<li class="page-item"><a class="page-link" href="{{ this }}">{{ inc @index }}</a></li>
{{/each}}
<li class="page-item {{#unless pagination.hasNext}}disabled{{/unless}}">
<a class="page-link" href="{{ pagination.nextUrl }}">Next</a>
</li>
</ul>
</nav>

<table class="table table-striped" id="urlsTable">
<table class="table table-striped">
<thead>
<tr>
<th>S.No.</th>
Expand All @@ -31,96 +31,28 @@
</tr>
</thead>
<tbody>
<tr id="url">
<td id="index"></td>
<td id="longUrl">
<a href="#" id="url"></a>
<span id="private"></span>
</td>
<td id="shortUrl">
<a href="#" id="url"></a>
</td>
<td id="hits"></td>
</tr>
{{#each urls}}
<tr>
<td>
{{ inc @index }}
</td>
<td>
<a href="{{ this.longUrl }}">
{{ this.longUrl }}
</a>
{{#if this.private }}
(Private 🤫)
{{/if}}
</td>
<td>
<a href="/urls/{{ this.codeActual }}">
cb.lk/{{ this.codeActual }}
</a>
</td>
<td>
{{this.hits}}
</td>
</tr>
{{/each}}
</tbody>
</table>

<script>
const apiEndPoint = "/api/urls"
let pagination = {}
function updateTable(params = {}) {
const table = $("#urlsTable")
const templateRow = table.find("tbody>tr").detach().first()
const pageItem = $("#pagination>#pageItem").detach().first()
$.get(apiEndPoint, params, function (response) {
response.urls.forEach((url, i) => {
const row = templateRow.clone()
row.find("#index").text(i+1)
const longUrl = row.find("#longUrl")
longUrl.find("#url").text(url.longUrl).attr("href", url.longUrl)
if (url.private){
longUrl.find("#private").text("(Private 🤫)")
} else {
longUrl.find("#private").text("")
}
row.find("#shortUrl>a").text("cb.lk/" + url.codeActual).attr("href", "urls/" + url.codeActual)
row.find("#hits").text(url.hits)
table.append(row)
})
pagination = response.pagination
for (var i=0; i<pagination.pageCount; i++) {
const pageItemClone = pageItem.clone()
pageItemClone.find("button")
.attr("page", i+1)
.text(i+1)
.click((e) => {
const page = e.target.attributes.page
updateTable({
page: page
})
})
pageItemClone.insertBefore('#pagination>#next')
}
})
}
function getAllUrls() {
updateTable({
page: pagination.page,
all: true
})
}
function nextPage() {
if (pagination.hasNext) {
updateTable({
page: pagination.page + 1
})
}
}
function prevPage() {
if (pagination.hasPrev) {
updateTable({
page: pagination.page - 1
})
}
}
$(document).ready(function() {
updateTable()
})
</script>
</table>

0 comments on commit b4760b9

Please sign in to comment.