-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore.js
344 lines (329 loc) · 15.3 KB
/
datastore.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
const {Datastore} = require('@google-cloud/datastore');
const { get } = require('request');
const datastore = new Datastore();
const TEACHER = "Teacher";
const STUDENT = "Student";
const SUPPLY = "Supply";
function fromDatastore(item){
item.id = item[Datastore.KEY].id;
return item;
}
function editTeacher(target_teacher){
var edited_teacher = {};
edited_teacher.teacher_autho = target_teacher.teacher_autho;
edited_teacher.students = target_teacher.students;
edited_teacher.email = target_teacher.email;
edited_teacher.supplies_needed = target_teacher.supplies_needed;
edited_teacher.name = target_teacher.name;
return edited_teacher;
}
function get_teacher_autho(teacher_autho){
console.log("inside get_teacher_autho - teacher_autho=" + teacher_autho);
return module.exports.get_teachers()
.then((teachers) => {
for(var i=0; i<teachers.length; i++){
if(teachers[i].teacher_autho === teacher_autho){
console.log("found teacher " + teachers[i]);
return teachers[i];
}
}
return null;
})
}
module.exports = {
post_teacher: function post_teacher(teacher_autho, name, email){
var key = datastore.key(TEACHER);
const new_teacher = {"teacher_autho": teacher_autho, "name": name, "email": email, "supplies_needed": [], "students": []};
return datastore.save({"key":key, "data":new_teacher})
.then(() => {
return datastore.get(key)
.then((entity) => {
return fromDatastore(entity[0]);
});
})
},
// display teachers
get_teachers: function get_teachers(){
console.log("inside get_all_teachers");
var q = datastore.createQuery(TEACHER);
return datastore.runQuery(q)
.then( (entities) => {
return entities[0].map(fromDatastore);
});
},
get_teacher: function get_teacher(teacher_id){
console.log("inside get_teacher - teacher_id=" + teacher_id);
const key = datastore.key([TEACHER, parseInt(teacher_id, 10)]);
return datastore.get(key).then((entity) => {
if (entity[0] === undefined || entity[0] === null) {
return null;
} else {
var teacher = fromDatastore(entity[0]);
return teacher;
}
});
},
delete_teacher_test: function delete_teacher_test(teacher_id){
console.log("inside delete_teacher id=" + teacher_id);
const key = datastore.key([TEACHER, parseInt(teacher_id,10)]);
return datastore.delete(key);
},
post_supply: function post_supply(teacher_autho, description, num_needed){
var supply_ret;
console.log("inside function post_supply");
// check if user with teacher_autho has an account
return get_teacher_autho(teacher_autho)
.then ((target_teacher) => {
if(target_teacher == null){
return null;
}
var key = datastore.key(SUPPLY);
const new_supply = {"teacher_autho": teacher_autho, "description": description, "num_needed": num_needed, "num_supplied": 0, "students": []};
return datastore.save({ "key": key, "data": new_supply })
.then(() => { return datastore.get(key)
.then((supply) => {
supply = fromDatastore(supply[0]);
supply_ret = supply;
// add supply to teacher's collection of supplies
target_teacher.supplies_needed.push({supply_id: supply.id});
const teacher_id = target_teacher.id;
const edited_teacher = editTeacher(target_teacher);
const teacher_key = datastore.key([TEACHER, parseInt(teacher_id,10)]);
datastore.save({"key": teacher_key, "data": edited_teacher})
})
.then(() => {
console.log("supply after save teacher " + supply_ret);
return supply_ret;
});
});
});
},
update_supply: function update_supply(id, teacher_autho, description, num_needed, num_supplied, students){
const key = datastore.key([SUPPLY, parseInt(id, 10)]);
const supply = { "teacher_autho": teacher_autho, "description": description,"num_needed":num_needed, "num_supplied":num_supplied, "students": students};
return datastore.save({ "key": key, "data": supply })
.then(() => { return datastore.get(key)
.then((entity) => {
return fromDatastore(entity[0]);
});
})
},
get_supplies: function get_supplies(req, teacher_autho){
console.log("inside get_supplies()");
var q = datastore.createQuery(SUPPLY).filter('teacher_autho', '=', teacher_autho).limit(5);
var results = {};
if(Object.keys(req.query).includes("cursor")){
q = q.start(req.query.cursor);
}
return datastore.runQuery(q)
.then( (entities) => {
results.supplies = entities[0];//.map(fromDatastore); // array of supplies
if(entities[1].moreResults !== Datastore.NO_MORE_RESULTS ){
results.next = req.protocol + "://" + req.get("host") + req.baseUrl + "/supplies?cursor=" + entities[1].endCursor;
}
results.supplies.map(fromDatastore);
var q_total = datastore.createQuery(SUPPLY).filter('teacher_autho', '=', teacher_autho);
return datastore.runQuery(q_total)
.then((entities2) => {
results.total = entities2[0].length;
return results;
});
});
},
delete_supply: function delete_supply(supply_id){
console.log("inside delete_supply id=" + supply_id);
return module.exports.get_supply(supply_id)
.then((supply) => {
const key = datastore.key([SUPPLY, parseInt(supply_id,10)]);
// delete supply from teacher
const teacher_autho = supply.teacher_autho;
return get_teacher_autho(teacher_autho)
.then((target_teacher) => {
var supplies = target_teacher.supplies_needed;
var index = -1;
for(var i=0; i<supplies.length; i++){
if(supplies[i].supply_id === supply_id){
index = i;
}
}
supplies.splice(index,1);
const teacher_id = target_teacher.id;
const edited_teacher = editTeacher(target_teacher);
const teacher_key = datastore.key([TEACHER, parseInt(teacher_id,10)]);
datastore.save({"key": teacher_key, "data": edited_teacher})
return datastore.delete(key);
});
});
},
get_supply: function get_supply(supply_id){
console.log("inside get_supply - supply_id=" + supply_id);
const key = datastore.key([SUPPLY, parseInt(supply_id, 10)]);
return datastore.get(key).then((entity) => {
if (entity[0] === undefined || entity[0] === null) {
return null;
} else {
var supply = fromDatastore(entity[0]);
console.log(supply);
return supply;
}
});
},
post_student: function post_student(teacher_autho, first_name, last_name, class_period){
var student_ret;
console.log("inside function post_student");
// check if user with teacher_autho has an account
return get_teacher_autho(teacher_autho)
.then((target_teacher) => {
if(target_teacher == null){
return null;
}
var key = datastore.key(STUDENT);
const new_student = {"teacher_autho": teacher_autho, "first_name": first_name, "last_name": last_name, "class_period": class_period, "supply_donated": null };
return datastore.save({ "key": key, "data": new_student })
.then(() => {
return datastore.get(key)
.then((student) => {
student = fromDatastore(student[0]);
student_ret = student;
// add student to teacher's collection of students
target_teacher.students.push({student_id: student.id});
const teacher_id = target_teacher.id;
const edited_teacher = editTeacher(target_teacher);
const teacher_key = datastore.key([TEACHER, parseInt(teacher_id,10)]);
datastore.save({"key": teacher_key, "data": edited_teacher})
})
.then(() => {
console.log("student after save teacher " + student_ret);
return student_ret;
});
});
});
},
update_student: function update_student(id, teacher_autho, first_name, last_name, class_period, supply){
console.log("before key " + id);
const key = datastore.key([STUDENT, parseInt(id, 10)]);
const student = { "teacher_autho": teacher_autho, "first_name": first_name,"last_name":last_name, "class_period":class_period, "supply_donated": supply};
return datastore.save({ "key": key, "data": student })
.then(() => { return datastore.get(key)
.then((entity) => {
return fromDatastore(entity[0]);
});
})
},
delete_student: function delete_student(student_id){
console.log("inside delete_student id=" + student_id);
return module.exports.get_student(student_id)
.then((student) => {
const key = datastore.key([STUDENT, parseInt(student_id,10)]);
// delete supply from teacher
const teacher_autho = student.teacher_autho;
return get_teacher_autho(teacher_autho)
.then((target_teacher) => {
var students = target_teacher.students;
var index = -1;
for(var i=0; i<students.length; i++){
if(students[i].student_id === student_id){
index = i;
}
}
students.splice(index,1);
const teacher_id = target_teacher.id;
const edited_teacher = editTeacher(target_teacher);
const teacher_key = datastore.key([TEACHER, parseInt(teacher_id,10)]);
datastore.save({"key": teacher_key, "data": edited_teacher})
return datastore.delete(key);
});
});
},
get_student: function get_student(student_id){
console.log("inside get_student - student_id=" + student_id);
const key = datastore.key([STUDENT, parseInt(student_id, 10)]);
return datastore.get(key).then((entity) => {
if (entity[0] === undefined || entity[0] === null) {
return null;
} else {
var student = fromDatastore(entity[0]);
console.log(student);
return student;
}
});
},
get_students: function get_students(req, teacher_autho){
console.log("inside get_students");
console.log(teacher_autho);
var q = datastore.createQuery(STUDENT).filter('teacher_autho', '=', teacher_autho).limit(5);
var results = {};
if(Object.keys(req.query).includes("cursor")){
q = q.start(req.query.cursor);
}
return datastore.runQuery(q)
.then( (entities) => {
console.log("entitities " + entities);
results.students = entities[0];
if(entities[1].moreResults !== Datastore.NO_MORE_RESULTS ){
results.next = req.protocol + "://" + req.get("host") + req.baseUrl + "/students?cursor=" + entities[1].endCursor;
}
results.students.map(fromDatastore);
var q_total = datastore.createQuery(STUDENT).filter('teacher_autho', '=', teacher_autho)
return datastore.runQuery(q_total)
.then((entities2) => {
results.total = entities2[0].length;
return results;
});
});
},
assign_supply_to_student: function assign_supply_to_student(supply_id, student_id){
return module.exports.get_student(student_id)
.then( (student) => {
const key = datastore.key([STUDENT, parseInt(student_id,10)]);
const supply_donated = {"supply_id": supply_id};
student.supply_donated = supply_donated;
return datastore.save({"key":key, "data":student});
});
},
add_student_to_supply: function add_student_to_supply(student_id, supply_id){
return module.exports.get_supply(supply_id)
.then( (supply) => {
const key = datastore.key([SUPPLY, parseInt(supply_id,10)]);
const student = {"student_id": student_id};
supply.students.push(student);
supply.num_supplied = supply.num_supplied + 1;
return datastore.save({"key":key, "data":supply});
});
},
delete_supply_from_student: function delete_supply_from_student(student_id){
console.log("inside delete_supply_from_student ");
return module.exports.get_student(student_id)
.then( (student) => {
student.supply_donated = null;
const key = datastore.key([STUDENT, parseInt(student_id,10)]);
return datastore.save({"key":key, "data": student});
});
},
delete_student_from_supply: function delete_student_from_supply(student_id, supply_id){
console.log("inside delete_student_from_supply ");
return module.exports.get_supply(supply_id)
.then( (supply) => {
var student_to_delete_index = -1;
for(var i=0; supply != null && i<supply.students.length; i++){
if(supply.students[i].student_id === student_id){
student_to_delete_index = i;
console.log("student_to_delete_index " + i);
}
}
supply.students.splice(student_to_delete_index,1);
supply.num_supplied = supply.num_supplied - 1;
console.log("supply.num_supplied " + supply.num_supplied);
const key = datastore.key([SUPPLY, parseInt(supply_id,10)]);
return datastore.save({"key":key, "data":supply});
});
},
delete_supply_from_all_students: function delete_supply_from_all_students(students){
for(var i=0; i<students.length; i++){
// remove supply from students because supply will be updated completely and num_supplied set to 0
var student_id = students[i].student_id;
console.log("inside for loop student_id " + student_id);
module.exports.delete_supply_from_student(student_id);
}
}
}