Skip to content

Commit

Permalink
feat(graphql): implements board connection
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Nov 25, 2022
1 parent a6646bc commit 1a4a7c7
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 66 deletions.
54 changes: 51 additions & 3 deletions app/nemeton/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/cosmos/cosmos-sdk/types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

const (
Expand Down Expand Up @@ -114,8 +114,8 @@ func (s *Store) GetPhases(criteriaFn func(p Phase) bool) []*Phase {
return filtered
}

func (s *Store) GetValidatorByID(ctx context.Context, id primitive.ObjectID) (*Validator, error) {
return s.GetValidatorBy(ctx, bson.M{"_id": id})
func (s *Store) GetValidatorByCursor(ctx context.Context, c Cursor) (*Validator, error) {
return s.GetValidatorBy(ctx, bson.M{"_id": c.objectID})
}

func (s *Store) GetValidatorByValoper(ctx context.Context, addr types.ValAddress) (*Validator, error) {
Expand Down Expand Up @@ -146,3 +146,51 @@ func (s *Store) GetValidatorBy(ctx context.Context, filter bson.M) (*Validator,
var val Validator
return &val, res.Decode(&val)
}

func (s *Store) GetBoard(ctx context.Context, limit int, after *Cursor) ([]*Validator, bool, error) {
var filter bson.M
if after != nil {
filter = bson.M{
"$or": bson.A{
bson.M{
"points": bson.M{"$lt": after.points},
},
bson.M{
"points": after.points,
"_id": bson.M{"$gt": after.objectID},
},
},
}
}
c, err := s.db.Collection(validatorsCollectionName).Find(
ctx,
filter,
options.Find().
SetSort(
bson.D{
bson.E{Key: "points", Value: -1},
bson.E{Key: "_id", Value: 1},
},
).
SetLimit(int64(limit+1)),
)
if err != nil {
return nil, false, err
}
defer func() {
_ = c.Close(ctx)
}()

validators := make([]*Validator, 0, limit)
count := 0
for count < limit && c.Next(ctx) {
count++
var validator Validator
if err := c.Decode(&validator); err != nil {
return nil, false, err
}
validators = append(validators, &validator)
}

return validators, c.Next(ctx), nil
}
29 changes: 19 additions & 10 deletions app/nemeton/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ import (
"net/url"

"github.com/cosmos/cosmos-sdk/types"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type Validator struct {
Moniker string `bson:"moniker"`
Identity *string `bson:"identity,omitempty"`
Valoper types.ValAddress `bson:"valoper"`
Delegator types.AccAddress `bson:"delegator"`
Twitter *string `bson:"twitter,omitempty"`
Website *url.URL `bson:"website,omitempty"`
Discord string `bson:"discord"`
Country string `bson:"country"`
Status string `bson:"status"`
Points int `bson:"points"`
ID primitive.ObjectID `bson:"_id,omitempty"`
Moniker string `bson:"moniker"`
Identity *string `bson:"identity,omitempty"`
Valoper types.ValAddress `bson:"valoper"`
Delegator types.AccAddress `bson:"delegator"`
Twitter *string `bson:"twitter,omitempty"`
Website *url.URL `bson:"website,omitempty"`
Discord string `bson:"discord"`
Country string `bson:"country"`
Status string `bson:"status"`
Points uint64 `bson:"points"`
}

func (v *Validator) Cursor() *Cursor {
return &Cursor{
points: v.Points,
objectID: v.ID,
}
}
1 change: 1 addition & 0 deletions gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ models:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
- github.com/99designs/gqlgen/graphql.Uint64
Time:
model:
- github.com/99designs/gqlgen/graphql.Time
Expand Down
90 changes: 49 additions & 41 deletions graphql/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions graphql/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1a4a7c7

Please sign in to comment.