-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (119 loc) · 5.48 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
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
const express = require('express');
const app = express();
const cors = require('cors');
const client = require('./database');
app.use(express.json());
app.use(express.static(__dirname));
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.post("/create-user", async (req, res) => {
try {
// Checking data
console.log(req.body);
// Getting the values from frontend
var firstName = req.body.firstName.toString();
var surname = req.body.surname.toString();
var displayName = req.body.displayName.toString();
var password = req.body.password.toString();
var dob = req.body.dob.toString();
var avatar = req.body.avatar.toString();
var bio = req.body.bio.toString();
var following = req.body.following.toString();
var obs_config_rmrplink = req.body.obs_config_rmrplink.toString();
var obs_config_streamkey = req.body.obs_config_streamkey.toString();
var live_uri = req.body.live_uri.toString();
var live_image = req.body.live_image.toString();
// Inserting the values to the db
const newUser = await client.query(
"INSERT INTO users (firstName, surname, displayName, password, dob, avatar, bio, following, obs_config_rmrplink, obs_config_streamkey, live_uri, live_image) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12); RETURNING *",
[firstName, surname, displayName, password, dob, avatar, bio, following, obs_config_rmrplink, obs_config_streamkey, live_uri, live_image]
);
res.json({ newUser });
} catch (error) {
console.log(error.message);
res.status(500).send("server Error");
}
});
app.put("/update-user/:id", async (req, res) => {
try {
var id = req.params.id.toString().trim();
console.log(id);
// Getting the values from frontend
var firstName = req.body.firstName.toString();
var surname = req.body.surname.toString();
var displayName = req.body.displayName.toString();
var password = req.body.password.toString();
var dob = req.body.dob.toString();
var avatar = req.body.avatar.toString();
var bio = req.body.bio.toString();
var following = req.body.following.toString();
var obs_config_rmrplink = req.body.obs_config_rmrplink.toString();
var obs_config_streamkey = req.body.obs_config_streamkey.toString();
var live_uri = req.body.live_uri.toString();
var live_image = req.body.live_image.toString();
if (firstName && surname && displayName && password && dob && avatar && bio && following && obs_config_rmrplink && obs_config_streamkey && live_uri && live_image) {
const userUpdate = await client.query("UPDATE users SET firstName = $2, surname = $3, displayName = $4, password = $5, dob = $6, avatar = $7, bio = $8, following = $9, obs_config_rmrplink = $10, obs_config_streamkey = $11, live_uri = $12, live_image = $13 WHERE user_id = $1",
[id, firstName, surname, displayName, password, dob, avatar, bio, following, obs_config_rmrplink, obs_config_streamkey, live_uri, live_image]);
res.status(200).send("User Updated");
} else {
console.log("Fill the required fields");
}
} catch (error) {
console.log(error.message);
res.status(500).send("server Error");
}
});
app.post("/follow/:userId/:userToFollowId", async (req, res) => {
try {
var userId = req.params.userId.toString().trim();
var userToFollowId = req.params.userToFollowId.toString().trim();
const followers = client.query("UPDATE users SET following = $2 WHERE user_id = $1", [userId, userToFollowId]);
res.status(200).send(followers);
// '{{"meeting", "lunch"}, {"meeting"}}');
} catch (error) {
console.log(error.message);
res.status(500).send("server Error");
}
});
app.post("/unfollow/:userId/:userToUnFollowId", async (req, res) => {
try {
var userId = req.params.userId.toString().trim();
var userToUnFollowId = req.params.userToUnFollowId.toString().trim();
const followers = client.query("DELETE following = $2 FROM users WHERE user_id = $1", [userId, userToUnFollowId]);
res.status(200).send(followers);
} catch (error) {
console.log(error.message);
res.status(500).send("server Error");
}
});
app.get("/followers/:userId", async (req, res) => {
try {
var userId = req.params.userId.toString().trim();
const followers = client.query("SELECT following FROM users WHERE user_id = $1", [userId]);
res.status(200).send(followers);
} catch (error) {
console.log(error.message);
res.status(500).send("server Error");
}
});
app.get("/users", async (req, res) => {
try {
const users = await client.query("SELECT * FROM users;");
// users.rows.map(item => ({
// ...item,
// id: item.user_id,
// live: {
// uri: item.live_uri,
// image: item.live_image
// },
// obs_config: {
// rmrpLink: item.obs_config_rmrplink,
// streamKey: item.obs_config_streamkey
// }
// }))
res.status(200).send(users);
} catch (error) {
console.log(error);
}
});
app.listen(process.env.PORT, () => console.log('server running'));