forked from panzerox123/res_review_app_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (121 loc) · 3.6 KB
/
index.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
127
128
129
const express = require('express');
const { Client } = require('pg');
const app = express();
const PORT = process.env.PORT || 8000;
app.use(express.json())
app.post('/createCustomer', async (req, res) => {
client = new Client();
client.connect(err => {
if (err) {
console.error(err);
} else {
console.log('Connected to DB');
}
});
//back = await client.query('SELECT * FROM Customer where customerid = 2')
try {
back = await client.query(`INSERT INTO login_credentials values ('${req.body['username']}', '${req.body['password']}')`);
back = await client.query(`INSERT INTO Customer (name, emailid, phoneno, username) values ('${req.body['name']}', '${req.body['email']}', ${req.body['phoneno']}, '${req.body['username']}')`);
client.end()
} catch {
res.status(400).send();
return;
}
res.status(200).send();
});
app.post('/login', async (req, res) => {
client = new Client();
client.connect(err => {
if (err) {
console.error(err);
} else {
console.log('Connected to DB');
}
});
try {
back = await (client.query(`SELECT usertype FROM login_credentials WHERE username='${req.body['username']}' and password='${req.body['password']}'`))
client.end()
} catch {
res.status(403).send();
return;
}
res.status(200).send(back.rows)
})
app.post('/viewOrders', async (req, res) => {
client = new Client();
client.connect(err => {
if (err) {
console.error(err);
} else {
console.log('Connected to DB');
}
});
try {
back = await client.query(`SELECT * FROM ORDERS O JOIN CUSTOMER C ON O.customerid = C.customerid WHERE C.username='${req.body['username']}'`);
client.end()
} catch {
res.status(400).send()
return
}
res.status(200).send(back.rows)
})
app.get('/viewRestaurantReviews', async(req,res) => {
client = new Client();
client.connect(err => {
if (err) {
console.error(err);
} else {
console.log('Connected to DB');
}
});
try {
back = await client.query(`SELECT r.comment, r.rating, r.time_stamp, s.name, c.name from (reviews r join customer c on r.customerid=c.customerid) join restaurants s on s.restaurantid = r.restaurantid`)
client.end()
} catch(err) {
console.log(err)
res.status(404).send()
return
}
res.status(200).send(back.rows)
})
app.get('/viewAllRestaurants', async(req,res)=>{
client = new Client();
client.connect(err => {
if (err) {
console.error(err);
} else {
console.log('Connected to DB');
}
});
try {
back = await client.query(`SELECT * FROM restaurants`);
client.end()
} catch (err){
console.log(err)
res.status(404).send()
return
}
res.status(200).send(back.rows)
})
app.post('/foodGivenRestaurant', async(req,res)=>{
client = new Client();
client.connect(err => {
if (err) {
console.error(err);
} else {
console.log('Connected to DB');
}
});
try {
back = await client.query(`SELECT * FROM food f JOIN restaurant_food r on f.foodid = r.foodid where r.restaurantid = ${req.body['resid']}`);
client.end()
} catch (err){
console.log(err)
res.status(404).send()
return
}
res.status(200).send(back.rows)
})
app.listen(PORT, () => {
console.log(`Server started on ${PORT}`);
})