Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a110f9

Browse files
committed
add proper pagination
1 parent 23ce340 commit 6a110f9

3 files changed

Lines changed: 55 additions & 9 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package handlers
2+
3+
const (
4+
DefaultLimit = 10
5+
DefaultPage = 1
6+
)
7+
8+
// Based on https://dev.to/rafaelgfirmino/pagination-using-gorm-scopes-3k5f
9+
10+
type Pagination struct {
11+
Limit int `json:"limit,omitempty" form:"limit"`
12+
Page int `json:"page,omitempty" form:"page"`
13+
TotalRows int64 `json:"total_rows"`
14+
TotalPages int `json:"total_pages"`
15+
Rows interface{} `json:"rows"`
16+
}
17+
18+
func (p *Pagination) GetOffset() int {
19+
return (p.GetPage() - 1) * p.GetLimit()
20+
}
21+
22+
func (p *Pagination) GetLimit() int {
23+
if p.Limit == 0 {
24+
p.Limit = DefaultLimit
25+
}
26+
return p.Limit
27+
}
28+
29+
func (p *Pagination) GetPage() int {
30+
if p.Page == 0 {
31+
p.Page = DefaultPage
32+
}
33+
return p.Page
34+
}

back_end/src/handlers/taskHandler.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package handlers
22

33
import (
44
"net/http"
5-
"strconv"
6-
"time"
75

86
"back_end/src/models"
97

@@ -21,7 +19,7 @@ func (h *TaskHandler) CreateTask(c *gin.Context) {
2119
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
2220
return
2321
}
24-
task.CreateDate = time.Now()
22+
2523
if err := h.DB.Create(&task).Error; err != nil {
2624
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
2725
return
@@ -30,16 +28,31 @@ func (h *TaskHandler) CreateTask(c *gin.Context) {
3028
}
3129

3230
func (h *TaskHandler) GetTasks(c *gin.Context) {
31+
var pagination Pagination
32+
if err := c.ShouldBindQuery(&pagination); err != nil {
33+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
34+
return
35+
}
36+
3337
var tasks []models.Task
34-
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
35-
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
36-
offset := (page - 1) * limit
38+
offset := pagination.GetOffset()
39+
limit := pagination.GetLimit()
3740

3841
if err := h.DB.Offset(offset).Limit(limit).Find(&tasks).Error; err != nil {
3942
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
4043
return
4144
}
42-
c.JSON(http.StatusOK, tasks)
45+
46+
// Calculate total rows and pages
47+
var totalRows int64
48+
h.DB.Model(&models.Task{}).Count(&totalRows)
49+
totalPages := int((totalRows + int64(limit) - 1) / int64(limit))
50+
51+
pagination.TotalRows = totalRows
52+
pagination.TotalPages = totalPages
53+
pagination.Rows = tasks
54+
55+
c.JSON(http.StatusOK, pagination)
4356
}
4457

4558
func (h *TaskHandler) GetTask(c *gin.Context) {

back_end/src/models/task.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ type Task struct {
1111
Name string `json:"name"`
1212
Description string `json:"description"`
1313
DueDate time.Time `json:"due_date"`
14-
CreateDate time.Time `json:"create_date"`
15-
Status string `json:"status"`
14+
Status string `json:"status" gorm:"-"`
1615
}

0 commit comments

Comments
 (0)