8
8
findUrlByShortcode ,
9
9
getAllUrlsForUser ,
10
10
PageOptions ,
11
+ URLOptions ,
12
+ updateUrl ,
13
+ createUrl ,
11
14
} from '../../controllers/urls'
12
15
import paginationMiddleware from '../../middlewares/pagination'
13
16
import { optsFromGroupedShortcode } from '../../utils/shortener'
@@ -57,6 +60,27 @@ route.get('/', async (req, res) => {
57
60
res . send ( JSON . stringify ( urlsAndPagination ) )
58
61
} )
59
62
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
+
60
84
route . get ( '/:url' , async ( req , res ) => {
61
85
try {
62
86
const url = await findUrlByShortcode ( req . params . url )
@@ -68,6 +92,20 @@ route.get('/:url', async (req, res) => {
68
92
}
69
93
} )
70
94
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
+
71
109
route . get ( '/:group/:url' , async ( req , res ) => {
72
110
try {
73
111
const group = await findGroupByPrefix ( req . params . group )
@@ -86,3 +124,22 @@ route.get('/:group/:url', async (req, res) => {
86
124
res . send ( e )
87
125
}
88
126
} )
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