DEV Community

Discussion on: Pagination using Gorm scopes

Collapse
 
stevebaros profile image
Steve Baros • Edited

Thank you this is good

for those using gin , you can dynamically pick the parameters like this

 page, _ := strconv.Atoi(context.Query("page"))
    if page <= 0 {
        page = 1
    }

    pageSize, _ := strconv.Atoi(context.Query("per_page"))
    sort := context.Query("sort")
    var sortWithDirection string
    if sort != "" {
        direction := context.Query("sortDesc")
        if direction != "" {
            if direction == "true" {
                sortWithDirection = sort + " desc"
            } else if direction == "false" {
                sortWithDirection = sort + " asc"
            }
        }

    }

    switch {
    case pageSize <= 0:
        pageSize = 10
    }
Enter fullscreen mode Exit fullscreen mode