Created
November 16, 2020 07:42
-
-
Save asim/4cc45b6fceb459332a61bed0f88a99d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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