-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (141 loc) · 3.14 KB
/
index.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
const express = require('express');
const morgan = require('morgan');
require('dotenv').config()
const db = require('./db/controller')
const app = express();
const port = 3000;
app.use(morgan('dev'));
app.use(express.json());
/*
QueryParameters
Parameter Type Description
page integer Selects the page of results to return. Default 1.
count integer Specifies how many results per page to return. Default 5.
sort text Changes the sort order of reviews to be based on "newest", "helpful", or "relevant"
product_id integer Specifies the product for which to retrieve reviews.
*/
app.get(`/${process.env.LOADERIO_TOKEN}`, (req, res)=>{
res.attachment('filename.txt')
res.type('txt')
res.send(`${process.env.LOADERIO_TOKEN}`)
})
app.get('/reviews',(req, res)=>{
if(req.query.page === undefined){
req.query.page = 1;
}
if(req.query.count === undefined){
req.query.count = 5;
}
db.getReviewsByProductID(req.query)
.then((data)=>{
res.send(data);
res.end();
})
.catch((err)=>{
console.log(err);
})
})
/*
Get Review Metadata
Returns review metadata for a given product.
GET /reviews/meta
Query Parameters
Parameter Type Description
product_id integer Required ID of the product for which data should be returned
{
"product_id": "40346",
"ratings": {
"1": "27",
"2": "56",
"3": "69",
"4": "53",
"5": "105"
},
"recommended": {
"false": "88",
"true": "222"
},
"characteristics": {
"Fit": {
"id": 135224,
"value": "3.0000000000000000"
},
"Length": {
"id": 135225,
"value": "3.2336065573770492"
},
"Comfort": {
"id": 135226,
"value": "3.1557377049180328"
},
"Quality": {
"id": 135227,
"value": "3.4750000000000000"
}
}
}
*/
app.get('/reviews/meta',(req, res)=>{
db.getMetadata(Number(req.query.product_id))
.then((data)=>{
//console.log('data', data)
res.send(data)
res.status(201)
})
.catch((err)=>{ res.send(err) })
.finally(()=>{ res.end() })
})
//To show differnce in peformance.
app.get('/reviews/meta/old',(req, res)=>{
db.getMetadata_old(Number(req.query.product_id))
.then((data)=>{
//console.log('data', data)
res.send(data)
res.status(201)
})
.catch((err)=>{ res.send(err) })
.finally(()=>{ res.end() })
})
app.post('/reviews', (req, res)=>{
db.postReview(req.body)
.then((data)=>{
res.send(data)
res.status(201)
})
.catch((err)=>{
res.send(err)
res.status(400)
})
.finally(()=>{
res.end();
})
})
app.put('/reviews/:review_id/helpful',(req, res)=>{
db.markAsHelpful(req.params.review_id)
.then((data)=>{
res.status(204)
res.send(data);
})
.catch((err)=>{
console.log(err)
})
.finally(()=>{
res.end();
})
})
app.put('/reviews/:review_id/report',(req, res)=>{
db.report(req.params.review_id)
.then((data)=>{
res.status(204)
res.send(data);
})
.catch((err)=>{
console.log(err)
})
.finally(()=>{
res.end();
})
})
app.listen(3000, ()=>{
console.log(`listening on port ${port}`);
});