1package hof
2
3import (
4 "errors"
5
6 "gno.land/p/demo/avl"
7 "gno.land/p/demo/ufmt"
8 "gno.land/p/jeronimoalbi/datasource"
9)
10
11func NewDatasource() Datasource {
12 return Datasource{exhibition}
13}
14
15type Datasource struct {
16 exhibition *Exhibition
17}
18
19func (ds Datasource) Size() int { return ds.exhibition.itemsSorted.Size() }
20
21func (ds Datasource) Records(q datasource.Query) datasource.Iterator {
22 return &iterator{
23 exhibition: ds.exhibition,
24 index: q.Offset,
25 maxIndex: q.Offset + q.Count,
26 }
27}
28
29func (ds Datasource) Record(id string) (datasource.Record, error) {
30 v, found := ds.exhibition.itemsSorted.Get(id)
31 if !found {
32 return nil, errors.New("realm submission not found")
33 }
34 return record{v.(*Item)}, nil
35}
36
37type record struct {
38 item *Item
39}
40
41func (r record) ID() string { return r.item.id.String() }
42func (r record) String() string { return r.item.pkgpath }
43
44func (r record) Fields() (datasource.Fields, error) {
45 fields := avl.NewTree()
46 fields.Set(
47 "details",
48 ufmt.Sprintf("Votes: ⏶ %d - ⏷ %d", r.item.upvote.Size(), r.item.downvote.Size()),
49 )
50 return fields, nil
51}
52
53func (r record) Content() (string, error) {
54 content := ufmt.Sprintf("# Submission #%d\n\n", int(r.item.id))
55 content += r.item.Render(false)
56 return content, nil
57}
58
59type iterator struct {
60 exhibition *Exhibition
61 index, maxIndex int
62 record *record
63}
64
65func (it iterator) Record() datasource.Record { return it.record }
66func (it iterator) Err() error { return nil }
67
68func (it *iterator) Next() bool {
69 if it.index >= it.maxIndex || it.index >= it.exhibition.itemsSorted.Size() {
70 return false
71 }
72
73 _, v := it.exhibition.itemsSorted.GetByIndex(it.index)
74 it.record = &record{v.(*Item)}
75 it.index++
76 return true
77}
datasource.gno
1.70 Kb · 77 lines