-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
208 lines (172 loc) · 6.37 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var pg = require('pg');
// var socket = io();
var isLoggedIn = false;
var userType = null;
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/public/login.html');
});
app.get('course.html', function(req, res){
if (isLoggedIn)
res.sendFile(__dirname + '/public/login.html');
else
res.sendFile(__dirname + '/public/course.html');
});
var client = new pg.Client({
user: 'postgres',
password: '8yn4ZdxUJF6uP7rIKrkO',
database: 'postgres',
host: 'umquestiondb.cz0erjq79iwr.us-east-1.rds.amazonaws.com',
port: 5432
});
client.connect();
client.query('SELECT * FROM account', (err, res) => {
console.log(err ? err.stack : res.rows[0]); // Hello World!
});
setTimeout(function(){
client.end();},
100000000);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('fetch_posts', async function(dta){
//socket.emit('send_posts', [204, 205, 26]);
// First ask for all the questions related to the course.
// For each question, get its answers.
// [{question: {postID: , msg: , authorId: , endorseCount:} answers: [{postID: , msg: , questionID: , authorID: } , ...]}, ...]
var posts = [];
try {
let res = await client.query("SELECT * FROM post WHERE cid = " + dta + " AND ptype = 'q'");
if (res == null || res == undefined){
console.log ("res is " + res);
return;
}
for (let x = 0; x < res.rows.length; x++) {
// const = res.rows[x];
if (res == null && res == undefined){
console.log("ERROR: questionRes is undefined or null");
return;
}
posts[x] = {question: {postId: res.rows[x].pid, message: res.rows[x].content, authorId: res.rows[x].uid}, answers: []};
try {
let answerRes = await client.query('SELECT * FROM reply_to R, post P WHERE R.originalid = '+ res.rows[x].pid +' AND R.replyid = P.pid');
for (let y = 0; y < answerRes.rows.length; y++) {
posts[x].answers.push({postId: answerRes.rows[y].pid, message: answerRes.rows[y].content, authorId: answerRes.rows[y].uid});
}
} catch (err) {
console.log(err);
}
}
} catch (err) {
console.log(err);
}
socket.emit('send_posts', posts);
});
socket.on('fetch_course_data', function(dta){
client.query('SELECT * FROM course WHERE cid=$1', [dta], (err, res) => {
console.log(err ? err.stack : res.rows[0]);
socket.emit('send_course_data', {teacher: res.rows[0].uid, name: res.rows[0].name});
});
});
socket.on('setLoggedIn', (isLoggedIn, userType) => {
this.isLoggedIn = isLoggedIn;
this.userType = userType;
});
socket.on('userLoggedOut', () => {
this.isLoggedIn = false;
this.userType = null;
});
socket.on('fetch_users', function(dta){ // Needs to return array of user IDs attached to a course ID
socket.emit('send_users', [401, 402, 72]);
});
socket.on('fetch_teacher', function(dta){ // Needs to return the UserID of the teacher attached to the CourseID.
socket.emit('send_teacher', 60);
});
socket.on('Login_Validation', function(dta){ // Needs to return the UserID of the teacher attached to the CourseID.
console.log(dta);
client.query('SELECT * FROM account WHERE email = $1', [dta] , (err, res) => { // Just needs to return the password attached to this email *ALEX*
console.log(err ? err.stack : res.rows[0]); // Hello World!
if(res.rows[0] === undefined){
console.log("Query was undefined.");
socket.emit("Login_return", [-1, 0]);
}
else{
console.log("Query was defined.");
socket.emit("Login_return", [res.rows[0].upassword, res.rows[0].utype, res.rows[0].uid]);
}
});
});
socket.on('create_user', function(dta){
client.query('INSERT INTO account(email, upassword, utype) VALUES($1, $2, $3) RETURNING uid', [dta.email, dta.password, dta.type], (err, res) => {
console.log(err ? err.stack : res.rows[0]); // Hello World!
if(err){
socket.emit("create_confirm", 0);
}
else{
socket.emit("create_confirm", res.rows[0].uid);
}
});
});
socket.on('add_question', function(dta){
client.query('INSERT INTO post(uid, cid, ptype, content) VALUES($1, $2, $3, $4)', [dta.aID, dta.cID, 'q', dta.msg], (err, res) => {
console.log(err ? err.stack : res.rows[0]); // Hello World!
socket.emit('update_UI');
});
});
socket.on('add_answer', async function(dta){
let res1 = await client.query('INSERT INTO post(uid, cid, ptype, content) VALUES($1, $2, $3, $4) RETURNING pid', [dta.aID, dta.cID, 'a', dta.msg]);
let res2 = await client.query('INSERT INTO reply_to(replyid, originalid) VALUES($1, $2)', [res1.rows[0].pid, dta.qID]);
socket.emit('update_UI');
});
socket.on('searchingCourse', function(dta){
client.query('SELECT * from course WHERE cid=$1', [dta], (err, res) => {
console.log(err ? err.stack : res.rows[0]); // Hello World!
if(err){
socket.emit('courseFound', 0);
}
else{
socket.emit('courseFound', 1);
}
});
});
socket.on('create_course', async function(dta){
let res1 = await client.query('INSERT INTO course(uid, name) VALUES($1, $2)', [dta.uID, dta.name]);
socket.emit('update_UI');
});
socket.on('fetch_course', async function(dta){
console.log(dta);
let res1 = await client.query('SELECT * FROM course WHERE uid=$1', [dta]);
socket.emit('received_course', res1.rows);
});
socket.on('fetch_course_admin', async function(){
let res1 = await client.query('SELECT * FROM course');
socket.emit('received_course', res1.rows);
});
socket.on('fetch_single_course', async function(dta){
let res1 = await client.query("SELECT * FROM course WHERE cid="+ dta);
if(res1.rows[0] != null || res1.rows[0] != undefined)
socket.emit('received_single_course', res1.rows[0].name);
else
socket.emit('failed_received_single_course');
});
socket.on('delete_course', async function(dta){
let res1 = await client.query("DELETE FROM course WHERE cid=" + dta);
});
socket.on('delete_post', async function(dta){
let res1 = await client.query("DELETE FROM post WHERE pid=" + dta);
});
socket.on('delete_user', async function(dta){
try {
let res = await client.query("DELETE FROM account WHERE email=$1 RETURNING *",[dta]);
socket.emit('confirm_delete', res.rows.length);
} catch (error) {
console.log(error);
}
});
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening on *:3000');
});