-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.go
142 lines (122 loc) · 3.96 KB
/
setting.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
package main
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// Setting struct
type Setting struct {
ID int `json:"id,omitempty"` // Pointer makes it optional
// The name of the setting
Name string `json:"name"`
// The value of the setting
Value string `json:"value"`
CreatedAt *time.Time `json:"created_at,omitempty" db:"created_at"` // Pointer makes it optional
UpdatedAt *time.Time `json:"updated_at,omitempty" db:"updated_at"` // Pointer makes it optional
}
func CreateSettingTable(ctx context.Context) {
// Open SQLite database
db, err := sql.Open("sqlite3", "./blocks.db")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to open database: %v", err))
return
}
defer db.Close()
// Create table
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY, name TEXT, value TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP)")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to create table: %v", err))
return
}
_, err = statement.Exec()
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to execute statement: %v", err))
return
}
}
func AddSetting(ctx context.Context, setting Setting) {
// Open SQLite database
db, err := sql.Open("sqlite3", "./blocks.db")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to open database: %v", err))
return
}
defer db.Close()
// Check if setting exists
row := db.QueryRow("SELECT * FROM settings WHERE name = ?", setting.Name)
var existingSetting Setting
err = row.Scan(&existingSetting.ID, &existingSetting.Name, &existingSetting.Value, &existingSetting.CreatedAt, &existingSetting.UpdatedAt)
if err == nil {
// Setting exists, update it
return
}
// Insert data
statement, err := db.Prepare("INSERT INTO settings (name, value, created_at, updated_at) VALUES (?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to prepare statement: %v", err))
return
}
_, err = statement.Exec(setting.Name, setting.Value)
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to execute statement: %v", err))
return
}
}
func GetSetting(ctx context.Context, name string) (Setting, error) {
// Open SQLite database
db, err := sql.Open("sqlite3", "./blocks.db")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to open database: %v", err))
return Setting{}, err
}
defer db.Close()
// Query data
row := db.QueryRow("SELECT * FROM settings WHERE name = ?", name)
var setting Setting
err = row.Scan(&setting.ID, &setting.Name, &setting.Value, &setting.CreatedAt, &setting.UpdatedAt)
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to scan row: %v", err))
return Setting{}, err
}
return setting, nil
}
func SettingExists(ctx context.Context, name string) bool {
// Open SQLite database
db, err := sql.Open("sqlite3", "./blocks.db")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to open database: %v", err))
return false
}
defer db.Close()
// Query data
row := db.QueryRow("SELECT * FROM settings WHERE name = ?", name)
var setting Setting
err = row.Scan(&setting.ID, &setting.Name, &setting.Value, &setting.CreatedAt, &setting.UpdatedAt)
if err != nil {
return false
}
return true
}
func UpdateSetting(ctx context.Context, setting Setting) error {
// Open SQLite database
db, err := sql.Open("sqlite3", "./blocks.db")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to open database: %v", err))
return err
}
defer db.Close()
// Update data
statement, err := db.Prepare("UPDATE settings SET value = ?, updated_at = CURRENT_TIMESTAMP WHERE name = ?")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to prepare statement: %v", err))
return err
}
_, err = statement.Exec(setting.Value, setting.Name)
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to execute statement: %v", err))
return err
}
return nil
}