Repository navigation
Expand file tree
/
Copy pathdatabase.js
More file actions
215 lines (168 loc) · 5.75 KB
/
Copy pathdatabase.js
File metadata and controls
215 lines (168 loc) · 5.75 KB
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
214
215
async function getClub(userInput) {
for (i = 0; i < userInput.length; i++) {
console.log(userInput[i]);
}
var rating = 0;
var clubinfo;
var firebase = require('firebase');
var serviceAccount = require("./clubselector-2394a-firebase-adminsdk-oc2gl-21c4ff87c9.json");
if (firebase.apps.length === 0)
{
//IF firebase is not connected, then connect
firebase.initializeApp
(
{
apiKey: "AIzaSyDrtl3qnLx_ZfdgxpUVy5XFvkXR4_f6_VU",
authDomain: "clubselector-2394a.firebaseapp.com",
databaseURL: "https://clubselector-2394a.firebaseio.com",
projectId: "clubselector-2394a",
storageBucket: "clubselector-2394a.appspot.com",
messagingSenderId: "1039301954159",
appId: "1:1039301954159:web:e458dc98ecad8303"
}
);
}
//Go to the database in firebase
var db = firebase.app().database();
//Go to the first node in the database
var ref = db.ref('clubselector-2394a');
var clubbool;
clubbool = false;
var finalstring;
finalstring = "";
var clubnoexist;
clubnoexist = "Club doesn't exist" + '</br>';
console.log(userInput.length);
var counter;
counter = 0;
var clubList = new Array();
for (i = 0; i < userInput.length; i++) {
var keyword = "";
console.log("begin");
var currentclubbool;
currentclubbool = false;
var children;
children = userInput[i];
console.log(children);
console.log(typeof children);
var refTest = firebase.app().database().ref().child('clubs').child(children);
var refTestprime = refTest.child('Rating');
var snapshot = await refTestprime.once('value');
if(snapshot.exists()) {
clubbool = true;
currentclubbool = true;
rating = snapshot.val();
}
else {
rating = -5;
}
var refTestprime = refTest.child('Keywords');
var snapshot = await refTestprime.once('value');
var keywordarray;
keywordarray = [];
finalkeyword = [];
if(snapshot.exists()) {
clubbool = true;
currentclubbool = true;
keywordarray.push(snapshot.val());
for (var k in keywordarray[0]) {
var string;
string = k;
keyword = keyword + string + ","
}
keyword = keyword.slice(0,-1);
}
else {
keyword = "poo";
}
var refTestprime = refTest.child('ClubInfo');
var snapshot = await refTestprime.once('value');
if(snapshot.exists()) {
clubinfo = snapshot.val();
}
else {
clubinfo = "you suck";
}
if (currentclubbool == true) {
finalstring = finalstring + "Club Title: " + JSON.stringify(userInput[i]) + '</br>' + "Rating: " + JSON.stringify(rating) + '</br>' + "Keyword: " + JSON.stringify(keyword) + '</br>' + "Clubinfo: " + JSON.stringify(clubinfo) + '</br>';
console.log(finalstring);
var title = (userInput[i]);
var ratingReturn = JSON.stringify(rating);
var keywordReturn = JSON.stringify(keyword);
var clubinfoReturn = JSON.stringify(clubinfo);
clubList[counter++] = new Array(title,ratingReturn,keywordReturn,clubinfoReturn);
}
}
if (clubbool == false) {
clubList[0] = new Array("club doesn't exist", -5, "", "");
}
//returnResult(clubList);
return clubList;
}
async function matchclub(input) {
var iskeyword;
iskeyword = false;
input = '"' + input + '"';
var rating = 0;
var keyword = "Error";
var clubinfo;
if (firebase.apps.length === 0) {
firebase.initializeApp (
{
apiKey: "AIzaSyDrtl3qnLx_ZfdgxpUVy5XFvkXR4_f6_VU",
authDomain: "clubselector-2394a.firebaseapp.com",
databaseURL: "https://clubselector-2394a.firebaseio.com",
projectId: "clubselector-2394a",
storageBucket: "clubselector-2394a.appspot.com",
messagingSenderId: "1039301954159",
appId: "1:1039301954159:web:e458dc98ecad8303"
}
);
}
var ref = firebase.app().database().ref().child('clubs');
var returnArr = [];
var counts = [];
var output = [];
var title = [];
var final = [];
await ref.once('value', function(snap) {
snap.forEach(function(item) {
var itemVal = item.val();
returnArr.push(itemVal);
}
);
});
for (i = 0; i < returnArr.length; i++) {
counts.push((returnArr[i]).Keywords);
title.push((returnArr[i]).Clubname);
}
for (i = 0; i < counts.length; i++) {
var l = counts[i];
for (var k in l) {
var string;
string = JSON.stringify(k);
if (string == input) {
console.log("yes");
iskeyword = true;
output.push(i);
}
else {
continue;
}
}
}
console.log("lenght is" + output.length);
if (iskeyword == true) {
for (i = 0; i < output.length; i++) {
final.push((title[output[i]]));
console.log(title[output[i]]);
}
}
else {
input = input.substring(1, input.length -1);
final.push(input);
}
getClub(final);
return final;
}
// getClub('IEEE');