forked from teamTwizzlers/questionsAnswers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
183 lines (160 loc) · 5.06 KB
/
Copy pathapp.js
File metadata and controls
183 lines (160 loc) · 5.06 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
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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
var cors = require('cors')
const StatsD = require("node-statsd"), statsDClient = new StatsD();
const app = express();
const PORT = 3030;
app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const MongoClient = require('mongodb').MongoClient;
let ObjectId = require('mongodb').ObjectId;
const assert = require('assert');
const url = 'mongodb://localhost:27017';
const dbName = 'questionAnswers';
app.get('', (req, res) => {
res.send('connected to api!')
})
//getQA
app.get('/qa/:id', (req, res) => {
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
const product_id = Number(req.params.id);
db.collection('questions').find({ 'product_id': product_id }).toArray(function (err, result) {
if (err) {
res.send(err)
}
res.send(result)
client.close();
});
});
});
//getSpecificAnswers
app.get('/qa/:questionId/answers', (req, res) => {
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
let question_id = req.params.questionId;
db.collection('answerscombined').find({ 'question_id': new ObjectId(question_id) }).toArray(function (err, result) {
if (err) {
res.send(err)
}
res.send(result);
client.close();
});
});
})
//askQuestion
app.post('/qa/:id', (req, res) => {
console.log(req)
let body = req.body.body;
let name = req.body.name;
let email = req.body.email;
let time = Date.now();
let product_id = Number(req.params.id);
let obj = { product_id: product_id, question_body: body, question_date: time, asker_name: name, asker_email: email, report: 0, question_helpfulness: 0 }
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
db.collection('questions').insertOne(obj, function (err, result) {
if (err) {
res.send(err)
}
res.send('inserted into questions collection')
client.close();
});
});
})
//answerQuestion
app.post('/qa/:questionId/answers', (req, res) => {
console.log(req.body)
let body = req.body.body;
let name = req.body.name;
let email = req.body.email;
let photos = req.body.photos;
let time = Date.now();
let question_id = req.params.questionId;
let answerObj = { question_id: new ObjectId(question_id), body: body, date: time, answerer_name: name, answerer_email: email, report: 0, helpfulness: 0, photos: photos }
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
db.collection('answerscombined').insertOne(answerObj, function (err, result) {
if (err) {
res.send(err)
}
res.send('inserted into answers collection')
client.close();
})
})
})
//markQAsHelpful
app.put('/qa/question/:questionId/helpful', (req, res) => {
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
const question_id = req.params.questionId;
db.collection('questions').update({ _id: new ObjectId(question_id) }, { $inc: { question_helpfulness: 1 } }, function (err, result) {
if (err) {
res.send(err)
}
res.send('success')
client.close();
});
});
})
//reportQuestion
app.put('/qa/question/:questionId/report', (req, res) => {
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
const question_id = req.params.questionId;
db.collection('questions').update({ _id: new ObjectId(question_id) }, { $set: { reported: 1 } }, function (err, result) {
if (err) {
res.send(err)
}
res.send('success')
client.close();
});
});
})
//markAnsAsHelpful
app.put('/qa/answer/:answerId/helpful', (req, res) => {
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
const answer_id = req.params.answerId;
db.collection('answerscombined').update({ _id: new ObjectId(answer_id) }, { $inc: { helpfulness: 1 } }, function (err, result) {
if (err) {
res.send(err)
}
res.send('success')
client.close();
});
});
})
//reportAns
app.put('/qa/answer/:answerId/report', (req, res) => {
const client = new MongoClient(url);
const db = client.connect(function (err) {
assert.equal(null, err);
const db = client.db(dbName);
const answer_id = req.params.answerId;
db.collection('answerscombined').update({ _id: new ObjectId(answer_id) }, { $set: { reported: 1 } }, function (err, result) {
if (err) {
res.send(err)
}
res.send('success')
client.close();
});
});
})
module.exports = app;