-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
126 lines (114 loc) · 3.64 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
const express = require('express')
const {MongoClient, ObjectId} = require('mongodb')
const app = express()
const port = 3000
app.use(express.json())
//db connection
async function connectToMongo() {
const connection = await MongoClient.connect("mongodb://root:password@localhost:27017")
return connection.db('revision')
}
//serve static
app.use(express.static('public'));
//routes
//home page:
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
//backend api
app.get('/qacodes', async (request, response) => {
try {
const db = await connectToMongo()
const qas = db.collection('questions_and_answers')
const result = await qas.find({}).project({qacode: 1, _id: 0}).toArray()
if (result.length > 0) {
response.json({
message: "Successfully retrieved qacodes",
data: result
});
response.status(200);
} else {
response.status(500).send({
message: "Unexpected error",
data: []
})
}
} catch {
response.status(500).send('Unexpected error');
}
})
app.get('/qa', async (request, response) => {
const qaParams = request.query
function filterProducts(qaParams) {
const filter = {};
if (qaParams && qaParams.qacode) {
filter.qacode = parseInt(qaParams.qacode);
}
return filter;
}
try {
const db = await connectToMongo()
const qas = db.collection('questions_and_answers')
const result = await qas.find(filterProducts(qaParams)).toArray()
if (result.length > 0) {
response.json({
message: "Successfully retrieved question and answer",
data: result
});
response.status(200);
} else {
response.status(500).send({
message: "Unexpected error",
data: []
})
}app.post('/addQA', async (request, response) => {
try {
const db = await connectToMongo()
const qas = db.collection('questions_and_answers')
//get request body
const newQandA = request.body
const result = await qas.insertOne(newQandA)
if (result.insertedId !== null) {
response.status(201).json({
message: "Successfully added question and answer"
});
} else {
response.status(400).send({
message: "Invalid question and answer",
data: []
})
}
} catch {
response.status(500).send({
message: 'Unexpected error'
});
}
})
} catch {
response.status(500).send('Unexpected error');
}
})
app.post('/addQA', async (request, response) => {
try {
const db = await connectToMongo()
const qas = db.collection('questions_and_answers')
//get request body
const newQandA = request.body
const result = await qas.insertOne(newQandA)
if (result.insertedId !== null) {
response.status(201).json({
message: "Successfully added question and answer"
});
} else {
response.status(400).send({
message: "Invalid question and answer",
data: []
})
}
} catch {
response.status(500).send({
message: 'Unexpected error'
});
}
})
app.listen(port);