| 1 | // Copyright (c) 2020-2021 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by a GPL license that can be found in the LICENSE file. |
| 3 | module main |
| 4 | |
| 5 | struct Settings { |
| 6 | id int @[primary; sql: serial] |
| 7 | mut: |
| 8 | oauth_client_id string |
| 9 | oauth_client_secret string |
| 10 | disable_tree_folder_size bool |
| 11 | } |
| 12 | |
| 13 | fn (s Settings) tree_folder_size_enabled() bool { |
| 14 | return !s.disable_tree_folder_size |
| 15 | } |
| 16 | |
| 17 | fn (mut app App) load_settings() { |
| 18 | settings_result := sql app.db { |
| 19 | select from Settings limit 1 |
| 20 | } or { [] } |
| 21 | |
| 22 | app.settings = if settings_result.len == 0 { |
| 23 | Settings{} |
| 24 | } else { |
| 25 | settings_result.first() |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret string, tree_folder_size_enabled bool) ! { |
| 30 | settings_result := sql app.db { |
| 31 | select from Settings limit 1 |
| 32 | } or { [] } |
| 33 | |
| 34 | old_settings := if settings_result.len == 0 { |
| 35 | Settings{} |
| 36 | } else { |
| 37 | settings_result.first() |
| 38 | } |
| 39 | |
| 40 | github_oauth_client_id := if oauth_client_id != '' { |
| 41 | oauth_client_id |
| 42 | } else { |
| 43 | old_settings.oauth_client_id |
| 44 | } |
| 45 | |
| 46 | github_oauth_client_secret := if oauth_client_secret != '' { |
| 47 | oauth_client_secret |
| 48 | } else { |
| 49 | old_settings.oauth_client_secret |
| 50 | } |
| 51 | |
| 52 | disable_tree_folder_size := !tree_folder_size_enabled |
| 53 | |
| 54 | if old_settings.id == 0 { |
| 55 | new_settings := Settings{ |
| 56 | oauth_client_id: github_oauth_client_id |
| 57 | oauth_client_secret: github_oauth_client_secret |
| 58 | disable_tree_folder_size: disable_tree_folder_size |
| 59 | } |
| 60 | |
| 61 | sql app.db { |
| 62 | insert new_settings into Settings |
| 63 | }! |
| 64 | } else { |
| 65 | sql app.db { |
| 66 | update Settings set oauth_client_id = github_oauth_client_id, oauth_client_secret = github_oauth_client_secret, |
| 67 | disable_tree_folder_size = disable_tree_folder_size where id == old_settings.id |
| 68 | }! |
| 69 | } |
| 70 | } |
| 71 | |