Skip to content

Instantly share code, notes, and snippets.

@timakin
Created December 2, 2018 06:19
Show Gist options
  • Save timakin/536e632cce1a6310d64bc0d3fe89d91c to your computer and use it in GitHub Desktop.
Save timakin/536e632cce1a6310d64bc0d3fe89d91c to your computer and use it in GitHub Desktop.
getVideosByPublishedAt
func (repo videoRepository) getVideosByPublishedAt(ctx context.Context, so *entity.SearchOptions) ([]*entity.Video, string, error) {
var vs []*entity.Video
g, err := infrastructure.BoomFromContext(ctx)
if err != nil {
return nil, "", err
}
query := g.Client.NewQuery("Video").Filter("Enabled = ", true).Limit(so.Paging.Limit).Order("-PublishedAt")
if so.Paging.Cursor != "" {
cursor, err := g.DecodeCursor(so.Paging.Cursor)
if err != nil {
return nil, "", err
}
query = query.Start(cursor)
}
var v entity.Video
co, err := g.Count(query)
if co == 0 || err != nil {
return nil, "", err
}
it := g.Run(query)
for {
_, err = it.Next(&v)
if err != nil {
break
}
v := v
vs = append(vs, &v)
}
if err != iterator.Done {
return nil, "", err
}
// Get the cursor for the next page of results.
nc, err := it.Cursor()
if err != nil {
return nil, "", err
}
return vs, nc.String(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment