-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
205 lines (160 loc) · 5.66 KB
/
db.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
import { createClient } from "@supabase/supabase-js";
// TODO: Add secrets
const supabaseUrl = "https://cscedmlehzsfhwojcahv.supabase.co";
const supabaseKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImNzY2VkbWxlaHpzZmh3b2pjYWh2Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzIxMjM1NTEsImV4cCI6MjA0NzY5OTU1MX0.LrjN_vWgXBd-44eLcxjm1RMizAR55QMhATHCAqIjSU8";//Replit code: process.env['SUPABASE_PUBLIC_KEY'];
const supabase = createClient(supabaseUrl, supabaseKey);
// !AUTHENTICATION! //
// Sign-Up
async function signUp(email, password, userType, name) {
const { data, error: signUpError } = await supabase.auth.signUp({
email: email,
password: password,
});
if (signUpError) {
throw new Error(signUpError.message);
}
// Add user to either Student or Instructor table
// based on user type when they signed up
const { error: tableError } = await supabase.from(userType).insert([
{
s_id: data.user.id,
name: name
},
]);
if (tableError) {
throw new Error(`Error inserting into ${userType} table`);
}
return data;
}
// Login
async function login(email, password) {
const { data, error } = await supabase.auth.signInWithPassword({
email: email,
password: password,
});
if (error) {
throw new Error(error.message);
}
return data;
}
// Get User Info
async function getUser() {
const { data } = await supabase.auth.getUser();
return data?.user;
}
// Logout Function
async function logout() {
const { error } = await supabase.auth.signOut();
if (error) {
throw new Error(error.message);
}
}
// !DATABASE FUNCTIONS! //
//RIGHT NOW, username will have to represent the first name
async function getStudentInfo(username) {
const { data, error } = await supabase.from("Student")
.select("*")
.eq("f_name", username);
if(error){
console.error("Error fetching student info:", error);
} else {
console.log("Student fetched:", data);
return data[0];
}
}
//Uses student s_id to get classes the student is registered in
async function getStudentClasses(s_id) {
const { data, error } = await supabase.from("StudentTakes")
//This should act like a join
.select(`c_code, Class(c_name)`)
.eq("s_id", s_id);
if (error) {
console.error("Error fetching classes student is enrolled in:", error);
} else {
console.log("Student classes fetched:", data);
return data;
}
}
async function getAllClassAssignments(c_code){
const { data, error } = await supabase.from("Assignment")
.select(`*`)
.eq("c_code", c_code);
if (error) {
console.error("Error fetching assignments for a class:", error);
} else {
console.log("Assignments fetched:", data);
return data;
}
}
async function getAllSubmissions(a_id){
const { data, error } = await supabase.from("Submission")
.select(`*`)
.eq("a_id", a_id);
if (error) {
console.error("Error fetching submissions:", error);
} else {
console.log("Submissions fetched:", data);
return data;
}
}
async function getSubmission(s_id, a_id){
const { data, error } = await supabase.from("Submission")
.select(`*`)
.eq("s_id", s_id)
.eq("a_id", a_id);
if (error) {
console.error("Error fetching submission:", error);
} else {
console.log("Submission fetched:", data);
return data[0];
}
}
//RIGHT NOW, username will have to represent the first name
async function getTeacherInfo(username) {
const { data, error } = await supabase.from("Teacher")
.select("*")
.eq("f_name", username);
if(error){
console.error("Error fetching teacher info:", error);
} else {
console.log("Teacher fetched:", data);
return data[0];
}
}
async function getTeacherClasses(t_id){
const { data, error } = await supabase.from("Classes")
//This should act like a join
.select(`*`)
.eq("t_id", t_id);
if (error) {
console.error("Error fetching classes for teacher:", error);
} else {
console.log("Teacher classes fetched:", data);
return data;
}
}
//TESTING CODE
// const s_id = (await getStudentInfo("John")).s_id;
// console.log(s_id);
// const c_code = (await getStudentClasses(s_id))[0].c_code;
// console.log(c_code);
// const a_id = (await getAllClassAssignments(c_code))[0].a_id;
// console.log(a_id);
// const submissions = await getAllSubmissions(a_id);
// const submission = await getSubmission(s_id, a_id);
// console.log(submissions[0].video + " " + submission.video);
// const t_id = (await getTeacherInfo("Jane")).t_id;
// console.log(t_id);
// c_code = (await getTeacherClasses(t_id))[0].c_code;
// console.log(c_code);
export default {
signUp,
login,
getStudentInfo,
getStudentClasses,
getAllClassAssignments,
getSubmission,
getAllSubmissions,
getTeacherInfo,
getTeacherClasses
};