-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (81 loc) · 2.03 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
const express = require('express');
const port = 8080;
const Mongodb = require('./mongo_database');
// const MySQLdb = require('./mysql_database');
const app = express();
app.use(express.json());
app.get('/questions', (req, res) => {
const param = req.body;
Mongodb.getQuestionsById(param.product_id, param.count, param.page, (err, data) => {
if (err) {
res.status(400).end(err);
} else {
res.send(data);
}
})
});
app.get('/questions/:question_id/answers', (req, res) => {
const param = req.body;
const id = req.params.question_id;
Mongodb.getAnswersByQuestionId(Number(id), param.count, param.page, (err, data) => {
if (err) {
res.status(400).end(err);
} else {
res.send(data);
}
})
});
app.post('/questions', (req, res) => {
Mongodb.addNewQuestion(req.body, (err, data) => {
if(err) {
res.status(400).end(err);
} else {
res.sendStatus(201);
}
})
});
app.post('/questions/:question_id/answers', (req, res) => {
const id = req.params.question_id;
req.body.question_id = Number(id);
Mongodb.addNewAnswer(req.body, (err, data)=>{
if(err) {
res.status(400).end(err);
} else {
res.sendStatus(201);
}
})
});
app.put('/questions/:question_id/helpful', (req, res) => {
const id = req.params.question_id;
Mongodb.markQuestionHelpful(Number(id), (err, data) => {
if(err) {
res.status(400).end(err);
} else {
res.sendStatus(204);
}
})
});
app.put('/answers/:answer_id/helpful', (req, res) => {
const id = req.params.question_id;
Mongodb.markAnswerHelpful(id, (err, data) => {
if(err) {
res.status(400).end(err);
} else {
res.sendStatus(204);
}
})
});
app.put('/answers/:answer_id/report', (req, res) => {
const id = req.params.question_id;
Mongodb.reportAnswer(id, (err, data) => {
if(err) {
res.status(400).end(err);
} else {
res.sendStatus(204);
}
})
})
app.listen(port, (error) => {
if (error) console.log(error);
console.log(`Listening to port ${port}`);
});