Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle url query parsing failure #344

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (nc *NewsController) GetAuthors(c *gin.Context) (int, gin.H, error) {
var err error
var total int

_, limit, offset, sort, _ := nc.GetQueryParam(c)
_, _, limit, offset, sort, _ := nc.GetQueryParam(c)

if limit == 0 {
limit = defaultLimit
Expand Down
4 changes: 2 additions & 2 deletions controllers/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewNewsController(s storage.NewsStorage) *NewsController {
}

// GetQueryParam pares url param
func (nc *NewsController) GetQueryParam(c *gin.Context) (mq models.MongoQuery, limit int, offset int, sort string, full bool) {
func (nc *NewsController) GetQueryParam(c *gin.Context) (err error, mq models.MongoQuery, limit int, offset int, sort string, full bool) {
where := c.Query("where")
_limit := c.Query("limit")
_offset := c.Query("offset")
Expand All @@ -43,7 +43,7 @@ func (nc *NewsController) GetQueryParam(c *gin.Context) (mq models.MongoQuery, l
where = "{}"
}

_ = models.GetQuery(where, &mq)
err = models.GetQuery(where, &mq)

return
}
14 changes: 12 additions & 2 deletions controllers/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"strconv"

//log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"twreporter.org/go-api/models"
)
Expand All @@ -14,9 +15,18 @@ import (
func (nc *NewsController) GetPosts(c *gin.Context) {
var total int
var posts []models.Post
var err error

mq, limit, offset, sort, full := nc.GetQueryParam(c)
err, mq, limit, offset, sort, full := nc.GetQueryParam(c)

// response empty records if parsing url query param occurs error
if err != nil {
c.JSON(http.StatusOK, gin.H{"status": "ok", "records": posts, "meta": models.MetaOfResponse{
Total: total,
Offset: offset,
Limit: limit,
}})
return
}

if limit == 0 {
limit = 10
Expand Down
13 changes: 11 additions & 2 deletions controllers/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ import (
func (nc *NewsController) GetTopics(c *gin.Context) {
var total int
var topics []models.Topic
var err error

mq, limit, offset, sort, full := nc.GetQueryParam(c)
err, mq, limit, offset, sort, full := nc.GetQueryParam(c)

// response empty records if parsing url query param occurs error
if err != nil {
c.JSON(http.StatusOK, gin.H{"status": "ok", "records": topics, "meta": models.MetaOfResponse{
Total: total,
Offset: offset,
Limit: limit,
}})
return
}

if limit == 0 {
limit = 10
Expand Down