-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.js
89 lines (74 loc) · 3.41 KB
/
database.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
try {
require('dotenv').config()
} catch (error) {
console.log(error);
}
const { createClient } = require('@supabase/supabase-js')
const supabase = createClient(process.env.DB_URL, process.env.DB_KEY)
class API {
async newParticipant(participant) {
const { data, error } = await supabase.from('Participants').insert(participant);
console.log("New Participant DB Query Data: ", data, " Error: ", error)
const participantID = data[0].participantID;
await this.addEmptyScoreEntry(participantID);
return participantID;
}
async addEmptyScoreEntry(participantID) {
//Add admin feature to change facelistID being served.
const activeFacelistID = await this.fetchActiveFacelist();
const { data, error } = await supabase.from('FacenameScore').insert({ participantID: participantID, facelistID: activeFacelistID });
return data;
}
async updateScore(participantID, param, score) {
const { data, error } = await supabase.from('FacenameScore').update({
[param]: score
}).match({ participantID: participantID });
return data;
}
async fetchActiveFacelist() {
const { data, error } = await supabase.from('Facelist').select('facelistID').match({ active: true });
return data[0].facelistID;
}
async fetchImageSequence() {
const { data, error } = await supabase.from('Facelist').select('image_sequence').match({ active: true });
console.log("Image Sequence: ", data)
return data[0].image_sequence;
}
async fetchFaceData(selectionString = "imageID, name, affiliation") {
const imageSequenceString = await this.fetchImageSequence();
const imageSequence = imageSequenceString.split(',');
console.log("Image sequence retrieved is: ", imageSequence)
const { data, error } = await supabase.from('Facedata').select(selectionString).in('imageID', imageSequence);
return data;
}
async addNewFace(imageID, name, affiliation) {
const { data, error } = await supabase.from('Facedata').insert({ imageID: imageID, name: name, affiliation: affiliation });
return data;
}
async checkAdmin(username_arg, hashedpwd_arg) {
const { data, error } = await supabase.from('Admin').select('username').match({ username: username_arg, hashedpwd: hashedpwd_arg });
if (data.length > 0) return true;
else return false;
}
async fetchFaceNameResults(participantID) {
const { data, error } = await supabase.from('FacenameScore').select().match({ participantID: participantID });
return data;
}
async fetch_lastimg(gender) {
const { data, error } = await supabase.from('last_face').select('number').match({ gender: gender });
//console.log("Number: ", data[0].number);
return data[0].number;
}
async update_lastimg(gender, img_num) {
const { data2, error2 } = await supabase.from('last_face').update({number: img_num}).match({ gender: gender });
// console.log("Image number updated ");
return 1;
}
async saveCorsiScore(participantID, correctCount, longestSpan) {
const { data, error } = await supabase.from('CorsiblockScores').insert({ participantID: participantID, correctCount: correctCount, longestSpan: longestSpan });
if (error)
throw (error)
return data;
}
}
module.exports = API;