mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
add Posts.Index
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user