-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
99 lines (87 loc) · 3.19 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
// SPECIFY YOUR API KEY GET FROM https://openweathermap.org/api
// Lucia Salvi
//const apiKey = '56f0615a9269c2e635656856e4e66dbe';
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const app = express();
app.set('port', (process.env.PORT || 3000));
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')
app.get('/', function (req, res) {
res.render('index', {events: null, weather: null, error: null});
})
app.post('/', function (req, res) {
//old1
//let url = `http://api.openweathermap.org/data/2.5/weather?q=${req.body.city}&units=imperial&appid=${apiKey}`
let url = process.env.ANYPOINTURL + `?city=${req.body.city}`;
let sfquery=`select name, name__c, duration__c, event_date__c, description__c from salesforce.social_event__c where city__c='${req.body.city.toLowerCase()}'`;
let { Client } = require('pg');
let pgclient = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
try{
// CALL POSTGRES WITH SF HEROKU CONNECT
pgclient.connect();
console.log(sfquery);
pgclient.query(sfquery, (err, dbres) => {
console.log(dbres);
// CALL WEATHER SERVICE
request(url, function (err, response, body) {
if (err) throw err;
let weather = JSON.parse(body);
if(weather != undefined){
//weather.main....
//let tempCels=Math.round((parseInt(weather.main.temp)-32)*0.5556);
let tempCels=Math.round(parseInt(weather.temp));
let weatherText = `Weather is ${weather.weather} with ${tempCels} degrees of temperature and XXX% of humidity in ${weather.city}!`;
// ${weather.humidity}
//COMPOSE RESPONSE
res.render('index', {events: dbres, weather: weatherText, error: null});
}
});
});
}catch (err){
console.log(err);
}finally{
console.log('End GetEvent Post!!');
}
});
app.get("/api/events/:city", function (req, res) {
let city = req.params.city;
let sfquery=`select name, name__c, duration__c, event_date__c, description__c from salesforce.social_event__c where city__c='${city.toLowerCase()}'`;
let { Client } = require('pg');
let pgclient = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
try{
// CALL POSTGRES WITH SF HEROKU CONNECT
pgclient.connect();
console.log(sfquery);
pgclient.query(sfquery, (err, dbres) => {
console.log(dbres);
let events=[];
for ( var event in dbres.rows) {
console.log(event);
events.push({
event_date: dbres.rows[event].event_date__c,
name: dbres.rows[event].name__c,
duration: dbres.rows[event].duration__c,
description: dbres.rows[event].description__c
});
}
res.set('Content-Type', 'application/json');
res.status(200).send(events);
});
}catch (err){
console.log(err);
}finally{
console.log('End Events API!!');
}
});
app.listen(app.get('port'), function () {
console.log('Example app listening on port '+ app.get('port'))
});