forked from rpp32-sdc-saline/questions_answers-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (82 loc) · 2.23 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
const dbQueries = require('./database/indexDB.js');
const express = require('express');
const client = require('./redis.js');
const app = express();
(async () => {
console.log('connecting client')
await client.connect();
})();
client.on('error', (err) => console.log('Redis Client Error: ', err));
const cache = async (req, res, next) => {
try {
const productID = req.query.product_id;
const queryResult = await client.get(req.query.product_id);
if (queryResult) {
res.send(JSON.parse(queryResult));
} else {
next();
}
} catch (err) {
res.send({ERROR: err});
}
}
app.use(express.json());
app.use('/', express.static('files'));
app.get('/qa/questions', cache, async (req, res) => {
try {
const allData = await dbQueries.allData(Number(req.query.product_id));
await client.set(req.query.product_id, JSON.stringify(allData));
res.send(allData);
} catch (error) {
res.send({ERROR: error});
}
});
app.post('/qa/questions', async (req, res) => {
try {
const hasAllParams = await dbQueries.paramsCheck(req.query);
if (!hasAllParams){
res.sendStatus(400);
return;
} else {
const result = await dbQueries.insertQuestion(req.query);
return;
}
} catch (error) {
res.send({ERROR: error});
}
});
app.get('qa/questions/test', cache, async (req, res) => {
try {
const productId = Math.floor(Math.random() * 1000012)
const allData = await dbQueries.allData(productId);
await client.set(productId, JSON.stringify(allData));
res.send(allData);
} catch (error) {
res.send({ERROR: error});
}
})
// app.get('/qa/questions/:question_id/answers', (req, res) => {
// var questionId = req.params.question_id;
// dbQueries.pullAllAnswers(questionId, (err, result) => {
// if (err) {
// res.send({err});
// } else {
// console.log(result);
// result = JSON.stringify(result);
// res.send(result);
// }
// })
// });
/**
* BELOW IS THE WORK TO BE COMPLETED IF TIME ALLOWS
*/
// app.post('/answers', (req, res) => {
// res.send({});
// });
// app.patch('/reportAnswer', (req, res) => {
// res.send({});
// });
// app.patch('/reportQuestion', (req, res) => {
// res.send({});
// });
module.exports = app;