forked from jbergant/chatbot-facebook-v2-final
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.js
76 lines (67 loc) · 2.27 KB
/
colors.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
'use strict';
const request = require('request');
const config = require('./config');
const pg = require('pg');
pg.defaults.ssl = true;
module.exports = {
readAllColors: function(callback) {
var pool = new pg.Pool(config.PG_CONFIG);
pool.connect(function(err, client, done) {
if (err) {
return console.error('Error acquiring client', err.stack);
}
client
.query(
'SELECT color FROM public.iphone_colors',
function(err, result) {
if (err) {
console.log(err);
callback([]);
} else {
let colors = [];
for (let i = 0; i < result.rows.length; i++) {
colors.push(result.rows[i]['color']);
}
callback(colors);
};
});
});
pool.end();
},
readUserColor: function(callback, userId) {
var pool = new pg.Pool(config.PG_CONFIG);
pool.connect(function(err, client, done) {
if (err) {
return console.error('Error acquiring client', err.stack);
}
client
.query(
'SELECT color FROM public.users WHERE fb_id=$1',
[userId],
function(err, result) {
if (err) {
console.log(err);
callback('');
} else {
callback(result.rows[0]['color']);
};
});
});
pool.end();
},
updateUserColor: function(color, userId) {
var pool = new pg.Pool(config.PG_CONFIG);
pool.connect(function(err, client, done) {
if (err) {
return console.error('Error acquiring client', err.stack);
}
let sql = 'UPDATE public.users SET color=$1 WHERE fb_id=$2';
client.query(sql,
[
color,
userId
]);
});
pool.end();
}
}