-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (93 loc) · 3.5 KB
/
app.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
const express = require("express");
const app = express();
//app.use(express.urlencoded({extended: true})); //for GET request with params in URL
app.use(express.json()); // for POST requests with JSON in the body
// req.body === json data in body of POST request
// req.params.id === app.get(/reviews/:id)
// req.query === query params (localhost/reviews?param=value)
const storage = require('./db/storage.js');
const authentication = require('./authentication.js');
storage.createAndPopulateTables();
//
// Auth Endpoint for loader.io
//
app.get('/' + authentication.loaderio, (req, res) => {
res.send(authentication.loaderio);
})
//
// Endpoints
//
// GET /reviews/
//
// 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('/reviews', (req, res) => {
//console.log('Accepting GET request to /reviews, query: ', req.query);
// check for required parameter
if (!('product_id' in req.query)) {
return res.status(422).send('Error: no product_id provided');
}
// default parameters
if (!('page' in req.query)) {
req.query.page = 1;
}
if (!('count' in req.query)) {
req.query.count = 5;
}
if (!('sort' in req.query)) {
req.query.sort = 'relevant';
}
storage.readReviewsForProductId(req.query.product_id)
.then((result) => {
res.send(result);
});
});
// GET /reviews/meta
//
// product_id integer Required ID of the product for which data should be returned
app.get('/reviews/meta', (req, res) => {
//console.log('Accepting GET request to /reviews/meta, query: ', req.query);
//check for required parameter
if (!('product_id' in req.query)) {
res.status(422).send('Error: no product_id provided');
}
// database access / calculations
storage.readReviewMetaForProductId(req.query.product_id)
.then(meta => res.send(meta));
});
// POST /reviews
//
// Parameter Type Description
// product_id integer Required ID of the product to post the review for
// rating int Integer (1-5) indicating the review rating
// summary text Summary text of the review
// body text Continued or full text of the review
// recommend bool Value indicating if the reviewer recommends the product
// name text Username for question asker
// email text Email address for question asker
// photos [text] Array of text urls that link to images to be shown
// characteristics object Object of keys representing characteristic_id and values representing the review value for that characteristic. { "14": 5, "15": 5 //...}
app.post('/reviews', (req, res) => {
console.log('Accepting POST request to /reviews, body: ', req.body);
// database access, save review
storage.writeReview(req.body)
.then(() => res.sendStatus(201));
});
// PUT /reviews/:review_id/helpful
app.put('/reviews/:review_id/helpful', (req, res) => {
console.log('Marking review as helpful: ', req.params.review_id);
// database access, update review
storage.markReviewHelpful(req.params.review_id)
.then(res.sendStatus(204));
});
// PUT /reviews/:review_id/report
app.put('/reviews/:review_id/report', (req, res) => {
console.log('Reporting review: ', req.params.review_id);
// database access, update review
storage.markReviewReported(req.params.review_id)
.then(res.sendStatus(204));
});
module.exports = app;