Skip to content

Instantly share code, notes, and snippets.

@asim
Created November 16, 2020 07:42
Show Gist options
  • Save asim/4cc45b6fceb459332a61bed0f88a99d6 to your computer and use it in GitHub Desktop.
Save asim/4cc45b6fceb459332a61bed0f88a99d6 to your computer and use it in GitHub Desktop.
func (p *Posts) Query(ctx context.Context, req *proto.QueryRequest, rsp *proto.QueryResponse) error {
var q model.Query
if len(req.Slug) > 0 {
logger.Infof("Reading post by slug: %v", req.Slug)
q = p.slugIndex.ToQuery(req.Slug)
} else if len(req.Id) > 0 {
logger.Infof("Reading post by id: %v", req.Id)
q = p.idIndex.ToQuery(req.Id)
} else {
q = p.createdIndex.ToQuery(nil)
var limit uint
limit = 20
if req.Limit > 0 {
limit = uint(req.Limit)
}
q.Limit = int64(limit)
q.Offset = req.Offset
logger.Infof("Listing posts, offset: %v, limit: %v", req.Offset, limit)
}
posts := []*proto.Post{}
err := p.db.List(q, &posts)
if err != nil {
return errors.BadRequest("proto.query.store-read", "Failed to read from store: %v", err.Error())
}
rsp.Posts = posts
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment