add Posts.Index

This commit is contained in:
Asim Aslam
2021-02-02 12:58:17 +00:00
parent fc61b45ee4
commit c19bf17fb5
4 changed files with 190 additions and 36 deletions

View File

@@ -183,6 +183,40 @@ func (p *Posts) diffTags(ctx context.Context, parentID string, oldTagNames, newT
return nil
}
func (p *Posts) Index(ctx context.Context, req *proto.IndexRequest, rsp *proto.IndexResponse) error {
// create a simple descending order query
q := model.QueryEquals("created", nil)
q.Order.Type = model.OrderTypeDesc
var posts []*proto.Post
// read all the records
if err := p.db.Read(q, &posts); err != nil {
return err
}
// model does not deal with limits yet
limit := int(req.Limit)
// TODO: implement offset
if limit == 0 {
limit = 20
}
// set the limit to length of posts
if v := len(posts); v < limit {
limit = v
}
// iterate and add
for i := 0; i <= limit; i++ {
// strip the content
posts[i].Content = ""
rsp.Posts = append(rsp.Posts, posts[i])
}
return nil
}
func (p *Posts) Query(ctx context.Context, req *proto.QueryRequest, rsp *proto.QueryResponse) error {
var q model.Query
if len(req.Slug) > 0 {