-
Notifications
You must be signed in to change notification settings - Fork 0
/
interaction.go
73 lines (55 loc) · 1.47 KB
/
interaction.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
package db
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/tofa-project/client-daemon/lib"
)
// Retrieves all records from 'apps' table formatted
func GetApps() []j {
rows := Query("SELECT * FROM apps;")
defer rows.Close()
response := make([]j, 0)
for rows.Next() {
var id, data string
err := rows.Scan(&id, &data)
if err != nil {
fmt.Println("Some error occurred: ", err, "Possibly corrupted data skipping...")
continue
}
dataMap := Decrypt(data)
dataMap["id"] = id
response = append(response, dataMap)
}
return response
}
// Retrieves row data by its ID formatted
func GetAppByID(id string) j {
var i, data string
res := Instance.QueryRow("SELECT * FROM apps WHERE id = ?", id)
err := res.Scan(&i, &data)
if err != nil {
return nil
}
dataMap := Decrypt(data)
dataMap["id"] = i
return dataMap
}
// Creates a new application with afferent data and inserts it in DB.
// Returns newly created app's ID
func MakeApp(data j) string {
dataEnc := Encrypt(data)
idBytes := md5.Sum([]byte(lib.GenerateRandomString(32)))
idHash := hex.EncodeToString(idBytes[:])
Exec("INSERT INTO apps VALUES(?, ?);", idHash, dataEnc)
return idHash
}
// Updates an app based on ID. Overwrites existing data
func UpdateApp(appID string, data j) {
dataEnc := Encrypt(data)
Exec("UPDATE apps SET data = ? WHERE id = ?", dataEnc, appID)
}
// Permanently deletes app from database
func DeleteApp(appID string) {
Exec("DELETE FROM apps WHERE id = ?", appID)
}