forked from sundeepg45/intro-to-heroku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (73 loc) · 3.11 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
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var pg = require('pg');
var app = express();
app.use(express.static('www'));
app.use(express.static(path.join('www', 'build')));
app.use(bodyParser.json());
var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/dreamhouse';
if (process.env.DATABASE_URL !== undefined) {
pg.defaults.ssl = true;
}
var client = new pg.Client(connectionString);
client.connect();
var propertyTable = 'property__c';
var favoriteTable = 'favorite__c';
var brokerTable = 'broker__c';
// setup the demo data if needed
client.query('SELECT * FROM salesforce.broker__c', function(error, data) {
if (error !== null) {
client.query('SELECT * FROM broker__c', function(error, data) {
if (error !== null) {
console.log('Loading Demo Data...');
require('./db/demo.js')(client);
console.log('Done Loading Demo Data!');
}
});
}
else {
var schema = 'salesforce.';
propertyTable = schema + 'property__c';
favoriteTable = schema + 'favorite__c';
brokerTable = schema + 'broker__c';
}
});
app.get('/property', function(req, res) {
client.query('SELECT * FROM ' + propertyTable, function(error, data) {
res.json(data.rows);
});
});
app.get('/property/:id', function(req, res) {
client.query('SELECT ' + propertyTable + '.*, ' + brokerTable + '.sfid AS broker__c_sfid, ' + brokerTable + '.name AS broker__c_name, ' + brokerTable + '.email__c AS broker__c_email__c, ' + brokerTable + '.phone__c AS broker__c_phone__c, ' + brokerTable + '.mobile_phone__c AS broker__c_mobile_phone__c, ' + brokerTable + '.title__c AS broker__c_title__c, ' + brokerTable + '.picture__c AS broker__c_picture__c FROM ' + propertyTable + ' INNER JOIN ' + brokerTable + ' ON ' + propertyTable + '.broker__c = ' + brokerTable + '.sfid WHERE ' + propertyTable + '.sfid = $1', [req.params.id], function(error, data) {
res.json(data.rows[0]);
});
});
app.get('/favorite', function(req, res) {
client.query('SELECT ' + propertyTable + '.*, ' + favoriteTable + '.sfid AS favorite__c_sfid FROM ' + propertyTable + ', ' + favoriteTable + ' WHERE ' + propertyTable + '.sfid = ' + favoriteTable + '.property__c', function(error, data) {
res.json(data.rows);
});
});
app.post('/favorite', function(req, res) {
client.query('INSERT INTO ' + favoriteTable + ' (property__c) VALUES ($1)', [req.body.property__c], function(error, data) {
res.json(data);
});
});
app.delete('/favorite/:sfid', function(req, res) {
client.query('DELETE FROM ' + favoriteTable + ' WHERE sfid = $1', [req.params.sfid], function(error, data) {
res.json(data);
});
});
app.get('/broker', function(req, res) {
client.query('SELECT * FROM ' + brokerTable, function(error, data) {
res.json(data.rows);
});
});
app.get('/broker/:sfid', function(req, res) {
client.query('SELECT * FROM ' + brokerTable + ' WHERE sfid = $1', [req.params.sfid], function(error, data) {
res.json(data.rows[0]);
});
});
var port = process.env.PORT || 8200;
app.listen(port);
//console.log('Listening at: http://localhost:' + port);