[Pharo] Personal fitness tracker web app.
Added generic insert:, delete:, and update: methods to the MFTSQLite3Database.
src/MyFitnessTracker/MFTSQLite3Database.class.st
@@ -185,11 +185,10 @@
185
185
{ #category : #'as yet unclassified' }
186
186
MFTSQLite3Database >> allUserWeights [
187
187
188
Removed:
^ (self class defaultConnection
189
Removed:
open;
190
Removed:
execute: 'SELECT weights.*
188
Added:
^ (connection
189
Added:
execute: 'SELECT *
191
190
FROM weights
192
Removed:
WHERE weights.user_id = ?'
191
Added:
WHERE user_id = ?'
193
192
value: userId) rows collect: [ :each |
194
193
MFTWeight newFromDictionary: each asDictionary ]
195
194
]
@@ -197,51 +196,115 @@
197
196
{ #category : #'as yet unclassified' }
198
197
MFTSQLite3Database >> allUserWorkouts [
199
198
200
Removed:
^ (self class defaultConnection
201
Removed:
open;
202
Removed:
execute: 'SELECT workouts.*
199
Added:
^ (connection
200
Added:
execute: 'SELECT *
203
201
FROM workouts
204
Removed:
WHERE workouts.user_id = ?'
202
Added:
WHERE user_id = ?'
205
203
value: userId) rows collect: [ :each |
206
204
MFTWorkout newFromDictionary: each asDictionary ]
207
205
]
208
206
209
207
{ #category : #'as yet unclassified' }
208
Added:
MFTSQLite3Database >> delete: anObject [
209
Added:
"Delete an object from the database, using the appropriate method."
210
Added:
211
Added:
| methods appropriateMethod |
212
Added:
methods := Dictionary newFrom: {
213
Added:
(MFTWeight -> #deleteWeight:).
214
Added:
(MFTWorkout -> #deleteWorkout:) }.
215
Added:
appropriateMethod := methods at: anObject class ifAbsent: [
216
Added:
^ self error:
217
Added:
('No appropriate {1} method for deleting {2}'
218
Added:
format: {
219
Added:
self.
220
Added:
anObject }) ].
221
Added:
self perform: appropriateMethod with: anObject
222
Added:
]
223
Added:
224
Added:
{ #category : #'as yet unclassified' }
225
Added:
MFTSQLite3Database >> deleteExercisesForWorkoutId: id [
226
Added:
227
Added:
connection
228
Added:
execute: 'DELETE
229
Added:
FROM exercises
230
Added:
WHERE workout_id = ?'
231
Added:
value: id
232
Added:
]
233
Added:
234
Added:
{ #category : #'as yet unclassified' }
210
235
MFTSQLite3Database >> deleteWeight: aMFTWeight [
211
236
212
Removed:
| weightId |
213
Removed:
weightId := (self
214
Removed:
select: 'id'
215
Removed:
from: 'weights'
216
Removed:
where:
217
Removed:
('weights.date = ''{1}'' AND weights.value = ''{2}'''
218
Removed:
format: {
219
Removed:
aMFTWeight date yyyymmdd.
220
Removed:
aMFTWeight value })) onlyValue.
221
Removed:
weightId ifNil: [
222
Removed:
self error:
223
Removed:
'Blendux: attempted to delete a weight from database with no primary key.' ].
224
237
connection
225
Removed:
execute: 'DELETE FROM weights
226
Removed:
WHERE weights.id = ?'
227
Removed:
value: weightId
238
Added:
execute: 'DELETE
239
Added:
FROM weights
240
Added:
WHERE user_id = ?
241
Added:
AND date = ?
242
Added:
AND value = ?'
243
Added:
with: {
244
Added:
userId.
245
Added:
aMFTWeight date yyyymmdd.
246
Added:
aMFTWeight value }
228
247
]
229
248
230
249
{ #category : #'as yet unclassified' }
250
Added:
MFTSQLite3Database >> deleteWorkout: aMFTWorkout [
251
Added:
252
Added:
connection
253
Added:
execute: 'DELETE
254
Added:
FROM workouts
255
Added:
WHERE user_id = ?
256
Added:
AND date = ?'
257
Added:
with: {
258
Added:
userId.
259
Added:
aMFTWorkout date yyyymmdd }
260
Added:
]
261
Added:
262
Added:
{ #category : #private }
263
Added:
MFTSQLite3Database >> insert: anObject [
264
Added:
"Insert an object in the database, using the appropriate method."
265
Added:
266
Added:
| methods appropriateMethod |
267
Added:
methods := Dictionary newFrom: {
268
Added:
(MFTWeight -> #insertWeight:).
269
Added:
(MFTWorkout -> #insertWorkout:) }.
270
Added:
appropriateMethod := methods at: anObject class ifAbsent: [
271
Added:
^ self error:
272
Added:
('No appropriate {1} method for inserting {2}'
273
Added:
format: {
274
Added:
self.
275
Added:
anObject }) ].
276
Added:
277
Added:
self perform: appropriateMethod with: anObject
278
Added:
]
279
Added:
280
Added:
{ #category : #'as yet unclassified' }
281
Added:
MFTSQLite3Database >> insertExercise: aMFTExercise forWorkoutId: workoutId [
282
Added:
283
Added:
connection
284
Added:
execute: 'INSERT INTO exercises(workout_id, english_name, sets)
285
Added:
VALUES( ?, ?, ?)'
286
Added:
with: {
287
Added:
workoutId.
288
Added:
aMFTExercise englishName.
289
Added:
aMFTExercise sets asJson }
290
Added:
]
291
Added:
292
Added:
{ #category : #'as yet unclassified' }
231
293
MFTSQLite3Database >> insertWeight: aMFTWeight [
232
294
233
295
connection
234
296
execute: 'INSERT INTO weights(user_id, date, value)
235
Removed:
VALUES( ?, ?, ?)'
297
Added:
VALUES(?, ?, ?)'
236
298
with: {
237
Removed:
1.
238
Removed:
aMFTWeight date.
299
Added:
userId.
300
Added:
aMFTWeight date yyyymmdd.
239
301
aMFTWeight value }
240
302
]
241
303
242
304
{ #category : #'as yet unclassified' }
243
305
MFTSQLite3Database >> insertWorkout: aMFTWorkout [
244
306
307
Added:
| workoutId |
245
308
connection
246
309
execute:
247
310
'INSERT INTO workouts(user_id, date, primary_muscle, secondary_muscles)
@@ -250,7 +313,22 @@
250
313
userId.
251
314
aMFTWorkout date yyyymmdd.
252
315
aMFTWorkout primaryMuscle englishName.
253
Removed:
(aMFTWorkout secondaryMuscles collect: #englishName) asCommaString }
316
Added:
(aMFTWorkout secondaryMuscles collect: #englishName) asCommaString }.
317
Added:
workoutId := (connection
318
Added:
execute: 'SELECT id
319
Added:
FROM workouts
320
Added:
WHERE user_id = ?
321
Added:
AND date = ?
322
Added:
AND primary_muscle = ?
323
Added:
AND secondary_muscles = ?'
324
Added:
with: {
325
Added:
userId.
326
Added:
aMFTWorkout date yyyymmdd.
327
Added:
aMFTWorkout primaryMuscle englishName.
328
Added:
(aMFTWorkout secondaryMuscles collect: #englishName)
329
Added:
asCommaString }) onlyValue.
330
Added:
aMFTWorkout exercises do: [ :exercise |
331
Added:
self insertExercise: exercise forWorkoutId: workoutId ]
254
332
]
255
333
256
334
{ #category : #'as yet unclassified' }
@@ -259,72 +337,102 @@
259
337
self user: aMFTUser.
260
338
connection
261
339
execute: 'UPDATE users
262
Removed:
SET last_login_date_time = ?
263
Removed:
WHERE username = ?'
340
Added:
SET last_login_date_time = ?
341
Added:
WHERE id = ?'
264
342
with: {
265
Removed:
aDateAndTime.
266
Removed:
user username }
343
Added:
aDateAndTime asString.
344
Added:
userId }
267
345
]
268
346
269
347
{ #category : #'as yet unclassified' }
270
Removed:
MFTSQLite3Database >> select: attributes from: aTable [
348
Added:
MFTSQLite3Database >> update: oldObject with: newObject [
349
Added:
"Update an object in the database, using the appropriate method."
271
350
272
Removed:
^ connection execute: ({
273
Removed:
'SELECT'.
274
Removed:
attributes.
275
Removed:
'FROM'.
276
Removed:
aTable asString } joinUsing: Character space)
351
Added:
| methods appropriateMethod |
352
Added:
methods := Dictionary newFrom: {
353
Added:
(MFTWeight -> #updateWeight:with:).
354
Added:
(MFTWorkout -> #updateWorkout:with:).
355
Added:
(MFTUser -> #updateUser:with:) }.
356
Added:
appropriateMethod := methods at: oldObject class ifAbsent: [
357
Added:
^ self error:
358
Added:
('No appropriate {1} method for updating {2}'
359
Added:
format: {
360
Added:
self.
361
Added:
oldObject }) ].
362
Added:
self perform: appropriateMethod with: oldObject with: newObject
277
363
]
278
364
279
365
{ #category : #'as yet unclassified' }
280
Removed:
MFTSQLite3Database >> select: attributes from: aTable where: aCondition [
366
Added:
MFTSQLite3Database >> updateUserWith: newMFTUser [
281
367
282
Removed:
^ connection execute: ({
283
Removed:
'SELECT'.
284
Removed:
attributes.
285
Removed:
'FROM'.
286
Removed:
aTable asString.
287
Removed:
'WHERE'.
288
Removed:
aCondition } joinUsing: Character space)
289
Removed:
]
290
Removed:
291
Removed:
{ #category : #'as yet unclassified' }
292
Removed:
MFTSQLite3Database >> updateProfile: aMFTUser [
293
Removed:
294
368
connection
295
369
execute: 'UPDATE users
296
Removed:
SET date = ?,
297
Removed:
value = ?
298
Removed:
WHERE user_id = ?'
299
Removed:
with: { }
370
Added:
SET username = ?,
371
Added:
password_hash = ?,
372
Added:
date_of_birth = ?,
373
Added:
real_first_name = ?,
374
Added:
real_last_name = ?
375
Added:
WHERE id = ?'
376
Added:
with: {
377
Added:
newMFTUser username.
378
Added:
newMFTUser passwordHash.
379
Added:
newMFTUser dateOfBirth.
380
Added:
newMFTUser realFirstName.
381
Added:
newMFTUser realLastName.
382
Added:
userId }
300
383
]
301
384
302
385
{ #category : #'as yet unclassified' }
303
Removed:
MFTSQLite3Database >> updateWeight: aMFTWeight [
386
Added:
MFTSQLite3Database >> updateWeight: oldMFTWeight with: newMFTWeight [
304
387
305
388
connection
306
389
execute: 'UPDATE weights
307
Removed:
SET date = ?,
308
Removed:
value = ?
309
Removed:
WHERE user_id = ?'
390
Added:
SET date = ?,
391
Added:
value = ?
392
Added:
WHERE user_id = ?
393
Added:
AND date = ?
394
Added:
AND value = ?'
310
395
with: {
311
Removed:
aMFTWeight date yyyymmdd.
312
Removed:
aMFTWeight value.
313
Removed:
1 }
396
Added:
newMFTWeight date yyyymmdd.
397
Added:
newMFTWeight value.
398
Added:
userId.
399
Added:
oldMFTWeight date yyyymmdd.
400
Added:
oldMFTWeight value }
314
401
]
315
402
316
403
{ #category : #'as yet unclassified' }
317
Removed:
MFTSQLite3Database >> updateWeight: aMFTWeight for: aMFTUser [
404
Added:
MFTSQLite3Database >> updateWorkout: oldMFTWorkout with: newMFTWorkout [
405
Added:
"We first obtain the workoutId to later replace the associated workout's exercises."
318
406
407
Added:
| workoutId |
408
Added:
workoutId := (connection
409
Added:
execute: 'SELECT id
410
Added:
FROM workouts
411
Added:
WHERE user_id = ?
412
Added:
AND date = ?
413
Added:
AND primary_muscle = ?
414
Added:
AND secondary_muscles = ?'
415
Added:
with: {
416
Added:
userId.
417
Added:
oldMFTWorkout date yyyymmdd.
418
Added:
oldMFTWorkout primaryMuscle englishName.
419
Added:
(oldMFTWorkout secondaryMuscles collect:
420
Added:
#englishName) asCommaString }) onlyValue.
319
421
connection
320
Removed:
execute: 'UPDATE weights
321
Removed:
SET date = ?,
322
Removed:
value = ?
323
Removed:
WHERE user_id'
422
Added:
execute: 'UPDATE workouts
423
Added:
SET date = ?,
424
Added:
primary_muscle = ?,
425
Added:
secondary_muscles = ?
426
Added:
WHERE id = ?'
324
427
with: {
325
Removed:
aMFTWeight date yyyymmdd.
326
Removed:
aMFTWeight value.
327
Removed:
aMFTUser username }
428
Added:
newMFTWorkout date yyyymmdd.
429
Added:
newMFTWorkout primaryMuscle englishName.
430
Added:
(newMFTWorkout secondaryMuscles collect: #englishName)
431
Added:
asCommaString.
432
Added:
workoutId }.
433
Added:
self deleteExercisesForWorkoutId: workoutId.
434
Added:
newMFTWorkout exercises do: [ :exercise |
435
Added:
self insertExercise: exercise forWorkoutId: workoutId ]
328
436
]
329
437
330
438
{ #category : #accessing }
@@ -338,10 +446,13 @@
338
446
339
447
user := aMFTUser.
340
448
userId := (connection
341
Removed:
execute: 'SELECT users.id
449
Added:
execute: 'SELECT id
342
450
FROM users
343
Removed:
WHERE users.username = ?'
344
Removed:
value: 'testMFT') onlyRow asArray first
451
Added:
WHERE username = ?
452
Added:
AND password_hash = ?'
453
Added:
with: {
454
Added:
aMFTUser username.
455
Added:
aMFTUser passwordHash }) onlyValue
345
456
]
346
457
347
458
{ #category : #accessing }