-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
209 lines (184 loc) · 6.4 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
207
208
209
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
var pool = require("./pg-connection-pool");
app.get('/db/userinfo', function(req, res) {
//retrives list of all users
pool.query("SELECT * FROM userinfo;").then(function(result) {
res.send(result.rows);
});
});
app.get('/db/userinfo/:username', function(req, res) {
//retrives list of users. Used in logging in/creating account
var username = req.params.username;
pool.query("SELECT * FROM userinfo WHERE username=$1::text ;", [username]).then(function(result) {
res.send(result.rows);
});
});
app.get('/db/library/:username', function(req, res) {
//retrieve library of user.
var username = req.params.username;
pool.query("SELECT * FROM library WHERE username=$1::text;", [username]).then(function(result) {
res.send(result.rows);
});
});
app.get('/db/watchlist/:username', function(req, res) {
//retrieve watchlist of user
var username = req.params.username;
pool.query("SELECT * FROM watchlist WHERE username=$1::text;", [username]).then(function(result) {
res.send(result.rows);
});
});
app.get('/db/library', function(req, res) {
//retrieve libraries of all users
pool.query("SELECT * FROM library;").then(function(result) {
res.send(result.rows);
});
});
app.get('/db/watchlist', function(req, res) {
//retrieve watchlists of all users
pool.query("SELECT * FROM watchlist;").then(function(result) {
res.send(result.rows);
});
});
app.get('/db/userinfo/:username', function(req, res) {
//get email by user
var username = req.params.username;
pool.query("SELECT email FROM userinfo WHERE username=$1::text;", [username]).then(function(result) {
res.send(result.rows);
});
});
app.post('/db/signup/', function(req, res) {
//add user to database
var item = req.body;
var sql = "INSERT INTO userinfo(username, email, password)" +
"VALUES ($1::text, $2::text, $3::text)";
var entry = [item.username, item.email, item.password];
pool.query(sql, entry).then(function() {
res.send("INSTERTED");
});
});
app.post('/db/library/', function(req, res) {
//add book to library
var item = req.body;
var sql = "INSERT INTO library(author, title, thumbnailurl, username, description)" +
"VALUES ($1::text, $2::text, $3::text, $4::text, $5::text)";
var entry = [item.author, item.title, item.thumbnailurl, item.username, item.description];
pool.query(sql, entry).then(function() {
res.status(201);
res.send("INSTERTED");
});
});
app.post('/db/watchlist/', function(req, res) {
//add book to watchlist
var item = req.body;
var sql = "INSERT INTO watchlist(author, title, thumbnailurl, username, description)" +
"VALUES ($1::text, $2::text, $3::text, $4::text, $5::text)";
var entry = [item.author, item.title, item.thumbnailurl, item.username, item.description];
pool.query(sql, entry).then(function() {
res.status(201);
res.send("INSTERTED");
});
});
app.delete('/db/library/:id', function(req, res) {
//delete from library
var id = req.params.id;
var sql = "DELETE FROM library WHERE id=$1::int;"
var entry = [id];
pool.query(sql, entry).then(function(result) {
res.send(result.rows);
});
});
app.delete('/db/watchlist/:id', function(req, res) {
//delete from watchlist
var id = req.params.id;
var sql = "DELETE FROM watchlist WHERE id=$1::int;"
var entry = [id];
pool.query(sql, entry).then(function(result) {
res.send(result.rows);
});
});
app.post('/email', function(req, res) {
//send email to user about book request
var body = req.body;
var user1 = {
name: body.userName1,
email: body.userEmail1,
title: body.title1
}
var user2 = {
name: body.userName2,
email: body.userEmail2,
title: body.title2
}
sendEmail(user1, user2);
console.log('Success');
res.send('Success');
});
app.get('/db/matches/:username', function(req, res) {
//get matches for user
var username = req.params.username;
pool.query("SELECT library.username, library.title FROM library, watchlist WHERE watchlist.title = library.title AND watchlist.username = $1::text;"
, [username]).then(function(result) {
res.send(result.rows);
});
});
app.post('/db/login', function(req, res) {
var body = req.body;
pool.query("SELECT * FROM userinfo WHERE username=$1::text", [req.body.username]).then(function(result) {
if (result.rows[0].password === body.password) {
res.send('Success');
} else {
res.send('Invalid');
}
}).catch(function(error) {
res.send('User not found');
});
});
// Sign-up user verification
app.get('/db/signup/:username', function (req, res) {
var username = req.params.username;
pool.query("SELECT username FROM userinfo WHERE username=$1::text", [username]).then(function(result) {
if(result.rows[0].username === username) {
res.send('Username taken');
} else {
res.send('Username available');
}
}).catch(function(error) {
res.send('Username available');
});
});
// Retrieve popular books
app.get('/db/popularBooks', function(req, res) {
pool.query("SELECT title, author, thumbnailurl, count(title) FROM watchlist GROUP BY title, author, thumbnailurl ORDER BY count DESC LIMIT 10").then(function(result) {
res.send(result.rows);
});
})
/*
This call sends an email to one recipient, using a validated sender address
*/
var mailjet = require ('node-mailjet')
.connect(process.env.API_KEY, process.env.API_SECRET);
function handleError (err) {
throw new Error(err.ErrorMessage);
}
function sendEmail (user1, user2) {
email = {};
email['FromName'] = 'Book Buddies';
email['FromEmail'] = '[email protected]';
email['Subject'] = user1.name + ' has requested a trade!';
email['Recipients'] = [{Email: user2.email}];
email['Text-Part'] = 'Hello, ' + user2.name + '. ' + user1.name + ' has proposed a trade with you. They would like to exchange their book(s) '
+ user1.title + ' for your book(s) called ' + user2.title + '. Please contact this user at ' + user1.email + ' if you wish to trade.';
mailjet.post('send')
.request(email)
.catch(handleError);
console.log('email success');
}
// Server port listen stuff
var port = process.env.PORT || 3030;
app.listen(port, function() {
console.log('Server is running on ' + port);
});