-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
197 lines (161 loc) · 4.84 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
"use strict";
const express = require('express'),
app = express(),
morgan = require('morgan'),
path = require('path'),
portNumber = process.env.PORT || process.argv[2] || 8080;
const bodyParser = require('body-parser');
const request = require('request');
const config = require('./config');
const mockedData = require("./mocked-data");
var i = 16;
app.use(express.static(__dirname));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/auth/facebook', function(req, res) {
var accessTokenUrl = 'https://graph.facebook.com/v2.5/oauth/access_token';
var params = {
code: req.body.code,
client_id: req.body.clientId,
client_secret: config.FACEBOOK_SECRET,
redirect_uri: req.body.redirectUri
};
request.get({ url: accessTokenUrl, qs: params, json: true }, function(err, response, accessToken) {
if (response.statusCode !== 200) {
return res.status(500).send({ message: accessToken.error.message });
}
return res.status(200).send({ access_token: accessToken.access_token });
});
});
app.get('/user/facebook', function(req, res) {
const fields = ['id', 'email', 'first_name', 'last_name', 'link', 'name'];
const graphApiUrl = 'https://graph.facebook.com/v2.5/me?fields=' + fields.join(',');
const accessToken = req.header("Authorization").split(' ')[1];
var params = {
fields: "id,email,name,picture",
access_token: accessToken,
token_type: "bearer",
expires_in: "5178000.0"
};
request.get({ url: graphApiUrl, qs: params, json: true }, function(err, response, profile) {
if (response.statusCode !== 200) {
return res.status(500).send({ message: profile.error.message });
}
var user = {
facebook: profile.id,
picture: 'https://graph.facebook.com/v2.3/' + profile.id + '/picture?type=normal',
displayName: profile.name
};
return res.status(200).send(user);
});
// End request
});
app.get('/bottles', (req, res) => {
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(mockedData.BOTTLES);
});
app.get('/bottle/:id', (req, res) => {
let id = req.params.id;
let bottle = findBottle(id);
bottle.comments = findComments(id);
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(bottle);
});
app.post('/bottle', (req, res) => {
var bottle = save(req.body);
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(bottle);
});
function save(bottle) {
let bottleToSave = {
id: i++,
title: bottle.title,
description: bottle.description,
lat: bottle.lat,
lng: bottle.lng
};
mockedData.BOTTLES.push(bottleToSave);
return bottleToSave;
}
app.post('/comment', (req, res) => {
var comment = saveComment(req.body);
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(comment);
});
function saveComment(comment) {
let commentToSave = {
bottle_id: comment.bottle_id,
name: comment.name,
date: comment.date,
message: comment.message,
image_url: comment.image_url
};
mockedData.COMMENTS.push(commentToSave);
return commentToSave;
}
app.put('/bottle', (req, res) => {
var bottle = update(req.body);
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(bottle);
});
function update(bottle) {
let bottleToUpdate = findBottle(bottle.id);
bottleToUpdate.title = bottle.title;
bottleToUpdate.description = bottle.description;
bottleToUpdate.lat = bottle.lat;
bottleToUpdate.lng = bottle.lng;
return bottleToUpdate;
}
app.get('/markers', (req, res) => {
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(toMarkers(mockedData.BOTTLES));
});
app.get('/marker/:id', (req, res) => {
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(toMarker(findBottle(req.params.id)));
});
function toMarker(bottle) {
return {
id: bottle.id,
lat: bottle.lat,
lng: bottle.lng,
label: bottle.title,
draggable: false
};
}
function toMarkers(bottles) {
var markers = [];
for (var i = 0; i < bottles.length; i++) {
if (i == 2) {
break;
}
markers.push(toMarker(bottles[i]));
}
return markers;
}
function findBottle(id) {
var bottles = mockedData.BOTTLES;
for (var i = 0; i < bottles.length; i++) {
if (bottles[i].id == id) {
return bottles[i];
}
}
return null;
}
function findComments(id) {
var comments = [];
var allComments = mockedData.COMMENTS;
for (var i = 0; i < allComments.length; i++) {
if (allComments[i].bottle_id == id) {
comments.push(allComments[i]);
}
}
return comments;
}
app.all('*', function (req, res) {
res.status(200).sendFile(path.join(__dirname, '/index.html'));
});
app.listen(portNumber, function () {
console.log("Listening on port " + portNumber);
});