-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathsettings.v
More file actions
62 lines (53 loc) · 1.39 KB
/
settings.v
File metadata and controls
62 lines (53 loc) · 1.39 KB
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
// Copyright (c) 2020-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
struct Settings {
id int @[primary; sql: serial]
mut:
oauth_client_id string
oauth_client_secret string
}
fn (mut app App) load_settings() {
settings_result := sql app.db {
select from Settings limit 1
} or { [] }
app.settings = if settings_result.len == 0 {
Settings{}
} else {
settings_result.first()
}
}
fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret string) ! {
settings_result := sql app.db {
select from Settings limit 1
} or { [] }
old_settings := if settings_result.len == 0 {
Settings{}
} else {
settings_result.first()
}
github_oauth_client_id := if oauth_client_id != '' {
oauth_client_id
} else {
old_settings.oauth_client_id
}
github_oauth_client_secret := if oauth_client_secret != '' {
oauth_client_secret
} else {
old_settings.oauth_client_secret
}
if old_settings.id == 0 {
new_settings := Settings{
oauth_client_id: github_oauth_client_id
oauth_client_secret: github_oauth_client_secret
}
sql app.db {
insert new_settings into Settings
}!
} else {
sql app.db {
update Settings set oauth_client_id = github_oauth_client_id, oauth_client_secret = github_oauth_client_secret
where id == old_settings.id
}!
}
}