Skip to content

Commit

Permalink
Merge pull request #6 from nrfta/feat/add-sort-by
Browse files Browse the repository at this point in the history
Add sort by to paging
  • Loading branch information
josemarluedke authored Sep 28, 2023
2 parents 30bcf97 + 86d1486 commit e6e37e0
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 3 deletions.
16 changes: 14 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ package paging

// PageArgs is used as the query inputs
type PageArgs struct {
First *int `json:"first,omitempty"`
After *string `json:"after,omitempty"`
First *int `json:"first,omitempty"`
After *string `json:"after,omitempty"`
sortByCols []string
isDesc bool
}

func WithSortBy(pa *PageArgs, isDesc bool, cols ...string) *PageArgs {
if pa == nil {
pa = &PageArgs{}
}

pa.isDesc = isDesc
pa.sortByCols = cols
return pa
}

// PageInfo is the base struct for building PageInfo. It expects inline functions for all the fields
Expand Down
14 changes: 14 additions & 0 deletions offset_paginator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package paging

import (
"strings"

"github.com/volatiletech/sqlboiler/v4/queries/qm"
)

Expand All @@ -11,6 +13,7 @@ type OffsetPaginator struct {
Limit int
Offset int
PageInfo PageInfo
orderBy string
}

// NewOffsetPaginator creates a new offset paginator
Expand All @@ -34,10 +37,20 @@ func NewOffsetPaginator(

offset := DecodeOffsetCursor(page.After)

orderBy := "created_at"
if len(page.sortByCols) > 0 {
orderBy = strings.Join(page.sortByCols, ", ")
}

if page.isDesc {
orderBy = orderBy + " DESC"
}

return OffsetPaginator{
Limit: limit,
Offset: offset,
PageInfo: NewOffsetBasedPageInfo(&limit, totalCount, offset),
orderBy: orderBy,
}
}

Expand All @@ -46,5 +59,6 @@ func (p *OffsetPaginator) QueryMods() []qm.QueryMod {
return []qm.QueryMod{
qm.Offset(p.Offset),
qm.Limit(p.Limit),
qm.OrderBy(p.orderBy),
}
}
116 changes: 116 additions & 0 deletions orderby_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package paging

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestGoPaging(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GoPaging Suite")
}

var _ = Describe("Order By clause", func() {
var (
pa *PageArgs
cols []string
)

BeforeEach(func() {
first := 0
after := "after"
pa = &PageArgs{
After: &after,
First: &first,
}
cols = []string{"col1", "col2"}
})

Describe("WithSortBy", func() {
It("should handle a nil PageArgs arg", func() {
pa := WithSortBy(nil, true, "col1")
Expect(pa).ToNot(BeNil())
})
})

It("should have zero values for basic PageArgs", func() {
Expect(pa.sortByCols).To(BeNil())
Expect(pa.isDesc).To(BeFalse())
})

Describe("Default", func() {
It("should use `created_at` for default orderby column", func() {
sut := NewOffsetPaginator(pa, 5)

Expect(sut.orderBy).To(Equal("created_at"))
})
})

Describe("Desc Flag & Cols", func() {
Describe("Desc = true", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, true, cols...)

Expect(pa.isDesc).To(BeTrue())
Expect(pa.sortByCols).To(ContainElements(cols))
})

It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, true, cols...)
sut := NewOffsetPaginator(pa, 5)

Expect(sut.orderBy).To(Equal("col1, col2 DESC"))
})
})

Describe("Desc = false", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, false, cols...)

Expect(pa.isDesc).To(BeFalse())
Expect(pa.sortByCols).To(ContainElements(cols))
})

It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, false, cols...)
sut := NewOffsetPaginator(pa, 5)

Expect(sut.orderBy).To(Equal("col1, col2"))
})
})
})

Describe("Desc Flag only", func() {
Describe("Desc = true", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, true)

Expect(pa.isDesc).To(BeTrue())
})

It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, true)
sut := NewOffsetPaginator(pa, 5)

Expect(sut.orderBy).To(Equal("created_at DESC"))
})
})

Describe("Desc = false", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, false)

Expect(pa.isDesc).To(BeFalse())
})

It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, false)
sut := NewOffsetPaginator(pa, 5)

Expect(sut.orderBy).To(Equal("created_at"))
})
})
})
})
File renamed without changes.
2 changes: 1 addition & 1 deletion go_paging_suite_test.go → tests/go_paging_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (

func TestGoPaging(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GoPaging Suite")
RunSpecs(t, "GoPaging Test Suite")
}
3 changes: 3 additions & 0 deletions offset_paginator_test.go → tests/offset_paginator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ var _ = Describe("OffsetPaginator", func() {

qm2 := reflect.TypeOf(mods[1]).String()
Expect(qm2).To(Equal("qm.limitQueryMod"))

qm3 := reflect.TypeOf(mods[2]).String()
Expect(qm3).To(Equal("qm.orderByQueryMod"))
})
})
File renamed without changes.

0 comments on commit e6e37e0

Please sign in to comment.