-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
152 lines (133 loc) · 4.81 KB
/
application.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
package main
import (
"context"
"database/sql"
"fmt"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type Application struct {
ID *int `json:"id,omitempty"` // Pointer makes it optional
Name string `json:"name"`
Description string `json:"description"`
Path string `json:"path,omitempty"` // Pointer makes it optional
IsGenerated bool `json:"is_generated,omitempty"` // Pointer makes it optional
IsSelected bool `json:"is_selected,omitempty"` // Pointer makes it optional
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 AddApplication(ctx context.Context, application Application) {
// 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()
// Insert data
statement, err := db.Prepare("INSERT INTO applications (name, description, path, is_generated, is_selected, created_at, updated_at) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to prepare statement: %v", err))
return
}
execStatement, err := statement.Exec(application.Name, application.Description, application.Path, application.IsGenerated, application.IsSelected)
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to execute statement: %v", err))
return
}
fmt.Println(execStatement)
}
func CreateApplicationTable(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 applications (id INTEGER PRIMARY KEY, name TEXT, description TEXT, path TEXT, is_generated BOOLEAN, is_selected BOOLEAN, 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
}
statement.Exec()
}
func GetApplications(ctx context.Context) []Application {
// 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 nil
}
defer db.Close()
// Query data
rows, err := db.Query("SELECT * FROM applications")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to query database: %v", err))
return nil
}
defer rows.Close()
// Get results
var applications []Application
for rows.Next() {
var application Application
err = rows.Scan(&application.ID, &application.Name, &application.Description, &application.Path, &application.IsGenerated, &application.IsSelected, &application.CreatedAt, &application.UpdatedAt)
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to scan row: %v", err))
return nil
}
applications = append(applications, application)
}
return applications
}
// Update is_selected to true for a given id and for the others false
func UpdateSelectedApplication(ctx context.Context, id int) {
// 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()
// Update data
statement, err := db.Prepare("UPDATE applications SET is_selected = (id = ?)")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to prepare statement: %v", err))
return
}
execStatement, err := statement.Exec(id)
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to execute statement: %v", err))
return
}
fmt.Println(execStatement)
}
// Get the row with is_selected = true
func GetSelectedApplication(ctx context.Context) Application {
// 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 Application{}
}
defer db.Close()
// Query data
rows, err := db.Query("SELECT * FROM applications WHERE is_selected = 1")
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to query database: %v", err))
return Application{}
}
defer rows.Close()
// Get results
var application Application
for rows.Next() {
err = rows.Scan(&application.ID, &application.Name, &application.Description, &application.Path, &application.IsGenerated, &application.IsSelected, &application.CreatedAt, &application.UpdatedAt)
if err != nil {
runtime.LogError(ctx, fmt.Sprintf("Failed to scan row: %v", err))
return Application{}
}
}
return application
}