Skip to content

Commit

Permalink
Update post list response
Browse files Browse the repository at this point in the history
  • Loading branch information
hayashiki committed Sep 21, 2020
1 parent 760ddad commit 5cbc51a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions post.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -86,6 +87,12 @@ type PostListOptions struct {
PerPage int `url:"per_page,omitempty"`
}

func (opts *PostListOptions) SetDefaultSort() {
if !strings.Contains(opts.Q, "desc:") {
opts.Q += "+desc:score"
}
}

// List Post
func (s *postService) List(opts *PostListOptions) ([]*Post, *Response, error) {

Expand All @@ -95,6 +102,7 @@ func (s *postService) List(opts *PostListOptions) ([]*Post, *Response, error) {
return nil, nil, err
}

opts.SetDefaultSort()
q := u.Query()
q.Set("per_page", strconv.Itoa(opts.PerPage))
q.Set("page", strconv.Itoa(opts.Page))
Expand All @@ -107,18 +115,18 @@ func (s *postService) List(opts *PostListOptions) ([]*Post, *Response, error) {
return nil, nil, err
}

mResp := &PostListResponse{}
resp, err := s.client.Do(req, mResp)
posts := &PostListResponse{}
resp, err := s.client.Do(req, posts)

if err != nil {
return nil, nil, err
}

resp.Total = mResp.Meta.Total
resp.NextPage = mResp.Meta.NextPage
resp.PreviousPage = mResp.Meta.PreviousPage
resp.Total = posts.Meta.Total
resp.NextPage = posts.Meta.NextPage
resp.PreviousPage = posts.Meta.PreviousPage

return mResp.Posts, resp, err
return posts.Posts, resp, err
}

// Get Post
Expand All @@ -136,14 +144,14 @@ func (s *postService) Get(postID int) (*Post, *Response, error) {
return nil, nil, err
}

mResp := &Post{}
resp, err := s.client.Do(req, mResp)
post := &Post{}
resp, err := s.client.Do(req, post)

if err != nil {
return nil, nil, err
}

return mResp, resp, err
return post, resp, err
}

// Create Post
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package docbase

const (
version = "0.1.1"
version = "1.0.0"
revision = "HEAD"
)

0 comments on commit 5cbc51a

Please sign in to comment.