Skip to content

Commit f6b19bf

Browse files
committed
CreateReference and DeleteReference
1 parent c04bf2a commit f6b19bf

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"koa-bodyparser": "^3.2.0",
1212
"koa-router": "^7.0.1",
1313
"mysql": "^2.10.2",
14+
"node-uuid": "^1.4.7",
1415
"sequelize": "^3.23.6"
1516
},
1617
"devDependencies": {

src/routes/index.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Router from 'koa-router';
22
import models from '../models';
3+
import * as uuid from 'node-uuid';
34
const router = new Router();
45

56
async 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);
3839
router.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
*/
5152
router.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

81117
export default router;

typings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dependencies": {
3+
"node-uuid": "registry:npm/node-uuid#1.4.7+20160723033700",
34
"sequelize": "registry:npm/sequelize#3.0.0+20160801095441"
45
},
56
"globalDependencies": {

0 commit comments

Comments
 (0)