-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
|
||
|
||
} |