Browse Source

Pagination, implement Limit func

Volodymyr Tkach 2 years ago
parent
commit
0252234b88
2 changed files with 29 additions and 0 deletions
  1. 5 0
      utils/pagination/pagination.go
  2. 24 0
      utils/pagination/pagination_test.go

+ 5 - 0
utils/pagination/pagination.go

@@ -48,3 +48,8 @@ func (d *Data) ResultsCount() int64 {
 func (d *Data) ResultsPerPage() int64 {
 func (d *Data) ResultsPerPage() int64 {
 	return d.resultsPerPage
 	return d.resultsPerPage
 }
 }
+
+// Returns [LIMIT, OFFSET]
+func (d *Data) Limit() (int64, int64) {
+	return d.resultsPerPage, d.currentPage*d.resultsPerPage - d.resultsPerPage
+}

+ 24 - 0
utils/pagination/pagination_test.go

@@ -73,6 +73,30 @@ var _ = Describe("pagination", func() {
 				Expect(pagination.New(0, 0, 3).ResultsPerPage()).To(Equal(int64(3)))
 				Expect(pagination.New(0, 0, 3).ResultsPerPage()).To(Equal(int64(3)))
 			})
 			})
 		})
 		})
+
+		Context("Limit", func() {
+			It("returns correct value", func() {
+				limit, offset := pagination.New(1, 10, 2).Limit()
+				Expect(limit).To(Equal(int64(2)))
+				Expect(offset).To(Equal(int64(0)))
+
+				limit, offset = pagination.New(2, 10, 2).Limit()
+				Expect(limit).To(Equal(int64(2)))
+				Expect(offset).To(Equal(int64(2)))
+
+				limit, offset = pagination.New(3, 10, 2).Limit()
+				Expect(limit).To(Equal(int64(2)))
+				Expect(offset).To(Equal(int64(4)))
+
+				limit, offset = pagination.New(4, 10, 2).Limit()
+				Expect(limit).To(Equal(int64(2)))
+				Expect(offset).To(Equal(int64(6)))
+
+				limit, offset = pagination.New(5, 10, 2).Limit()
+				Expect(limit).To(Equal(int64(2)))
+				Expect(offset).To(Equal(int64(8)))
+			})
+		})
 	})
 	})
 })
 })