Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (73 loc) · 1.97 KB
/
Copy pathserver.js
File metadata and controls
87 lines (73 loc) · 1.97 KB
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
require('dotenv').config()
// Initialize Server
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const PORT = process.env.SERVER_PORT || 9999;
app.use(bodyParser.json());
const db = require('./db/db.js');
const queries = require('./db/queries.js');
// GET /reviews/
app.get('/reviews', (req, res) => {
const {
page,
count,
sort,
product_id
} = req.query;
queries.getReviews(page, count, sort, product_id)
.then(response => {
res.status(response.status).end(JSON.stringify(response.data));
})
.catch(error => res.status(500));
});
// GET /reviews/meta
app.get('/reviews/meta', (req, res) => {
const {
product_id,
} = req.query;
queries.getReviewsMeta(product_id)
.then(response => {
res.status(response.status).end(JSON.stringify(response.data));
})
.catch(error => res.status(500));
});
// POST /reviews
app.post('/reviews', (req, res) => {
queries.postReview(req.body)
.then(response => {
res.status(response.status).send(JSON.stringify(response.message));
})
.catch(error => res.status(500));
});
// PUT /reviews/:review_id/helpful
app.put('/reviews/:review_id/helpful', (req, res) => {
const {
review_id,
} = req.params;
queries.putReviewHelpful(review_id)
.then(response => {
res.status(response.status).send(JSON.stringify(response.message));
})
.catch(error => res.status(500));
});
// PUT /reviews/:review_id/report
app.put('/reviews/:review_id/report', (req, res) => {
const {
review_id,
} = req.params;
queries.putReviewReport(review_id)
.then(response => {
res.status(response.status).send(JSON.stringify(response.message));
})
.catch(error => res.status(500));
});
app.get(`/${process.env.LOADERIO_API}`, (req, res) => {
res.send(process.env.LOADERIO_API);
});
// Start the server
const server = app.listen(PORT, () => console.log(`listening on ${PORT}\n`));
module.exports = {
server,
db
};