-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.go
81 lines (63 loc) · 1.45 KB
/
crud.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
package main
import (
"context"
"crcls-converse/logger"
"encoding/json"
"strings"
ipfsDs "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
func (crcls *CRCLS) CreateCircle(ctx context.Context, data []byte) error {
c := &Circle{}
if err := json.Unmarshal(data, c); err != nil {
return err
}
id := c.Id
c.Author = crcls.Account.Wallet.Address
// TODO: validate the id
// TODO: mint NFT
data, err := json.Marshal(&c)
if err != nil {
return err
}
key := ipfsDs.NewKey("/circles/" + id)
crcls.DS.Put(ctx, key, data)
return nil
}
type FilterKeyIncludes struct {
Matches []string
}
func (f FilterKeyIncludes) Filter(e query.Entry) bool {
for _, match := range f.Matches {
if found := strings.HasPrefix(e.Key, "/circles/"+match); found {
return true
}
}
return false
}
func (f FilterKeyIncludes) String() string {
return "FilterKeyIncludes"
}
func (crcls *CRCLS) ListCircles(ctx context.Context) ([]*Circle, error) {
log := logger.GetLogger()
circles := make([]*Circle, 0)
q := query.Query{
Filters: []query.Filter{FilterKeyIncludes{crcls.Account.Circles}},
KeysOnly: false,
}
results, err := crcls.DS.Query(ctx, q)
if err != nil {
log.Debug(err)
return circles, err
}
defer results.Close()
for res := range results.Next() {
circle := &Circle{}
if err := json.Unmarshal(res.Value, circle); err != nil {
log.Error(err)
continue
}
circles = append(circles, circle)
}
return circles, nil
}