Browse Source

Add pagination package

Volodymyr Tkach 2 years ago
parent
commit
1637fe42e0
2 changed files with 132 additions and 0 deletions
  1. 50 0
      utils/pagination/pagination.go
  2. 82 0
      utils/pagination/pagination_test.go

+ 50 - 0
utils/pagination/pagination.go

@@ -0,0 +1,50 @@
+package pagination
+
+import "math"
+
+type Data struct {
+	currentPage    int64
+	maxPages       int64
+	resultsCount   int64
+	resultsPerPage int64
+}
+
+func New(currentPage, resultsCount, resultsPerPage int64) *Data {
+	maxPages := int64(1)
+	if resultsCount > 0 {
+		maxPages = int64(
+			math.Ceil(
+				float64(resultsCount) / float64(resultsPerPage),
+			),
+		)
+	}
+
+	if currentPage < 1 {
+		currentPage = 1
+	} else if currentPage > maxPages {
+		currentPage = maxPages
+	}
+
+	return &Data{
+		currentPage:    currentPage,
+		maxPages:       maxPages,
+		resultsCount:   resultsCount,
+		resultsPerPage: resultsPerPage,
+	}
+}
+
+func (d *Data) CurrentPage() int64 {
+	return d.currentPage
+}
+
+func (d *Data) MaxPages() int64 {
+	return d.maxPages
+}
+
+func (d *Data) ResultsCount() int64 {
+	return d.resultsCount
+}
+
+func (d *Data) ResultsPerPage() int64 {
+	return d.resultsPerPage
+}

+ 82 - 0
utils/pagination/pagination_test.go

@@ -0,0 +1,82 @@
+package pagination_test
+
+import (
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"github.com/vladimirok5959/golang-utils/utils/pagination"
+)
+
+var _ = Describe("pagination", func() {
+	Context("Data", func() {
+		Context("New", func() {
+			It("generate correct data", func() {
+				pd := pagination.New(0, 0, 2)
+				Expect(pd.CurrentPage()).To(Equal(int64(1)))
+				Expect(pd.MaxPages()).To(Equal(int64(1)))
+				Expect(pd.ResultsCount()).To(Equal(int64(0)))
+				Expect(pd.ResultsPerPage()).To(Equal(int64(2)))
+			})
+		})
+
+		Context("CurrentPage", func() {
+			It("returns correct value", func() {
+				Expect(pagination.New(0, 0, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(1, 0, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(2, 0, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(3, 0, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(-1, 0, 2).CurrentPage()).To(Equal(int64(1)))
+
+				Expect(pagination.New(0, 2, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(1, 2, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(2, 2, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(3, 2, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(-1, 2, 2).CurrentPage()).To(Equal(int64(1)))
+
+				Expect(pagination.New(0, 4, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(1, 4, 2).CurrentPage()).To(Equal(int64(1)))
+				Expect(pagination.New(2, 4, 2).CurrentPage()).To(Equal(int64(2)))
+				Expect(pagination.New(3, 4, 2).CurrentPage()).To(Equal(int64(2)))
+				Expect(pagination.New(-1, 4, 2).CurrentPage()).To(Equal(int64(1)))
+			})
+		})
+
+		Context("MaxPages", func() {
+			It("returns correct value", func() {
+				Expect(pagination.New(0, 0, 2).MaxPages()).To(Equal(int64(1)))
+				Expect(pagination.New(0, 1, 2).MaxPages()).To(Equal(int64(1)))
+				Expect(pagination.New(0, 2, 2).MaxPages()).To(Equal(int64(1)))
+				Expect(pagination.New(0, 3, 2).MaxPages()).To(Equal(int64(2)))
+				Expect(pagination.New(0, 4, 2).MaxPages()).To(Equal(int64(2)))
+				Expect(pagination.New(0, 5, 2).MaxPages()).To(Equal(int64(3)))
+				Expect(pagination.New(0, 6, 2).MaxPages()).To(Equal(int64(3)))
+				Expect(pagination.New(0, 7, 2).MaxPages()).To(Equal(int64(4)))
+				Expect(pagination.New(0, 8, 2).MaxPages()).To(Equal(int64(4)))
+				Expect(pagination.New(0, 9, 2).MaxPages()).To(Equal(int64(5)))
+				Expect(pagination.New(0, 10, 2).MaxPages()).To(Equal(int64(5)))
+			})
+		})
+
+		Context("ResultsCount", func() {
+			It("returns correct value", func() {
+				Expect(pagination.New(0, 1, 0).ResultsCount()).To(Equal(int64(1)))
+				Expect(pagination.New(0, 2, 0).ResultsCount()).To(Equal(int64(2)))
+				Expect(pagination.New(0, 3, 0).ResultsCount()).To(Equal(int64(3)))
+			})
+		})
+
+		Context("ResultsPerPage", func() {
+			It("returns correct value", func() {
+				Expect(pagination.New(0, 0, 1).ResultsPerPage()).To(Equal(int64(1)))
+				Expect(pagination.New(0, 0, 2).ResultsPerPage()).To(Equal(int64(2)))
+				Expect(pagination.New(0, 0, 3).ResultsPerPage()).To(Equal(int64(3)))
+			})
+		})
+	})
+})
+
+func TestSuite(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "pagination")
+}