From 7c390800797439ffb1b3b9a45011859b88c1abf2 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 2 Feb 2021 11:30:22 +0000 Subject: [PATCH] update posts handler --- posts/handler/posts.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/posts/handler/posts.go b/posts/handler/posts.go index c448633..39c0202 100644 --- a/posts/handler/posts.go +++ b/posts/handler/posts.go @@ -205,7 +205,23 @@ func (p *Posts) Query(ctx context.Context, req *proto.QueryRequest, rsp *proto.Q logger.Infof("Listing posts, offset: %v, limit: %v", req.Offset, limit) } - return p.db.Read(q, &rsp.Posts) + var posts []*proto.Post + + if err := p.db.Read(q, &posts); err != nil { + return err + } + + if len(posts) <= int(req.Limit) { + rsp.Posts = posts + return nil + } + + // TODO: deal with offset or let model handle it. + for i := 0; i < int(req.Limit); i++ { + rsp.Posts = append(rsp.Posts, posts[i]) + } + + return nil } func (p *Posts) Delete(ctx context.Context, req *proto.DeleteRequest, rsp *proto.DeleteResponse) error {