-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
105 lines (95 loc) · 2.84 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
const router = require("koa-router")();
const views = require('co-views');
const Playlist = require('./services/playlists');
const Text = require('./services/texts');
const Video = require('./services/videos');
const appConfig = require('./config/config')();
const config = JSON.stringify({
baseUrl: appConfig.baseUrl,
youtubeKey: appConfig.youtubeKey
});
const render = views('views/', {
map: {
html: 'hogan'
}
});
module.exports = function(app, io) {
let Socket;
io.on('connection', function (socket) {
console.log('connection established');
Socket = socket;
});
router.get('/playlists', function *(next) {
yield next;
this.body = yield render('index', {
config: config
});
});
router.get('/playlists/all', function *(next) {
yield next;
let playlists = yield Playlist.retrieveAll();
this.body = yield {
playlists: playlists
};
})
.get('/playlists/:id', function *(next) {
yield next;
this.body = yield render('index', {
config: config
});
})
.post('/playlists/create', function *(next) {
yield next;
this.body = this.request.body;
let playlist = yield Playlist.create(this.body);
this.body = yield {
playlist: playlist
};
})
.get('/playlists/get/:id', function *(next) {
yield next;
let playlist = yield Playlist.retrieve(this.params.id);
this.body = yield {
playlist: playlist
};
})
.put('/playlists/:id', function *(next) {
yield next;
this.body = this.request.body;
let playlist = yield Playlist.update(this.body);
this.body = yield {
playlist: playlist
};
})
.del('/videos/:id', function *(next) {
yield next;
this.body = this.request.body;
yield Video.destroy(this.params.id);
})
.post('/text', function *(next) {
yield next;
let match = /(^\d+)\s(.+)$/.exec(this.request.body.Body);
// let playlistId = match[1];
let query = match[2];
// let playlist = yield Playlist.retrieve(playlistId);
let result = yield Text.search(query);
// let body = {
// playlist: playlist,
// video: {
// thumbnail: result.snippet.thumbnails.default.url,
// title: result.snippet.title,
// videoid: result.id.videoId
// }
// };
Socket.emit('text', {
video: {
thumbnail: result.snippet.thumbnails.default.url,
title: result.snippet.title,
videoid: result.id.videoId
}
});
});
app.use(router.routes());
app.use(router.allowedMethods());
};