-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
45 lines (41 loc) · 974 Bytes
/
history.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
package store
import (
"time"
)
func (s *Store) AddHistory(command []string) error {
commandStr, err := ArrayToJsonArray(command)
if err != nil {
return err
}
_, err = s.historyDb.Exec("INSERT INTO history (command_array, used) VALUES (?, ?)", commandStr, time.Now().Format(time.RFC3339))
return err
}
type Result struct {
Command []string
Executed time.Time
}
func (s *Store) GetHistory() ([]Result, error) {
results := make([]Result, 0)
rows, err := s.historyDb.Query("SELECT command_array, used FROM history")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var commandJson string
var d string
if err := rows.Scan(&commandJson, &d); err != nil {
return nil, err
}
var r Result
if r.Command, err = JsonArrayToArray(commandJson); err != nil {
return nil, err
}
r.Executed, err = time.Parse(time.RFC3339, d)
if err != nil {
return nil, err
}
results = append(results, r)
}
return results, nil
}