-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.js
284 lines (262 loc) · 10.8 KB
/
fixtures.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
var SQLite = require('react-native-sqlite-storage')
var main = function () {
var testing = true;
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
populateDatabase(db, testing); // adds the Exercises, bool to say if testing
// getWorkout().then((d) => {
// console.log("ddd", d);
// return insertWorkout(10, "Pushups", 100, 5, 1);
// }).then(() => {
// return getWorkout();
// }).then((d) => {
// console.log("ddd prime", d);
// }).catch((err) => {console.log("ddd err", err)});
insertCurrent("Situps", 222).then(() => {
console.log("gc", getCurrent());
});
}
var successcb = function () {
console.log("successcb");
}
var errorcb = function () {
console.log("errorcb");
}
var populateDatabase = function (db, testing) {
// Drop the tables
var dropStmts;
if(testing) {
console.log("Testing is true, dropping all tables");
db.executeSql("DROP TABLE IF EXISTS Workout", [], successcb, errorcb);
db.executeSql("DROP TABLE IF EXISTS Current", [], successcb, errorcb);
}
db.executeSql("DROP TABLE IF EXISTS Exercise", [], successcb, errorcb);
// Create the tables
db.executeSql("CREATE TABLE IF NOT EXISTS Exercise (name TEXT PRIMARY KEY NOT NULL, startCount INT NOT NULL)", [], successcb, errorcb);
db.executeSql("CREATE TABLE IF NOT EXISTS Workout (id INT PRIMARY KEY NOT NULL, name TEXT NOT NULL, count INT NOT NULL, difficulty INT NOT NULL, time DATETIME NOT NULL, completed BOOL NOT NULL, FOREIGN KEY (name) REFERENCES Exercise (name))", [], successcb, errorcb);
db.executeSql("CREATE TABLE IF NOT EXISTS Current (id INT PRIMARY KEY NOT NULL, name TEXT NOT NULL, count INT NOT NULL, time DATETIME NOT NULL, FOREIGN KEY (name) REFERENCES Exercise (name))", [], successcb, errorcb);
// Insert values
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Pushups", 10);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Situps", 20);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Diamond Pushups", 15);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Basic Squats", 30);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Broad Jump", 40);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Calf Raises", 50);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Tricep Dip", 20);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Plank", 30);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Mountain Climbers", 30);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Bear Crawls", 50);', []);
db.executeSql('INSERT INTO Exercise (name, startCount) VALUES ("Basic Burpees", 25);', []);
if(testing) addExampleData(db);
}
var addExampleData = function(db) {
// Insert values
db.executeSql('INSERT INTO Workout (id, name, count, difficulty, time, completed) VALUES (1, "Pushups", 10, 1, "2004-01-01 02:34:56", 1);', []);
db.executeSql('INSERT INTO Workout (id, name, count, difficulty, time, completed) VALUES (2, "Pushups", 11, 1, "2004-01-02 02:34:56", 1);', []);
db.executeSql('INSERT INTO Workout (id, name, count, difficulty, time, completed) VALUES (3, "Situps", 20, 3, "2004-01-02 05:34:56", 1);', []);
db.executeSql('INSERT INTO Workout (id, name, count, difficulty, time, completed) VALUES (4, "Diamond Pushups", 15, 5, "2004-01-02 05:34:56", 0);', []);
db.executeSql('INSERT INTO Workout (id, name, count, difficulty, time, completed) VALUES (5, "Pushups", 20, 4, "2004-03-02 05:34:56", 1);', []);
db.executeSql('INSERT INTO Current (id, name, count, time) VALUES (0, "Pushups", 21, "2004-03-02 05:34:56");', []);
}
export function countExerciseFailure() {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('SELECT count(*) FROM Workout where completed = 0', [],
function (data) {
console.log("select Current succeded");
if(data && data["rows"] && data["rows"]["length"]) {
max = undefined;
for(var i=0; i < data["rows"]["length"]; i++) {
if(max === undefined)
max = data["rows"]["item"](i)["count(*)"];
else
max = max > data["rows"]["item"](i) ? max : data["rows"]["item"](i)["count(*)"];
}
resolve(max);
}
},
function (error) {
reject("Exercise count failures failed.");
});
});
}
export function countExerciseComplete() {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('SELECT count(*) FROM Workout where completed = 1', [],
function (data) {
console.log("select Current succeded");
if(data && data["rows"] && data["rows"]["length"]) {
max = undefined;
for(var i=0; i < data["rows"]["length"]; i++) {
if(max === undefined)
max = data["rows"]["item"](i)["count(*)"];
else
max = max > data["rows"]["item"](i) ? max : data["rows"]["item"](i)["count(*)"];
}
resolve(max);
}
},
function (error) {
reject("Exercise count complete failed.");
});
});
}
export function randomExercise() {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('SELECT * FROM Exercise', [],
function (data) {
e = undefined;
if(data && data["rows"] && data["rows"]["length"]) {
r = Math.floor(Math.random() * (data["rows"]["length"])) // 0 to length-1 inclusive
e = data["rows"]["item"](r);
}
resolve(e);
},
function (error) {
reject("Exercise random selection failed.");
});
});
}
export function getCurrent() {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('SELECT * FROM Current', [],
function (data) {
cs = [];
if(data && data["rows"] && data["rows"]["length"]) {
for(var i=0; i < data["rows"]["length"]; i++) {
cs.push(data["rows"]["item"](i));
}
}
resolve(cs);
},
function (error) {
reject("getCurrent failed");
});
});
}
export function removeCurrentById(id) {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('DELETE FROM Current where id = ?', [id],
function (data) {
resolve();
},
function (error) {
reject("removeCurrentById failed");
});
});
}
export function getWorkout() {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('SELECT * FROM Workout', [],
function (data) {
ws = [];
if(data && data["rows"] && data["rows"]["length"]) {
for(var i=0; i < data["rows"]["length"]; i++) {
ws.push(data["rows"]["item"](i));
}
}
resolve(ws);
},
function (error) {
reject("getWorkout failed");
});
});
}
var getMaxCurrentId = function() {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('SELECT Max(id) FROM Current', [],
function (data) {
var max = undefined;
for(var i=0; i < data["rows"]["length"]; i++) {
if(max === undefined)
max = data["rows"]["item"](i)["Max(id)"];
else
max = max > data["rows"]["item"](i) ? max : data["rows"]["item"](i)["Max(id)"];
}
resolve(max);
},
function (error) {
reject("getMaxCurrentId failed");
});
});
}
var insertCurrent = function(name, count) {
return new Promise(function(resolve, reject) {
getMaxCurrentId().then((maxID) => {
var currentId = maxID + 1;
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
var n = 15; // num of minutes to advance the time // TODO rand time
db.executeSql('INSERT INTO Current (id, name, count, time) VALUES (?, ?, ?, datetime(\'now\', \'localtime\', \'+' + n + ' seconds\'))',
[currentId, name, count],
function () {
resolve(currentId);
},
function (error) {
console.log('insertCurrent failed', error);
reject("insertCurrent failed");
});
}).catch((err) => {
console.log("couldn't getMaxCurrentId in insertCurrent", err);
reject("couldn't getMaxCurrentId in insertCurrent");
});
});
}
export function randomExerciseAndInsertCurrent() {
return new Promise(function(resolve, reject) {
randomExercise().then((ex) => {
var sc = ex.startCount;
var count = Math.floor(((Math.random() - 0.5) * sc / 4) + sc); // TODO better random count
return insertCurrent(ex.name, count);
}).then((currentId) => {
resolve(currentId);
}) .catch((err) => {
console.log("something went wrong in randomExerciseAndInsertCurrent", err);
reject(err);
});
});
}
var getMaxWorkoutId = function() {
return new Promise(function(resolve, reject) {
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('SELECT Max(id) FROM Workout', [],
function (data) {
var max = undefined;
for(var i=0; i < data["rows"]["length"]; i++) {
if(max === undefined)
max = data["rows"]["item"](i)["Max(id)"];
else
max = max > data["rows"]["item"](i) ? max : data["rows"]["item"](i)["Max(id)"];
}
resolve(max);
},
function (error) {
reject("getMaxWorkoutId failed");
});
});
}
export function insertWorkout(name, count, difficulty, completed) {
return new Promise(function(resolve, reject) {
getMaxWorkoutId().then((maxID) => {
var workoutId = maxID + 1;
var db = SQLite.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
db.executeSql('INSERT INTO Workout (id, name, count, time, difficulty, completed) VALUES (?, ?, ?, datetime(\'now\', \'localtime\'), ?, ?)',
[workoutId, name, count, difficulty, completed],
function () {
resolve(workoutId);
},
function (error) {
console.log('insertWorkout failed', error);
reject("insertWorkout failed");
});
}).catch((err) => {
console.log("couldn't getMaxWorkoutId in insertWorkout", err);
reject("couldn't getMaxWorkoutId in insertWorkout");
});
});
}
main();