[Pharo] Personal fitness tracker web app.
Make sure to properly reuse session database connection!
Previously, a new temporarry connection was created, opened, then closed. Not optimal.
Changed files
src/MyFitnessTracker/MFTSQLite3Database.class.st
@@ -2,8 +2,7 @@
2
2
#name : #MFTSQLite3Database,
3
3
#superclass : #SQLite3Database,
4
4
#instVars : [
5
Removed:
'user',
6
Removed:
'userId'
5
Added:
'user'
7
6
],
8
7
#classInstVars : [
9
8
'defaultLocation',
@@ -79,44 +78,53 @@
79
78
]
80
79
81
80
{ #category : #accessing }
82
Removed:
MFTSQLite3Database class >> getAllSetsForUserId: anInteger [
81
Added:
MFTSQLite3Database class >> getAllSetsForUser: aMFTUser connection: openConnection [
83
82
84
Removed:
| connection allSetRows sets |
85
Removed:
connection := self defaultConnection open.
86
Removed:
allSetRows := (connection
83
Added:
| allSetRows sets |
84
Added:
allSetRows := (openConnection
87
85
execute:
88
86
'SELECT s.* FROM Sets s JOIN Workouts w ON s.workout_id = w.id WHERE w.user_id = ?'
89
Removed:
value: anInteger) rows.
87
Added:
value: aMFTUser id) rows.
90
88
sets := allSetRows collect: [ :row |
91
89
MFTSet newFromDictionary: row asDictionary ].
92
90
^ sets
93
91
]
94
92
95
93
{ #category : #accessing }
96
Removed:
MFTSQLite3Database class >> getAllWorkoutsForTestUser [
94
Added:
MFTSQLite3Database class >> getAllWeightsForUser: aMFTUser connection: openConnection [
97
95
98
Removed:
^ self getAllWorkoutsForUserId: 1
96
Added:
| allWeightRows weights |
97
Added:
allWeightRows := (openConnection
98
Added:
execute: 'SELECT * FROM weights WHERE user_id = ?'
99
Added:
value: aMFTUser id) rows.
100
Added:
weights := allWeightRows collect: [ :row |
101
Added:
MFTWeight newFromDictionary: row asDictionary ].
102
Added:
^ weights
99
103
]
100
104
101
105
{ #category : #accessing }
102
Removed:
MFTSQLite3Database class >> getAllWorkoutsForUser: aMFTUser [
106
Added:
MFTSQLite3Database class >> getAllWorkoutsForTestUser [
103
107
104
Removed:
^ self getAllWorkoutsForUserId: aMFTUser id
108
Added:
| connection workouts |
109
Added:
connection := self defaultConnection open.
110
Added:
workouts := self
111
Added:
getAllWorkoutsForUser: testUser
112
Added:
connection: connection.
113
Added:
connection close.
114
Added:
^ workouts
105
115
]
106
116
107
117
{ #category : #accessing }
108
Removed:
MFTSQLite3Database class >> getAllWorkoutsForUserId: anInteger [
118
Added:
MFTSQLite3Database class >> getAllWorkoutsForUser: aMFTUser connection: openConnection [
109
119
110
Removed:
| connection allWorkoutRows workouts sets |
111
Removed:
connection := self defaultConnection open.
112
Removed:
connection beginTransaction.
113
Removed:
allWorkoutRows := (connection
120
Added:
| allWorkoutRows workouts sets |
121
Added:
allWorkoutRows := (openConnection
114
122
execute:
115
123
'SELECT * FROM Workouts WHERE user_id = ?'
116
Removed:
value: anInteger) rows.
124
Added:
value: aMFTUser id) rows.
125
Added:
sets := self getAllSetsForUser: aMFTUser connection: openConnection.
117
126
workouts := allWorkoutRows collect: [ :row |
118
127
MFTWorkout newFromDictionary: row asDictionary ].
119
Removed:
sets := self getAllSetsForUserId: anInteger.
120
128
workouts do: [ :workout |
121
129
workout sets: (sets select: [ :set | set workoutId = workout id ]) ].
122
130
^ workouts
@@ -177,7 +185,7 @@
177
185
MFTSQLite3Database class >> populatePlaceholderWeights [
178
186
179
187
| db |
180
Removed:
db := self newOnDefaultLocation userId: 1.
188
Added:
db := self newOnDefaultLocation user: testUser.
181
189
db connection open.
182
190
db connection execute: 'DELETE FROM Weights'.
183
191
MFTWeight placeholderCollection do: [ :weight |
@@ -189,7 +197,7 @@
189
197
MFTSQLite3Database class >> populatePlaceholderWorkouts [
190
198
191
199
| db |
192
Removed:
db := self newOnDefaultLocation userId: testUser id.
200
Added:
db := self newOnDefaultLocation user: testUser.
193
201
db connection open.
194
202
db connection
195
203
execute: 'DELETE FROM Workouts';
@@ -233,31 +241,6 @@
233
241
]
234
242
235
243
{ #category : #'as yet unclassified' }
236
Removed:
MFTSQLite3Database >> allUserWeights [
237
Removed:
238
Removed:
^ (connection
239
Removed:
execute: 'SELECT *
240
Removed:
FROM weights
241
Removed:
WHERE user_id = ?'
242
Removed:
value: userId) rows collect: [ :row |
243
Removed:
MFTWeight newFromDictionary: row asDictionary ]
244
Removed:
]
245
Removed:
246
Removed:
{ #category : #'as yet unclassified' }
247
Removed:
MFTSQLite3Database >> allUserWorkouts [
248
Removed:
249
Removed:
| rows workouts |
250
Removed:
rows := (connection
251
Removed:
execute: 'SELECT *
252
Removed:
FROM workouts
253
Removed:
WHERE user_id = ?'
254
Removed:
value: userId) rows.
255
Removed:
workouts := rows collect: [ :each |
256
Removed:
MFTWorkout newFromDictionary: each asDictionary ].
257
Removed:
^ workouts
258
Removed:
]
259
Removed:
260
Removed:
{ #category : #'as yet unclassified' }
261
244
MFTSQLite3Database >> delete: anObject [
262
245
"Delete an object from the database, using the appropriate method."
263
246
@@ -294,7 +277,7 @@
294
277
AND date = ?
295
278
AND value = ?'
296
279
with: {
297
Removed:
userId.
280
Added:
user id.
298
281
aMFTWeight date yyyymmdd.
299
282
aMFTWeight value }
300
283
]
@@ -309,11 +292,23 @@
309
292
WHERE user_id = ?
310
293
AND date = ?'
311
294
with: {
312
Removed:
userId.
295
Added:
user id.
313
296
aMFTWorkout date yyyymmdd }.
314
297
connection commitTransaction
315
298
]
316
299
300
Added:
{ #category : #'as yet unclassified' }
301
Added:
MFTSQLite3Database >> getAllUserWeights [
302
Added:
303
Added:
^ self class getAllWeightsForUser: user connection: connection
304
Added:
]
305
Added:
306
Added:
{ #category : #'as yet unclassified' }
307
Added:
MFTSQLite3Database >> getAllUserWorkouts [
308
Added:
309
Added:
^ self class getAllWorkoutsForUser: user connection: connection
310
Added:
]
311
Added:
317
312
{ #category : #accessing }
318
313
MFTSQLite3Database >> insert: anObject [
319
314
"Insert an object in the database, using the appropriate method."
@@ -364,7 +359,7 @@
364
359
execute: 'INSERT INTO weights(user_id, date, value)
365
360
VALUES(?, ?, ?)'
366
361
with: {
367
Removed:
userId.
362
Added:
user id.
368
363
aMFTWeight date yyyymmdd.
369
364
aMFTWeight value }
370
365
]
@@ -378,7 +373,7 @@
378
373
execute:
379
374
'INSERT INTO Workouts ("user_id", "date", "primary_muscle_group_id") VALUES(?, ?, ?)'
380
375
with: {
381
Removed:
userId.
376
Added:
user id.
382
377
aMFTWorkout date yyyymmdd.
383
378
aMFTWorkout primaryMuscleGroupId }.
384
379
workoutId := (connection
@@ -388,7 +383,7 @@
388
383
AND date = ?
389
384
AND primary_muscle_group_id = ?'
390
385
with: {
391
Removed:
userId.
386
Added:
user id.
392
387
aMFTWorkout date yyyymmdd.
393
388
aMFTWorkout primaryMuscleGroupId }) onlyValue.
394
389
self insertSets: aMFTWorkout sets forWorkoutId: workoutId.
@@ -405,7 +400,7 @@
405
400
WHERE id = ?'
406
401
with: {
407
402
aDateAndTime asString.
408
Removed:
userId }
403
Added:
user id }
409
404
]
410
405
411
406
{ #category : #'as yet unclassified' }
@@ -443,7 +438,7 @@
443
438
newMFTUser dateOfBirth.
444
439
newMFTUser realFirstName.
445
440
newMFTUser realLastName.
446
Removed:
userId }
441
Added:
user id }
447
442
]
448
443
449
444
{ #category : #'as yet unclassified' }
@@ -459,7 +454,7 @@
459
454
with: {
460
455
newMFTWeight date yyyymmdd.
461
456
newMFTWeight value.
462
Removed:
userId.
457
Added:
user id.
463
458
oldMFTWeight date yyyymmdd.
464
459
oldMFTWeight value }
465
460
]
@@ -477,7 +472,7 @@
477
472
AND primary_muscle = ?
478
473
AND secondary_muscles = ?'
479
474
with: {
480
Removed:
userId.
475
Added:
user id.
481
476
oldMFTWorkout date yyyymmdd.
482
477
oldMFTWorkout primaryMuscle englishName.
483
478
(oldMFTWorkout secondaryMuscles collect:
@@ -508,25 +503,5 @@
508
503
{ #category : #accessing }
509
504
MFTSQLite3Database >> user: aMFTUser [
510
505
511
Removed:
user := aMFTUser.
512
Removed:
userId := (connection
513
Removed:
execute: 'SELECT id
514
Removed:
FROM users
515
Removed:
WHERE username = ?
516
Removed:
AND password_hash = ?'
517
Removed:
with: {
518
Removed:
aMFTUser username.
519
Removed:
aMFTUser passwordHash }) onlyValue
520
Removed:
]
521
Removed:
522
Removed:
{ #category : #accessing }
523
Removed:
MFTSQLite3Database >> userId [
524
Removed:
525
Removed:
^ userId
526
Removed:
]
527
Removed:
528
Removed:
{ #category : #accessing }
529
Removed:
MFTSQLite3Database >> userId: anInteger [
530
Removed:
531
Removed:
userId := anInteger
506
Added:
user := aMFTUser
532
507
]
src/MyFitnessTracker/MFTSession.class.st
@@ -33,8 +33,8 @@
33
33
{ #category : #'as yet unclassified' }
34
34
MFTSession >> loadUserDataFromDb [
35
35
36
Removed:
user workouts: db allUserWorkouts.
37
Removed:
user weights: db allUserWeights
36
Added:
user workouts: db getAllUserWorkouts.
37
Added:
user weights: db getAllUserWeights
38
38
]
39
39
40
40
{ #category : #login }
src/MyFitnessTracker/MFTUser.class.st
@@ -225,7 +225,7 @@
225
225
MFTUser >> weights [
226
226
227
227
^ self session user isLoggedIn
228
Removed:
ifTrue: [ self session db allUserWeights ]
228
Added:
ifTrue: [ self session db getAllUserWeights ]
229
229
ifFalse: [ weights ]
230
230
]
231
231
@@ -239,7 +239,7 @@
239
239
MFTUser >> workouts [
240
240
241
241
^ self session user isLoggedIn
242
Removed:
ifTrue: [ self session db allUserWorkouts ]
242
Added:
ifTrue: [ self session db getAllUserWorkouts ]
243
243
ifFalse: [ workouts ]
244
244
]
245
245