/
actor.go
86 lines (73 loc) · 1.91 KB
/
actor.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
package postgres
import (
"github.com/glifio/graph/pkg/lily"
_ "github.com/lib/pq"
)
type Actor struct {
db Database
}
func (t *Actor) Init(db Database) error {
t.db = db;
return nil
}
func (t *Actor) Get(id string) (*lily.ActorItem, error) {
// Prepare query, takes a name argument, protects from sql injection
// stmt, err := t.db.Db.Prepare("select m.id, m.code, m.head, m.nonce, m.balance, m.state_root, m.height from actors m where m.id = $1")
// if err != nil {
// fmt.Println("Get Actor Preperation Err: ", err)
// }
var actor lily.ActorItem
// Make query with our stmt, passing in name argument
// err = stmt.QueryRow(id).Scan(&actor.ID,
// &actor.Code,
// &actor.Head,
// &actor.Nonce,
// &actor.Balance,
// &actor.StateRoot,
// &actor.Height)
// if err != nil {
// fmt.Println("GetMessages Query Err: ", err)
// }
// if err != nil {
// return nil, err
// }
return &actor, nil
}
func (t *Actor) List() ([]lily.ActorItem, error) {
// Prepare query, takes a name argument, protects from sql injection
// stmt, err := t.db.Db.Prepare("select m.id, m.code, m.head, m.nonce, m.balance, m.state_root, m.height from actors m limit 5")
// if err != nil {
// fmt.Println("Get Actors Preperation Err: ", err)
// }
// // Make query with our stmt
// rows, err := stmt.Query()
// if err != nil {
// fmt.Println("Get Actors Query Err: ", err)
// }
// if rows != nil {
// defer rows.Close()
// }
// if err != nil {
// return nil, err
// }
// Create slice of Users for our response
actors := []lily.ActorItem{}
// for rows.Next() {
// actor := lily.ActorItem{}
// err = rows.Scan(&actor.ID,
// &actor.Code,
// &actor.Head,
// &actor.Nonce,
// &actor.Balance,
// &actor.StateRoot,
// &actor.Height)
// if err != nil {
// return nil, err
// }
// actors = append(actors, actor)
// }
// if rows.Err() != nil {
// return nil, err
// }
return actors, nil
}