forked from HRNYC12-Socrates/OkCoupon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
215 lines (188 loc) · 5.61 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
210
211
212
213
214
215
const express = require('express');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const app = express();
const db = require('./db/index2.js')
const apiHelp = require('./APIhelper.js');
const bodyParser = require('body-parser');
const compiler = webpack(webpackConfig);
app.use(express.static(__dirname + '/www'));
app.use(bodyParser.json());
//////////////////////////////// ETHAN
app.post('/signUp', (req, res) => {
let u = req.body.user_name;
let p = req.body.password;
// save a new username/password combo to users table
db.addUser(u, p, () => {
// redirect to login page
res.status(201).end();
});
});
app.post('/login', (req, res) => {
let u = req.body.user_name;
let p = req.body.password;
db.authenticateUser(u, p, (err, data) => {
// console.log('data ', data[0].id) //cookie
if (data.length) {
res.send(data);
} else {
res.status(404).end()
}
});
});
app.post('/delete', (req, res) => {
let i = req.body.params.userID;
let c = req.body.params.couponURL;
console.log('deleting: ', i, c);
db.deleteSaved(i, c, (err, data) => {
if (err) {
res.send(err);
} else {
console.log('in server ')
res.status(200).send(data)
}
});
})
app.get('/userData', (req, res) => {
let u = req.body.username;
// get all user's saved coupons:
db.getSaved(u, () => {
res.status(302).redirect('/saved');
});
});
/////////////Reviews//////////////////
app.post('/reviews', (req, res) => {
let i = req.body.params.userID;
let c = req.body.params.couponURL;
let d = req.body.params.description;
console.log('here is your review data ', i, c, d)
db.addReview(i, c, d, (err, data) => {
if (err) {
res.send(err);
} else {
console.log('data ', data)
res.status(200).send(data)
}
});
})
app.get('/reviews', (req, res) => {
let c = req.query.couponURL;
console.log('whats body ', req.query.couponURL)
// get all user's saved coupons:
db.getReviews(c, (err, data) => {
if (err) {
res.send(err);
} else {
console.log('data ', data)
res.status(200).send(data);
}
});
});
app.post('/deleteReview', (req, res) => {
let i = req.body.params.userID;
let c = req.body.params.couponURL;
console.log('deleting: ', i, c);
db.deleteReview(i, c, (err, data) => {
if (err) {
res.send(err);
} else {
console.log('in server ')
res.status(200).send(data)
}
});
})
app.get('/userInfo', (req, res) => {
let i = req.query.userID;
console.log('what is i ', i)
// get all user's saved coupons:
db.getUserInfo(i, (err, data) => {
if (err) {
res.send(err);
} else {
console.log('data ', data)
res.status(200).send(data);
}
});
});
//////////////////////////////////////
app.post('/helper', (req, res) => {
let savedCoupons = {};
db.getSaved(req.body.userID, (err, data) => {
if ( err ) {
console.error(err);
} else {
if ( data.length ) {
data.forEach(deal => {
savedCoupons[deal.imgUrl] = true
});
}
}
});
apiHelp.couponHelper(req.body.postal, req.body.filter, (data) => {
var newData = [];
data.deals.forEach((eachDeal) => {
// check if coupon is already in user's saved coupons:
if ( savedCoupons[eachDeal.deal.image_url] === undefined ) {
console.log('newDeal url: ', eachDeal.deal.url);
var newDeal = {
latitude: eachDeal.deal.merchant.latitude.toString(),
longitude: eachDeal.deal.merchant.longitude.toString(),
imgUrl: eachDeal.deal.image_url,
title: eachDeal.deal.title,
price: JSON.stringify(eachDeal.deal.price),
discount: JSON.stringify(eachDeal.deal.discount_percentage),
merchant: eachDeal.deal.merchant.name,
url: eachDeal.deal.url,
pureUrl: eachDeal.deal.untracked_url,
id: eachDeal.deal.id
}
newData.push(newDeal)
} else {
console.log('you already saved this deal. skipping to the next one.');
}
});
res.status(200).send(newData)
});
});
app.get('/savedCoupons', (req, res) => {
db.getSaved(req.query.userID, (err, data) => {
res.status(200).send(data)
});
});
app.get('/saveCount', (req, res) => {
console.log('image_url to look up: ', req.query.image_url);
db.getSaveCount(req.query.image_url, (err, data) => {
if ( err ) {
console.error('db.getSaveCount error: ', err);
res.end();
} else {
res.send(data);
}
});
});
////////////////////////////////////////////////////////////////////////// ETHAN
// instead of storing all items and then updating items on 'yes', only save items on yes
// and remove the 'no' route altogether.
app.post('/yes', (req, res) => {
let u = req.body.userID;
let c = req.body.data;
console.log('coupon data: ', c);
db.addSaved(u, c, () => {
res.status(201).send('saved coupon to db.');
});
});
//////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////// AARON
app.get('/categories', (req, res) => {
apiHelp.categoryList((categories) => {
res.status(200).send(categories)
});
});
/////////////////////////////////////////////////////////////
app.set('port', process.env.PORT || 3306)
const server = app.listen(app.get('port'))
// console.log(server)
// const host = server.address().address;
// const port = server.address().port;
// console.log('Example app listening at http://%s:%s', host, port);