Skip to content

Commit 2bf9bfa

Browse files
committed
update and create api
1 parent b4760b9 commit 2bf9bfa

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/routes/api/urls.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
findUrlByShortcode,
99
getAllUrlsForUser,
1010
PageOptions,
11+
URLOptions,
12+
updateUrl,
13+
createUrl,
1114
} from '../../controllers/urls'
1215
import paginationMiddleware from '../../middlewares/pagination'
1316
import { optsFromGroupedShortcode } from '../../utils/shortener'
@@ -57,6 +60,27 @@ route.get('/', async (req, res) => {
5760
res.send(JSON.stringify(urlsAndPagination))
5861
})
5962

63+
route.post('/', async (req, res) => {
64+
try {
65+
const url = await createUrl(
66+
{
67+
longUrl: req.body.longUrl,
68+
shortCode: req.body.shortCode,
69+
private: req.body.private,
70+
},
71+
req.user,
72+
)
73+
if (!url) {
74+
throw new Error('Error creating shortlink. Try again')
75+
}
76+
77+
res.send(url)
78+
} catch (e) {
79+
Raven.captureException(e)
80+
res.send(e)
81+
}
82+
})
83+
6084
route.get('/:url', async (req, res) => {
6185
try {
6286
const url = await findUrlByShortcode(req.params.url)
@@ -68,6 +92,20 @@ route.get('/:url', async (req, res) => {
6892
}
6993
})
7094

95+
route.post('/:url', async (req, res) => {
96+
try {
97+
const newUrl: URLOptions = {
98+
longUrl: req.body.longUrl,
99+
private: req.body.private,
100+
}
101+
const urlOpts = await updateUrl(req.params.url, newUrl, req.user)
102+
res.send(JSON.stringify(urlOpts))
103+
} catch (e) {
104+
Raven.captureException(e)
105+
res.send(e)
106+
}
107+
})
108+
71109
route.get('/:group/:url', async (req, res) => {
72110
try {
73111
const group = await findGroupByPrefix(req.params.group)
@@ -86,3 +124,22 @@ route.get('/:group/:url', async (req, res) => {
86124
res.send(e)
87125
}
88126
})
127+
128+
route.post('/:group/:url', async (req, res) => {
129+
try {
130+
const newUrl: URLOptions = {
131+
longUrl: req.body.longUrl,
132+
private: req.body.private,
133+
}
134+
const group = await findGroupByPrefix(req.params.group)
135+
if (!group) {
136+
throw new Error('Group prefic does not exist')
137+
}
138+
const urlOpts = await updateUrl(req.params.url, newUrl, req.user, group)
139+
140+
res.send(JSON.stringify(urlOpts))
141+
} catch (e) {
142+
Raven.captureException(e)
143+
res.send(e)
144+
}
145+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy