MFTSQLite3Database is now cognizant of the logged in user.

Commit
35fa87fbd2a1ada60b7583cb33db0d2be6083767
Author
Marius Peter <marius.peter@tutanota.com>
Author date
Committer
Marius Peter <marius.peter@tutanota.com>
Committer date
src/MyFitnessTracker/MFTSQLite3Database.class.st
index b285ce29..a79e0719 100644..100644
@@ -1,6 +1,10 @@
1 1 Class {
2 2 #name : #MFTSQLite3Database,
3 3 #superclass : #SQLite3Database,
4 Added: #instVars : [
5 Added: 'user',
6 Added: 'userId'
7 Added: ],
4 8 #classInstVars : [
5 9 'defaultLocation'
6 10 ],
@@ -111,15 +115,29 @@
111 115 | connection |
112 116 connection := self defaultConnection open.
113 117 MFTWorkout placeholderCollection do: [ :workout |
118 Added: | workoutId |
114 119 connection
115 120 execute:
116 121 'INSERT INTO workouts(user_id, date, primary_muscle, secondary_muscles)
117 Removed: VALUES( ?, ?, ?, ?)'
122 Added: VALUES(?, ?, ?, ?)'
118 123 with: {
119 124 1.
120 Removed: workout date.
121 Removed: workout primaryMuscle.
122 Removed: workout secondaryMuscles } ].
125 Added: workout date yyyymmdd.
126 Added: workout primaryMuscle latinName.
127 Added: (workout secondaryMuscles collect: #latinName) asCommaString }.
128 Added: workoutId := (connection
129 Added: execute: 'SELECT workouts.id
130 Added: FROM workouts
131 Added: WHERE date = ?'
132 Added: value: workout date yyyymmdd) onlyValue.
133 Added: workout exercises do: [ :exercise |
134 Added: connection
135 Added: execute: 'INSERT INTO exercises(workout_id, english_name, sets)
136 Added: VALUES(?, ?, ?)'
137 Added: with: {
138 Added: workoutId.
139 Added: exercise englishName.
140 Added: exercise sets asJson } ] ].
123 141 connection close
124 142 ]
125 143
@@ -165,6 +183,51 @@
165 183 ]
166 184
167 185 { #category : #'as yet unclassified' }
186 Added: MFTSQLite3Database >> allUserWeights [
187 Added:
188 Added: ^ (self class defaultConnection
189 Added: open;
190 Added: execute: 'SELECT weights.*
191 Added: FROM weights
192 Added: WHERE weights.user_id = ?'
193 Added: value: userId) rows collect: [ :each |
194 Added: MFTWeight newFromDictionary: each asDictionary ]
195 Added: ]
196 Added:
197 Added: { #category : #'as yet unclassified' }
198 Added: MFTSQLite3Database >> allUserWorkouts [
199 Added:
200 Added: ^ (self class defaultConnection
201 Added: open;
202 Added: execute: 'SELECT workouts.*
203 Added: FROM workouts
204 Added: WHERE workouts.user_id = ?'
205 Added: value: userId) rows collect: [ :each |
206 Added: MFTWorkout newFromDictionary: each asDictionary ]
207 Added: ]
208 Added:
209 Added: { #category : #'as yet unclassified' }
210 Added: MFTSQLite3Database >> deleteWeight: aMFTWeight [
211 Added:
212 Added: | weightId |
213 Added: weightId := (self
214 Added: select: 'id'
215 Added: from: 'weights'
216 Added: where:
217 Added: ('weights.date = ''{1}'' AND weights.value = ''{2}'''
218 Added: format: {
219 Added: aMFTWeight date yyyymmdd.
220 Added: aMFTWeight value })) onlyValue.
221 Added: weightId ifNil: [
222 Added: self error:
223 Added: 'Blendux: attempted to delete a weight from database with no primary key.' ].
224 Added: connection
225 Added: execute: 'DELETE FROM weights
226 Added: WHERE weights.id = ?'
227 Added: value: weightId
228 Added: ]
229 Added:
230 Added: { #category : #'as yet unclassified' }
168 231 MFTSQLite3Database >> insertWeight: aMFTWeight [
169 232
170 233 connection
@@ -182,70 +245,75 @@
182 245 connection
183 246 execute:
184 247 'INSERT INTO workouts(user_id, date, primary_muscle, secondary_muscles)
185 Removed: VALUES( ?, ?, ?, ?)'
186 Removed: with: { 1. 'foo'. 'Primary muscle'. 'Test' }
248 Added: VALUES(?, ?, ?, ?)'
249 Added: with: {
250 Added: userId.
251 Added: aMFTWorkout date yyyymmdd.
252 Added: aMFTWorkout primaryMuscle englishName.
253 Added: (aMFTWorkout secondaryMuscles collect: #englishName) asCommaString }
187 254 ]
188 255
189 256 { #category : #'as yet unclassified' }
190 257 MFTSQLite3Database >> login: aMFTUser atDateAndTime: aDateAndTime [
191 258
259 Added: self user: aMFTUser.
192 260 connection
193 261 execute: 'UPDATE users
194 262 SET last_login_date_time = ?
195 263 WHERE username = ?'
196 264 with: {
197 265 aDateAndTime.
198 Removed: aMFTUser username }
266 Added: user username }
199 267 ]
200 268
201 269 { #category : #'as yet unclassified' }
202 270 MFTSQLite3Database >> select: attributes from: aTable [
203 271
204 Removed: ^ connection execute:
205 Removed: 'SELECT ' , attributes , ' FROM ' , aTable asString
272 Added: ^ connection execute: ({
273 Added: 'SELECT'.
274 Added: attributes.
275 Added: 'FROM'.
276 Added: aTable asString } joinUsing: Character space)
206 277 ]
207 278
208 279 { #category : #'as yet unclassified' }
209 280 MFTSQLite3Database >> select: attributes from: aTable where: aCondition [
210 281
211 Removed: ^ connection execute:
212 Removed: 'SELECT ' , attributes , ' FROM ' , aTable asString , 'WHERE '
213 Removed: , aCondition
282 Added: ^ connection execute: ({
283 Added: 'SELECT'.
284 Added: attributes.
285 Added: 'FROM'.
286 Added: aTable asString.
287 Added: 'WHERE'.
288 Added: aCondition } joinUsing: Character space)
214 289 ]
215 290
216 291 { #category : #'as yet unclassified' }
217 Removed: MFTSQLite3Database >> selectAll: aTable [
292 Added: MFTSQLite3Database >> updateProfile: aMFTUser [
218 293
219 Removed: ^ self select: '*' from: aTable
294 Added: connection
295 Added: execute: 'UPDATE users
296 Added: SET date = ?,
297 Added: value = ?
298 Added: WHERE user_id = ?'
299 Added: with: { }
220 300 ]
221 301
222 302 { #category : #'as yet unclassified' }
223 Removed: MFTSQLite3Database >> selectWeightsWhereUser: aMFTUser [
303 Added: MFTSQLite3Database >> updateWeight: aMFTWeight [
224 304
225 Removed: ^ (self class defaultConnection
226 Removed: open;
227 Removed: execute: 'SELECT weights.*
228 Removed: FROM weights
229 Removed: INNER JOIN users ON weights.user_id = users.id
230 Removed: WHERE users.username = ?'
231 Removed: value: aMFTUser username) rows collect: [ :each |
232 Removed: MFTWeight newFromDictionary: each asDictionary ]
305 Added: connection
306 Added: execute: 'UPDATE weights
307 Added: SET date = ?,
308 Added: value = ?
309 Added: WHERE user_id = ?'
310 Added: with: {
311 Added: aMFTWeight date yyyymmdd.
312 Added: aMFTWeight value.
313 Added: 1 }
233 314 ]
234 315
235 316 { #category : #'as yet unclassified' }
236 Removed: MFTSQLite3Database >> selectWorkoutsWhereUser: aMFTUser [
237 Removed:
238 Removed: ^ (self class defaultConnection
239 Removed: open;
240 Removed: execute: 'SELECT workouts.*
241 Removed: FROM workouts
242 Removed: INNER JOIN users ON workouts.user_id = users.id
243 Removed: WHERE users.username = ?'
244 Removed: value: aMFTUser username) rows collect: [ :each |
245 Removed: MFTWorkout newFromDictionary: each asDictionary ]
246 Removed: ]
247 Removed:
248 Removed: { #category : #'as yet unclassified' }
249 317 MFTSQLite3Database >> updateWeight: aMFTWeight for: aMFTUser [
250 318
251 319 connection
@@ -257,4 +325,33 @@
257 325 aMFTWeight date yyyymmdd.
258 326 aMFTWeight value.
259 327 aMFTUser username }
328 Added: ]
329 Added:
330 Added: { #category : #accessing }
331 Added: MFTSQLite3Database >> user [
332 Added:
333 Added: ^ user
334 Added: ]
335 Added:
336 Added: { #category : #accessing }
337 Added: MFTSQLite3Database >> user: aMFTUser [
338 Added:
339 Added: user := aMFTUser.
340 Added: userId := (connection
341 Added: execute: 'SELECT users.id
342 Added: FROM users
343 Added: WHERE users.username = ?'
344 Added: value: 'testMFT') onlyRow asArray first
345 Added: ]
346 Added:
347 Added: { #category : #accessing }
348 Added: MFTSQLite3Database >> userId [
349 Added:
350 Added: ^ userId
351 Added: ]
352 Added:
353 Added: { #category : #accessing }
354 Added: MFTSQLite3Database >> userId: anInteger [
355 Added:
356 Added: userId := anInteger
260 357 ]