-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
206 lines (169 loc) · 5.9 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const superagent = require('superagent');
const pg = require('pg');
const { response } = require('express');
const app = express();
const client = new pg.Client(process.env.DATABASE_URL);
app.use(cors());
// app.use(express.static('./public'));
const PORT = process.env.PORT || 3000;
client.connect().then(() => {
app.listen(PORT,() => console.log(`Listening on port ${PORT}`));
});
// ----------- Gets API ----------- //
app.get('/', function (req, res) {
res.status(200).send('Welcome to the ?best? API');
});
app.get('/show', function (req, res) {
let SQL = 'SELECT * FROM locations'
client.query(SQL).then(result => {
res.status(200).send(result.rows);
});
});
app.get('/location', function (req, res) {
let city = req.query.city;
let isWrong = badInput(city);
if(isWrong) {
res.status(403).send({ 'status': 403, msg: 'Wrong input'});
}
else {
existsDB(city).then(doesExist => {
if (doesExist.length>0) {
res.status(200).send(doesExist[0]);
} else {
const geo_key = process.env.GEOCODE_API_KEY;
let url = `https://eu1.locationiq.com/v1/search.php?key=${geo_key}&q=${city}&addressdetails=1&format=json`;
superagent.get(url).then( data => {
let locationData = new Location(city, data.body);
insertDB(locationData);
res.status(200).send(locationData);
});
}
});
}
});
app.get('/weather', (req, res) => {
Weather.all = [];
let locationLat = req.query.latitude;
let locationLon = req.query.longitude;
const weather_key = process.env.WEATHER_API_KEY;
let url = `https://api.weatherbit.io/v2.0/forecast/daily?lat=${locationLat}&lon=${locationLon}&days=6&key=${weather_key}`;
superagent.get(url).then( data => {
let weatherData = JSON.parse(data.text).data;
weatherData.map(element => {
let localWeather = new Weather(element);
Weather.all.push(localWeather);
})
res.status(200).json(Weather.all);
});
});
app.get('/trails', (req, res) => {
Trail.all = [];
let locationLat = req.query.latitude;
let locationLon = req.query.longitude;
let maxDistance = 100; // In KMs
let maxResults = 5;
const trail_key = process.env.TRAIL_API_KEY;
let url = `https://www.hikingproject.com/data/get-trails?lat=${locationLat}&lon=${locationLon}&maxDistance=${maxDistance}&maxResults=${maxResults}&key=${trail_key}`;
superagent.get(url).then( data => {
let trailData = JSON.parse(data.text).trails;
trailData.map(element => {
let localTrail = new Trail(element);
Trail.all.push(localTrail);
})
res.status(200).json(Trail.all);
});
});
app.get('/movies', (req, res) => {
Movie.all = [];
let region_code = req.query.country_code;
let page_limit = 6;
const movie_key = process.env.MOVIE_API_KEY;
let url = `https://api.themoviedb.org/3/discover/movie?api_key=${movie_key}&language=en-US®ion=${region_code}&sort_by=popularity.desc&include_adult=false&include_video=false&page=1`;
superagent.get(url).then( data => {
let movieData = JSON.parse(data.text).results;
for (var i = 0; i < page_limit; i++) {
let localMovie = new Movie(movieData[i]);
Movie.all.push(localMovie);
}
res.status(200).json(Movie.all);
});
});
app.get('/yelp', (req, res) => {
Yelp.all = [];
let locationLat = req.query.latitude;
let locationLon = req.query.longitude;
let limit = 5;
let offset = (req.query.page-1)*5;
const yelp_key = process.env.YELP_API_KEY;
let url = `https://api.yelp.com/v3/businesses/search?term=restaurants&latitude=${locationLat}&longitude=${locationLon}&limit=${limit}&offset=${offset}`;
superagent.get(url)
.set('Authorization', `Bearer ${yelp_key}`)
.then( data => {
let yelpData = data.body.businesses;
yelpData.map(element => {
let localYelp = new Yelp(element);
Yelp.all.push(localYelp);
})
res.status(200).json(Yelp.all);
});
});
app.use('*', (request, response) => response.send('Sorry, that route does not exist.'));
// ----------- constructors and functions ----------- //
function Location(city, data) {
this.search_query = city;
this.formatted_query = data[0].display_name;
this.latitude = data[0].lat;
this.longitude = data[0].lon;
this.country_code = data[0].address.country_code.toUpperCase();
}
function Weather(data) {
this.forecast = data.weather.description;
this.time = data.valid_date;
}
function Trail(data) {
this.name = data.name;
this.location = data.location;
this.length = data.length;
this.stars = data.stars;
this.star_votes = data.starVotes;
this.summary = data.summary;
this.trail_url = data.url;
this.conditions = data.conditionStatus;
this.condition_details = data.conditionDetails;
this.condition_time = data.conditionDate;
}
function Movie(data) {
this.title = data.title;
this.overview = data.overview;
this.average_votes = data.vote_average;
this.total_votes = data.vote_count;
this.image_url = `https://image.tmdb.org/t/p/w500${data.poster_path}`;
this.popularity = data.popularity;
this.released_on = data.release_date;
}
function Yelp(data) {
this.name = data.name;
this.image_url = data.image_url;
this.price = data.price;
this.rating = data.rating;
this.url = data.url;
}
let badInput = (city) => !city ? true : false ;
function existsDB(city) {
let SQL = `SELECT search_query,formatted_query,latitude,longitude,country_code FROM locations WHERE search_query=$1;`;
let values = [city];
let query = client.query(SQL, values).then((result) => {
return result.rows;
});
return query;
}
function insertDB(data) {
let SQL = `INSERT INTO locations (search_query,formatted_query,latitude,longitude,country_code) VALUES ($1,$2,$3,$4,$5)`;
let values = [data.search_query,data.formatted_query,data.latitude,data.longitude,data.country_code];
client.query(SQL, values);
console.log(SQL, values);
}