RTradeLtd / Temporal

☄️ Temporal is an easy-to-use, enterprise-grade interface into distributed and decentralized storage
https://temporal.cloud
MIT License
227 stars 40 forks source link

Enable Paginated Responses From APIs #433

Closed bonedaddy closed 4 years ago

bonedaddy commented 4 years ago

Why are you submitting this feature request Certain APIs can have massive payloads, for example accounts that have thousands of uploads.

Describe the solution you'd like Be able to return paginated results from APIs that return large payloads

Describe alternatives you've considered

N/A

Additional context

~https://github.com/biezhi/gorm-paginator~ Use our fork https://github.com/RTradeLtd/gpaginator

bonedaddy commented 4 years ago

Example

    // TODO(bonedaddy): add tests
    if c.Param("paged") == "true" {
        page := c.Param("page")
        if page == "" {
            page = "1"
        }
        limit := c.Param("limit")
        if limit == "" {
            limit = "10"
        }
        pageInt, err := strconv.Atoi(page)
        if err != nil {
            Fail(c, err, http.StatusBadRequest)
            return
        }
        limitInt, err := strconv.Atoi(limit)
        if err != nil {
            Fail(c, err, http.StatusBadRequest)
            return
        }
        var uploads []models.Upload
        paged, err := gpaginator.Paging(
            &gpaginator.Param{
                DB:    api.upm.DB.Where("user_name = ?", username),
                Page:  pageInt,
                Limit: limitInt,
            },
            &uploads,
        )
        if err != nil {
            api.LogError(c, err, "failed to get paged user upload")
            return
        }
        Respond(c, http.StatusOK, gin.H{"response": paged})
        return
    }