forked from Team-6-Stone/projectCatwalk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
317 lines (265 loc) · 8.26 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const express = require('express');
const bodyparser = require('body-parser');
const axios = require('axios');
require('dotenv').config();
const app = express();
app.use(bodyparser.json());
app.use(express.static(__dirname + '/public'));
//const apiURL = 'https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe';
app.get('/products', function (req, res) {
let url = 'https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/products';
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then((response) => {
res.status(202).send(response.data);
})
.catch((error) => {
console.log('error in initial /products get request, error:', error)
res.status(404).send(error)
})
})
app.get('/products/:productId/styles', function (req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/products/${req.params.productId}/styles`
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then((response) => {
res.status(202).send(response.data);
})
.catch((error) => {
console.log('error in /products/:productId/styles axios get request, error:, ', error)
res.status(404).send(error)
})
})
app.get('/reviews/:productId', function (req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/reviews/?product_id=${req.params.productId}`
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then((response) => {
//console.log('got our reviews data from API!')
res.status(202).send(response.data);
})
.catch((error) => {
console.log('error in /products/:productId/reviews axios get request, error:', error)
res.status(404).send(error)
})
})
// AF GET request for cart
app.get('/cart', function (req, res) {
let url = 'https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/cart';
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then((response) => {
console.log('got our cart data from API!')
res.status(202).send(response.data);
})
.catch((error) => {
console.log('error in /cart axios get request, error:', error)
res.status(404).send(error)
})
})
// AF POST request to cart
app.post('/cart', function (req, res) {
console.log('cart POST req.params: ', req.body)
let url = 'https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/cart';
let data = req.body;
let config = {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
}
axios.post(url, data, config)
.then((response) => {
console.log('successfully posted to cart')
})
.catch((error) => {
console.log('error in our POST to cart')
res.status(404).send(error)
})
})
// RR GET request for related item id's
app.get('/products/:productId/related', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/products/${req.params.productId}/related`;
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then(response => {
res.status(202).send(response.data);
})
.catch(error => {
console.log('/RELATED GET ERROR: ', error)
res.status(404).send(error);
})
})
//TV GET request for products questions and answers
app.get('/questions/:productId', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/qa/questions/?product_id=${req.params.productId}`;
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then(response => {
res.status(202).send(response.data);
})
.catch(error => {
console.log('/RELATED GET ERROR: ', error)
res.status(404).send(error);
})
})
//TV PUT request to update usefulness of a question
app.put('/questionshelpful/:questionId', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/qa/questions/${req.params.questionId}/helpful`;
axios({ method: 'put', url: url, headers: { 'Authorization': process.env.GITHUB_API_KEY } })
.then(response => {
res.status(204).send('put req successful')
})
.catch(error => {
console.log('/RELATED GET ERROR: ', error)
res.status(404).send(error);
})
})
//TV PUT request to update usefulness of an answer
app.put('/answerhelpful/:answerId', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/qa/answers/${req.params.answerId}/helpful`;
axios({ method: 'put', url: url, headers: { 'Authorization': process.env.GITHUB_API_KEY } })
.then(response => {
res.status(204).send('put req (a) successful')
})
.catch(error => {
console.log('/RELATED GET ERROR: ', error)
res.status(404).send(error);
})
})
//TV PUT request to report an answer
app.put('/answerreport/:answerId', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/qa/answers/${req.params.answerId}/report`;
axios({ method: 'put', url: url, headers: { 'Authorization': process.env.GITHUB_API_KEY } })
.then(response => {
res.status(204).send('answer has been reported successfully')
})
.catch(error => {
console.log('/RELATED GET ERROR: ', error)
res.status(404).send(error);
})
})
//TV POST request to ask a question
app.post('/qa/ask', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/qa/questions`;
let data = req.body;
let config = {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
}
axios.post(url, data, config)
.then(response => {
res.status(202).send('question has been created')
})
.catch(error => {
console.log('/RELATED GET ERROR: ', error)
res.status(404).send(error);
})
})
//TV POST request to add an answer
app.post('/qa/answer/:question_id', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/qa/questions/${req.params.question_id}/answers`;
let data = req.body;
let config = {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
}
axios.post(url, data, config)
.then(response => {
res.status(202).send('answer has been created')
})
.catch(err => {
console.log('/RELATED GET ERROR: ', err)
res.status(404).send(err)
})
})
// RR GET request for item id product info
app.get('/products/:productId', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/products/${req.params.productId}`;
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then((response) => {
res.status(202).send(response.data);
})
.catch((error) => {
console.log('RR GET error /products/:productId');
res.status(404).send(error)
})
})
// MM GET request for meta reviews from product id
app.get('/reviews/meta/:productId', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/reviews/meta/?product_id=${req.params.productId}`;
axios.get(url, {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
})
.then(response => {
// console.log('getting data for RP Card: ', res.data)
res.status(202).send(response.data);
})
.catch(error => {
console.log('RP CARD DATA GET ERROR: ', error)
res.status(404).send(error);
})
})
//MM post of the new written review
app.post('/reviews', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/reviews`;
let data = req.body;
let config = {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
}
axios.post(url, data, config)
.then(response => {
res.status(202).send('A NEW REVIEW HAS BEEN MADE')
})
.catch(error => {
console.log('REVIEW POST ERR: ', error)
res.status(404).send(error);
})
})
app.put('/reviews/:review_id/helpful', function(req, res) {
let url = `https://app-hrsei-api.herokuapp.com/api/fec2/hr-rfe/reviews/${req.params.review_id}/helpful`;
console.log("params in the put request", req.params.review_id)
let config = {
headers: {
'Authorization': process.env.GITHUB_API_KEY
}
}
axios.put(url, config)
.then(response => {
res.status(204).send("A helpful mark has been added")
})
.catch(error => {
res.status(404).send(error);
})
})
let port = 8080
app.listen(port, () => {
console.log(`listening on port ${port}`);
});