-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
111 lines (94 loc) · 2.79 KB
/
server.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
106
107
108
109
110
111
const Hapi = require('hapi');
//Hapi plugins
const Boom = require('boom');
const Joi = require('joi');
//DB configs
const pgp = require('pg-promise')();
/**********************************************************************/
//TODO -> Don't forget to tell pgp your pg config
/**********************************************************************/
const db = pgp(`postgres://Warren@localhost:5432/workshop`);
//Models
const trackModule = require('./models/track');
//APIs
const SC = require('./models/soundcloud');
//Server configs
const server = new Hapi.Server();
server.connection({
port: 3001,
host: 'localhost'
});
/*
Search
*/
/**********************************************************************/
//TODO -> Let's implement search route here
/**********************************************************************/
/*
Track
*/
server.route({
method: 'POST',
path: '/track',
handler: (request, reply) => {
const track = request.payload;
trackModule.insertTrack(db, track, (err, result) => {
if (err) return reply(Boom.badImplementation(err));
reply(result);
});
},
config: {
validate: {
payload: {
id : Joi.number().integer().required(),
detail : Joi.object().required(),
comment : Joi.string().required().allow('').allow(null),
}
}
}
});
server.route({
method: 'GET',
path: '/track',
handler: (request, reply) => {
const {filter} = request.query;
trackModule.getTracks(db, filter, (err, result) => {
if (err) return reply(Boom.badImplementation(err));
reply(result);
});
},
config: {
validate: {
query: {
filter: Joi.string().valid('all', 'commented', 'nocomment').required(),
}
}
}
});
server.route({
method: 'PUT',
path: '/track',
handler: (request, reply) => {
const track = request.payload;
trackModule.updateTrack(db, track, (err, result) => {
if (err) return reply(Boom.badImplementation(err));
reply(result);
});
},
config: {
validate: {
payload: {
id : Joi.number().integer().required(),
detail : Joi.object().required(),
comment : Joi.string().required().allow('').allow(null),
}
}
}
});
/**********************************************************************/
//TODO -> Let's implement delete track route here
/**********************************************************************/
server.start(err => {
if (err) throw err;
console.log(`Server running at ${server.info.uri}`);
});