-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopulateScript.js
94 lines (87 loc) · 2.4 KB
/
PopulateScript.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
const timeSlots = [
{"time": "7:15am - 8:45am"},
{"time": "9:00am - 10:30am"},
{"time": "10:45am - 12:15pm"},
{"time": "12:30pm - 2:00pm"},
{"time": "2:15pm - 3:45pm"},
{"time": "4:00pm - 5:30pm"},
{"time": "7:30pm - 9:00pm"}
];
async function createSession(session) {
let res = await fetch('http://localhost:8090/api/sessions', {
method: 'POST',
body: JSON.stringify(session),
headers: {
"Content-Type": 'application/json'
}
});
const data = await res.json();
return data;
}
async function createDay(day){
let res = await fetch('http://localhost:8090/api/days', {
method: 'POST',
body: JSON.stringify(day),
headers: {
"Content-Type": 'application/json'
}
});
const data = await res.json();
return data;
}
function determineDay(Tempday){
let day;
switch (Tempday) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
return(day);
}
async function PopulateDatabase(){
let currentDate = new Date().toJSON().slice(0, 10).split('-');
let currentDay = new Date().getDay();
for (let temp = 0; temp < 5; temp++) {
let tempDay = determineDay(currentDay);
let tempDate = currentDate[1] + '-' + currentDate[2] + '-' + currentDate[0];
let newDay = {
"weekday": tempDay,
"date" : tempDate
}
currentDate[2]++;
currentDay++;
currentDay = currentDay%7;
let dayID = await createDay(newDay);
for (const sessions of timeSlots){
let tempSessionTime = sessions.time.split('-');
let tempNewSession = {
"DayId": dayID.id,
"start":tempSessionTime[0],
"end": tempSessionTime[1],
"description": "TEMP DESC",
"title": tempDay,
"chair": "Temporary Chair",
"room": "Temporary Room"
}
await createSession(tempNewSession);
}
}
}
PopulateDatabase();