-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
89 lines (78 loc) · 1.79 KB
/
app.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
package main
import (
"context"
"encoding/json"
"fmt"
"keygugo/constant"
"keygugo/database"
"keygugo/database/dao"
"keygugo/internal"
"keygugo/security"
"os"
"path/filepath"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) shutdown(ctx context.Context) {
dbPath := filepath.Join(constant.GetFolderPath(), "storage.db")
bs := make([]byte, 32)
s := constant.Key
copy(bs, []byte(s))
_, err := security.EncryptFile(dbPath, bs)
if err != nil {
fmt.Println(err)
}
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) GetDB() string {
return string(database.GetData())
}
func (a *App) AddNoteToDB(jsonData string) {
type localData struct {
Title string `json:"title"`
Content string `json:"content"`
}
data := localData{}
err := json.Unmarshal([]byte(jsonData), &data)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(data)
database.AddNote(data.Title, data.Content)
}
func (a *App) DeleteNoteFromDB(id int) {
dao.DeleteNoteDB(id)
}
func (a *App) EditNoteToDB(id int, title string, content string) {
database.UpdateNote(id, title, content)
}
func (a *App) GetKey(key string) bool {
constant.Key = key
constant.HashSum, _ = internal.ReadFileHash()
fmt.Println(constant.Key, constant.HashSum)
err := internal.CheckKeyHash(constant.Key)
if err != nil {
fmt.Println(err)
if os.IsNotExist(err) {
internal.CreateKeyHash(constant.Key)
} else {
return false
}
}
database.LoadData()
return true
}