-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB_Functions.js
213 lines (195 loc) · 6.31 KB
/
DB_Functions.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
209
210
211
212
213
import mysql from 'mysql2'
import dotenv from 'dotenv'
dotenv.config();
/*
* Remember to setup your own .env file
*/
var db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB,
socketPath: process.env.DB_SOCKETPATH
}).promise();
//LISTENING FOR ERRORS WHILE ESTABLISHING CONNECTION
db.on('error', function(err) {
console.log("COULDN'T CONNECT TO DATABASE:\n", err);
});
//VIEW EXISTING COURSES IN THE TABLE
export async function viewCourses(){
const Q = `
SELECT * FROM Courses;
;`;
try{
const [courses] = await db.query(Q);
return courses;
}catch(err){
return "Could Not Fetch Courses"
}
}
//ADD A NEW COURSE - NOTE: cannot add course if a record with the same course_name and instructor_id exists
export async function addCourse(courseObj){
const Q = `
INSERT INTO Courses (course_name, instructor_id, max_seat, free_seats, start_date) VALUES
(?,?,?,?,?)
;`;
//Retrieve records with matching name and instructor_id
const checkExistingRecords = `
SELECT course_id FROM Courses WHERE course_name = ? AND instructor_id = ?;
`;
const [r] = await db.query(checkExistingRecords,[courseObj[0],courseObj[1]])
if(r.length == 0){ //IF NO SUCH RECORDS EXIST, WE ARE GOOD TO GO
try{
await db.query(Q,courseObj);
return "Successfully Added to Courses DB"
}catch(err){
return "Error Adding Course!"+err;
}
}
else{
return "Course Already Exists!"
}
}
//UPDATE A COURSE
export async function updateCourse(id,courseObj){
const values = courseObj.concat(id);
const Q=`
UPDATE Courses
SET course_name = ?, instructor_id = ?, max_seat = ?, free_seats = ?, start_date = ?
WHERE course_id = ?
;`;
const [r] = await db.query(`SELECT * FROM Courses WHERE course_name =? AND instructor_id=?`,[courseObj[0],courseObj[1]]);
if(r.length>0){
await db.query(`
UPDATE Courses
SET max_seat=?, free_seats=?, start_date=?
WHERE course_id = ?
;`,[courseObj[2],courseObj[3],courseObj[4],id]);
return "Duplicate Course Exists -- Updated Existing Course"
}
else{
try{
await db.query(Q, values)
const [r] = await db.query(`SELECT * FROM Courses WHERE course_id = ?`,[id])
return "Updated Course\n"+JSON.stringify(r)
}catch(err){
return "Could Not Update Course \n"+err
}
}
}
//REGISTER NEW LEARNER -- email is unique
export async function registerLearner(learnerObj){ //email id is unique
const Q=`
INSERT INTO Learners (name, email, phone_number, linkedin) VALUES (?,?,?,?)
;`;
try{
await db.query(Q,learnerObj)
return "User Registered Successfully!"
}catch(err){
return "Unable to Register User:"+err;
}
}
//A LEAD IS CREATED WHENEVER A NEW USER REGISTERS
export async function registerLead(c_id, l_email){
const get_learner_id=`
SELECT learner_id FROM Learners WHERE
email = ?
;`;
try{ //GET LEARNER ID
let [l_id] = await db.query(get_learner_id,[l_email]);
l_id = l_id[0].learner_id;
try{
const [r] = await db.query(
`SELECT start_date, free_seats FROM Courses
WHERE course_id=?`,[c_id]);
const curDate = new Date();
let status="";
//SET STATUS
if(r[0].start_date > curDate)
status = "Accept";
else
status = "Reject"; //Course has already Started!
if(r[0].free_seats==0) //The course instructor can update waitlisted candidates
status = 'Waitlist';
try{
const insertLead=`
INSERT INTO Leads (course_id, learner_id, status) VALUES
(?,?,?)
`;
await db.query(insertLead,[c_id, l_id, status]);
if(status == 'Accept'){
await db.query(`
UPDATE Courses
SET free_seats = free_seats - 1
WHERE course_id = ?;
`,[c_id]);
}
return "Lead Created for learner. Entrollment Status: "+status;
}catch(err){
return "Error Creating Lead:\n"+err;
}
}catch(err){
return "Error Registering:\n"+err;
}
}catch(err){
return "Learner not registered!:\n",+err;
}
}
//UPDATE LEAD
export async function updateLead(id, leadInfo){
const values = leadInfo.concat(id);
const Q=`
UPDATE Leads
SET course_id = ?, learner_id = ?, status = ?
WHERE lead_id = ?
;`;
try{
await db.query(Q,values);
return "Lead Updated Successfully!";
}catch(err){
return "Lead Could not be Updated!";
}
}
//SEARCH LEAD my learner email (since it's unique)
export async function searchLead(email){
const Q=`
SELECT Leads.lead_id, Leads.course_id, Leads.learner_id, Leads.status FROM Leads
JOIN Learners
ON Leads.learner_id = Learners.learner_id
WHERE Learners.email = ?;
;`;
try{
const [r]=await db.query(Q,[email]);
return r
}catch(err){
return "Could not retrieve lead"
}
}
//ADD COMMENT
export async function addComment(commentObj){
const Q=`
SELECT * FROM Leads
JOIN Courses ON Leads.course_id = Courses.course_id
WHERE Leads.lead_id = ? AND Courses.instructor_id = ?
;`;
/*
* Why not just add INSERT in query 'Q'?
* While it would work just fine, it won't return a statement if the record is not inserted
*/
try{
const [r] = await db.query(Q,[commentObj[0],commentObj[1]])
//lead_id and instructor_id
if(r.length > 0){
const insertComment = `
INSERT INTO Comments (lead_id, instructor_id, comment) VALUES
(?,?,?);
`
await db.query(insertComment, commentObj)
return "Comment added Successfully"
}else{
return "The Instructor Does Not Teach the Course corresponding to the Lead";
}
}catch(err){
return "Could not Add Comment:\n"+err;
}
}