Skip to content

Commit

Permalink
update and create api
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinkatyal13 committed Dec 21, 2018
1 parent b4760b9 commit 2bf9bfa
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/routes/api/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
findUrlByShortcode,
getAllUrlsForUser,
PageOptions,
URLOptions,
updateUrl,
createUrl,
} from '../../controllers/urls'
import paginationMiddleware from '../../middlewares/pagination'
import { optsFromGroupedShortcode } from '../../utils/shortener'
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
})

0 comments on commit 2bf9bfa

Please sign in to comment.