11import * as Router from 'koa-router' ;
22import models from '../models' ;
3+ import * as uuid from 'node-uuid' ;
34const router = new Router ( ) ;
45
56async function redirect ( ctx : Router . IRouterContext ) {
@@ -9,7 +10,7 @@ async function redirect(ctx: Router.IRouterContext) {
910 ctx . status = 404 ;
1011 return ;
1112 }
12- let ref = JSON . parse ( link . references ) . filter ( ( ref : { url : string , userAgent : string } ) => {
13+ let ref = JSON . parse ( link . references || '[]' ) . filter ( ( ref : { url : string , userAgent : string } ) => {
1314 if ( ! ref . userAgent ) {
1415 return true ;
1516 } else {
@@ -38,22 +39,22 @@ router.get('/:token/:param', redirect);
3839router . get ( '/' , async ( ctx ) => {
3940 const links = await models . link . findAll ( ) ;
4041 ctx . type = '.json' ;
41- ctx . body = JSON . stringify ( links . map ( ( link ) => ( link . references = JSON . parse ( link . references ) ) && link ) ) ;
42+ ctx . body = JSON . stringify ( links . map ( ( link ) => ( link . references = JSON . parse ( link . references || '[]' ) ) && link ) ) ;
4243} ) ;
4344
4445/**
4546 * @api {PUT } /:token Create or update link
4647 * @apiName PutLink
4748 * @apiGroup Link
48- * @apiParam {String} url Target url
4949 * @apiParam {String} token
50+ * @apiParam {Array} references
5051 */
5152router . put ( '/:token' , async ( ctx : any ) => {
5253 const { token} = ctx . params ;
5354 const { body} = ctx . request ;
5455 const [ link , created ] = await models . link . findOrCreate ( { where : { token } } ) ;
55- if ( body . url ) {
56- link . set ( 'references' , JSON . stringify ( [ { url : body . url } ] ) ) ;
56+ if ( body . references ) {
57+ link . set ( 'references' , JSON . stringify ( body . references ) ) ;
5758 }
5859 if ( body . token ) {
5960 link . set ( 'token' , body . token ) ;
@@ -75,7 +76,42 @@ router.delete('/:token', async (ctx) => {
7576 } else {
7677 await link . destroy ( ) ;
7778 }
78- ctx . body = 'OK' ;
79- } )
79+ ctx . status = 200 ;
80+ } ) ;
81+
82+ /**
83+ * @api {POST } /:token/references Create reference
84+ * @apiName CreateReference
85+ * @apiGroup Reference
86+ */
87+ router . post ( '/:token/references' , async ( ctx : any ) => {
88+ const { token} = ctx . params ;
89+ const { url, userAgent} = ctx . request . body ;
90+ const [ link , created ] = await models . link . findOrCreate ( { where : { token } } ) ;
91+ const id = uuid . v4 ( ) ;
92+ let references = JSON . parse ( link . references || '[]' ) ;
93+ references . push ( { id, userAgent, url} ) ;
94+ link . set ( 'references' , JSON . stringify ( references ) ) ;
95+ await link . save ( ) ;
96+ ctx . status = 200 ;
97+ } ) ;
98+
99+ /**
100+ * @api {DELETE } /:token/references/:refId Delete reference
101+ * @apiName DeleteReference
102+ * @apiGroup Reference
103+ */
104+ router . delete ( '/:token/references/:refId' , async ( ctx ) => {
105+ const { token, refId} = ctx . params ;
106+ const link = await models . link . findOne ( { where : { token } } ) ;
107+ if ( ! link ) {
108+ ctx . status = 404 ;
109+ return ;
110+ }
111+ let references = JSON . parse ( link . references || '[]' ) . filter ( ( ref : { id : string } ) => ref . id != refId ) ;
112+ link . set ( 'references' , JSON . stringify ( references ) ) ;
113+ await link . save ( ) ;
114+ ctx . status = 200 ;
115+ } ) ;
80116
81117export default router ;
0 commit comments