-
Notifications
You must be signed in to change notification settings - Fork 2
/
DB_user_settings.go
370 lines (325 loc) · 10.1 KB
/
DB_user_settings.go
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package drugdose
import (
"context"
"fmt"
"strconv"
"database/sql"
// MySQL driver needed for sql module
_ "github.com/go-sql-driver/mysql"
// SQLite driver needed for sql module
_ "modernc.org/sqlite"
)
// Constants used for matching settings
const settingTypeID string = "remember-id"
const rememberIDTableName string = "useIDForRemember"
func settingsTables(settingType string) (error, string) {
const printN string = "settingsTables()"
table := ""
if settingType == settingTypeID {
table = rememberIDTableName
} else {
return fmt.Errorf("%s%w: %s", sprintName(printN), NoNametypeError, settingType), ""
}
return nil, table
}
// InitUserSettings creates the row with default settings for a user.
// These settings are kept in the database and are not global like the config
// files. All users have their own settings.
//
// db - open database connection
//
// ctx - context to be passed to sql queries
//
// username - the user to create default settings for
func (cfg *Config) InitUserSettings(db *sql.DB, ctx context.Context, username string) error {
const printN string = "InitUserSettings()"
tx, err := db.BeginTx(ctx, nil)
if err != nil {
err = fmt.Errorf("%s%w", sprintName(printN), err)
return err
}
stmt, err := tx.Prepare("insert into userSettings" +
" (username, useIDForRemember) " +
"values(?, ?)")
err = handleErrRollbackSeq(err, tx, printN, "tx.Prepare(): ")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(username, ForgetInputConfigMagicNumber)
err = handleErrRollbackSeq(err, tx, printN, "tx.Exec(): ")
if err != nil {
return err
}
err = tx.Commit()
err = handleErrRollbackSeq(err, tx, printN, "tx.Commit(): ")
if err != nil {
return err
}
printName(printN, "User settings initialized successfully!")
return nil
}
func returnSetUserSetStmt(set string) string {
return fmt.Sprintf("update userSettings set %s = ? where username = ?", set)
}
// SetUserSettings changes the user settings in the database.
//
// db - open database connection
//
// ctx - context to be passed to sql queries
//
// errChannel - the gorouting channel which returns the errors
// (set to nil if function doesn't need to be concurrent)
//
// set - the name of the setting to change, available names are: remember-id
//
// username - the user the setting is changed for
//
// setValue - the value the setting is changed to
func (cfg *Config) SetUserSettings(db *sql.DB, ctx context.Context,
errChannel chan<- ErrorInfo, set string, username string, setValue string) ErrorInfo {
const printN string = "SetUserSettings()"
tempErrInfo := ErrorInfo{
Err: nil,
Username: username,
Action: ActionSetUserSettings,
}
ret := checkIfExistsDB(db, ctx,
"username", "userSettings",
cfg.DBDriver, cfg.DBSettings[cfg.DBDriver].Path,
nil, username)
if ret == false {
err := cfg.InitUserSettings(db, ctx, username)
if err != nil {
tempErrInfo.Err = fmt.Errorf("%s%w", sprintName(printN), err)
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
}
err, set := settingsTables(set)
if err != nil {
tempErrInfo.Err = fmt.Errorf("%s%w", sprintName(printN), err)
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
stmtStr := returnSetUserSetStmt(set)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
tempErrInfo.Err = fmt.Errorf("%s%s: %w", sprintName(printN), "db.BeginTx()", err)
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
stmt, err := tx.Prepare(stmtStr)
if handleErrRollback(err, tx, errChannel, &tempErrInfo, printN, "tx.Prepare(): ") {
return tempErrInfo
}
defer stmt.Close()
_, err = stmt.Exec(setValue, username)
if handleErrRollback(err, tx, errChannel, &tempErrInfo, printN, "stmt.Exec():") {
return tempErrInfo
}
err = tx.Commit()
if handleErrRollback(err, tx, errChannel, &tempErrInfo, printN, "tx.Commit(): ") {
return tempErrInfo
}
printNameVerbose(cfg.VerbosePrinting, printN, set+": setting changed to:", setValue)
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
// GetUserSettings return the value of a setting for a given user from the database.
//
// db - open database connection
//
// ctx - context to be passed to sql queries
//
// userSetErrChannel - a gorouting channel which is of type UserSettingError,
// holding an error variable and a string of the value for a given setting,
// make sure to always check the error first
// (set to nil if function doesn't need to be concurrent)
//
// set - the name of the setting to get the value of
//
// username - the user for which to get the setting
func (cfg *Config) GetUserSettings(db *sql.DB, ctx context.Context,
userSetErrChannel chan<- UserSettingError, set string, username string) UserSettingError {
const printN string = "GetUserSettings()"
tempUserSetErr := UserSettingError{
UserSetting: "",
Username: username,
Err: nil,
}
fmtStmt := fmt.Sprintf("select %s from userSettings where username = ?", set)
stmt, err := db.PrepareContext(ctx, fmtStmt)
if err != nil {
tempUserSetErr.Err = fmt.Errorf("%s%w", sprintName(printN), err)
tempUserSetErr.UserSetting = ""
if userSetErrChannel != nil {
userSetErrChannel <- tempUserSetErr
}
return tempUserSetErr
}
defer stmt.Close()
var got string
err = stmt.QueryRowContext(ctx, username).Scan(&got)
if err != nil {
tempUserSetErr.Err = fmt.Errorf("%s%w", sprintName(printN), err)
tempUserSetErr.UserSetting = ""
if userSetErrChannel != nil {
userSetErrChannel <- tempUserSetErr
}
return tempUserSetErr
}
tempUserSetErr.Err = nil
tempUserSetErr.UserSetting = got
if userSetErrChannel != nil {
userSetErrChannel <- tempUserSetErr
}
return tempUserSetErr
}
// RememberDosing stores an ID of a log to reuse later via RecallDosing().
// This allows input of the dose only then drug, route, units will be reused
// according to the ID set to recall.
//
// db - open database connection
//
// ctx - context to be passed to sql queries
//
// errChannel - the gorouting channel which returns the errors
// (set to nil if function doesn't need to be concurrent)
//
// username - the user to use for remembering a dosing
//
// forID - the ID to use for remembering a dosing
func (cfg *Config) RememberDosing(db *sql.DB, ctx context.Context,
errChannel chan<- ErrorInfo, username string, forID int64) ErrorInfo {
const printN string = "RememberDosing()"
tempErrInfo := ErrorInfo{
Err: nil,
Username: username,
Action: ActionRememberDosing,
}
forIDStr := strconv.FormatInt(forID, 10)
if forIDStr == "0" {
gotLogs := cfg.GetLogs(db, ctx, nil, 1, 0, username, true, "none", "")
if gotLogs.Err != nil {
tempErrInfo.Err = fmt.Errorf("%s%w", sprintName(printN), gotLogs.Err)
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
forIDStr = strconv.FormatInt(gotLogs.UserLogs[0].StartTime, 10)
}
gotErrInfo := cfg.SetUserSettings(db, ctx, nil, settingTypeID, username, forIDStr)
if gotErrInfo.Err != nil {
tempErrInfo.Err = fmt.Errorf("%s%w", sprintName(printN), gotErrInfo.Err)
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
// RecallDosing gives the data for the last configured dosing using the ID.
// Checkout RememberDosing() for more info!
//
// db - open database connection
//
// ctx - context to be passed to sql queries
//
// userLogsErrorChannel - the goroutine channel used to return the logs and
// the error
// (set to nil if function doesn't need to be concurrent)
//
// username - the user to recall the logs for
func (cfg *Config) RecallDosing(db *sql.DB, ctx context.Context,
userLogsErrorChannel chan<- UserLogsError, username string) UserLogsError {
const printN string = "RememberConfig()"
tempUserLogsError := UserLogsError{
UserLogs: nil,
Username: username,
Err: nil,
}
gotUserSetErr := cfg.GetUserSettings(db, ctx, nil, "useIDForRemember", username)
err := gotUserSetErr.Err
if err != nil {
err = fmt.Errorf("%s%w", sprintName(printN), err)
tempUserLogsError.Err = err
if userLogsErrorChannel != nil {
userLogsErrorChannel <- tempUserLogsError
}
return tempUserLogsError
}
gotID := gotUserSetErr.UserSetting
if gotID == ForgetInputConfigMagicNumber {
tempUserLogsError.Err = nil
tempUserLogsError.UserLogs = nil
if userLogsErrorChannel != nil {
userLogsErrorChannel <- tempUserLogsError
}
return tempUserLogsError
}
gotInt, err := strconv.ParseInt(gotID, 10, 64)
if err != nil {
err := fmt.Errorf("%s%s: %w", sprintName(printN), "strconv.ParseInt()", err)
tempUserLogsError.Err = err
if userLogsErrorChannel != nil {
userLogsErrorChannel <- tempUserLogsError
}
return tempUserLogsError
}
tempUserLogsError = cfg.GetLogs(db, ctx, nil, 1, gotInt, username, false, "", "")
err = tempUserLogsError.Err
if err != nil {
err = fmt.Errorf("%s%w", sprintName(printN), err)
}
tempUserLogsError.Err = err
if userLogsErrorChannel != nil {
userLogsErrorChannel <- tempUserLogsError
}
return tempUserLogsError
}
// ForgetDosing is meant to reset the setting for remembering dosings.
// ForgetInputConfigMagicNumber is a constant containing the value meant to
// be ignored. Checkout RememberDosing()!
//
// db - open database connection
//
// ctx - context to be passed to sql queries
//
// errChannel - the gorouting channel which returns the errors
// (set to nil if function doesn't need to be concurrent)
//
// username - the user for which to forget the dosing
func (cfg *Config) ForgetDosing(db *sql.DB, ctx context.Context,
errChannel chan<- ErrorInfo, username string) ErrorInfo {
const printN string = "ForgetConfig()"
tempErrInfo := ErrorInfo{
Err: nil,
Username: username,
Action: ActionForgetDosing,
}
gotErrInfo := cfg.SetUserSettings(db, ctx, nil, settingTypeID, username, ForgetInputConfigMagicNumber)
if gotErrInfo.Err != nil {
tempErrInfo.Err = fmt.Errorf("%s%w", sprintName(printN), gotErrInfo.Err)
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}
if errChannel != nil {
errChannel <- tempErrInfo
}
return tempErrInfo
}