Skip to content

Commit aee367e

Browse files
committed
refactor(backend): move pagination calculation to crud package
- Move pagination logic to a shared crud interface - Update managers to use shared pagination struct
1 parent 4ccece1 commit aee367e

File tree

7 files changed

+48
-66
lines changed

7 files changed

+48
-66
lines changed

internal/collections/manager.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,12 @@ func (c *CollectionsManager) ListPaginated(ctx context.Context, limit, offset in
127127
entities[i] = CollectionEntity{Collection: collection}
128128
}
129129

130-
// Calculate pagination metadata
131-
totalPages := int((total + int64(limit) - 1) / int64(limit)) // Ceiling division
132-
currentPage := (offset / limit) + 1
133-
hasNext := (offset + limit) < int(total)
134-
hasPrev := offset > 0
130+
pagination := crud.CalculatePagination(total, limit, offset)
135131

136132
result := &PaginatedCollections{
137-
Collections: entities,
138-
Total: total,
139-
Offset: offset,
140-
Limit: limit,
141-
HasNext: hasNext,
142-
HasPrev: hasPrev,
143-
TotalPages: totalPages,
144-
CurrentPage: currentPage,
133+
Collections: entities,
134+
PaginationMetadata: pagination,
145135
}
146-
log.Info("retrieved collections", "count", len(entities), "total", total, "page", currentPage, "total_pages", totalPages)
136+
log.Info("retrieved collections", "count", len(entities), "total", pagination.Total, "page", pagination.CurrentPage, "total_pages", pagination.TotalPages)
147137
return result, nil
148138
}

internal/collections/models.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package collections
33
import (
44
"time"
55

6+
"github.com/maniac-en/req/internal/crud"
67
"github.com/maniac-en/req/internal/database"
78
"github.com/maniac-en/req/internal/log"
89
)
@@ -43,11 +44,5 @@ type CollectionsManager struct {
4344

4445
type PaginatedCollections struct {
4546
Collections []CollectionEntity `json:"collections"`
46-
Total int64 `json:"total"`
47-
HasNext bool `json:"has_next"`
48-
HasPrev bool `json:"has_prev"`
49-
Limit int `json:"limit"`
50-
Offset int `json:"offset"`
51-
TotalPages int `json:"total_pages"`
52-
CurrentPage int `json:"current_page"`
47+
crud.PaginationMetadata
5348
}

internal/crud/interfaces.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,30 @@ type Manager[T Entity] interface {
2626
Delete(ctx context.Context, id int64) error
2727
List(ctx context.Context) ([]T, error)
2828
}
29+
30+
type PaginationMetadata struct {
31+
Total int64 `json:"total"`
32+
HasNext bool `json:"has_next"`
33+
HasPrev bool `json:"has_prev"`
34+
Limit int `json:"limit"`
35+
Offset int `json:"offset"`
36+
TotalPages int `json:"total_pages"`
37+
CurrentPage int `json:"current_page"`
38+
}
39+
40+
func CalculatePagination(total int64, limit, offset int) PaginationMetadata {
41+
totalPages := int((total + int64(limit) - 1) / int64(limit)) // Ceiling division
42+
currentPage := (offset / limit) + 1
43+
hasNext := (offset + limit) < int(total)
44+
hasPrev := offset > 0
45+
46+
return PaginationMetadata{
47+
Total: total,
48+
HasNext: hasNext,
49+
HasPrev: hasPrev,
50+
Limit: limit,
51+
Offset: offset,
52+
TotalPages: totalPages,
53+
CurrentPage: currentPage,
54+
}
55+
}

internal/endpoints/manager.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,13 @@ func (e *EndpointsManager) ListByCollection(ctx context.Context, collectionID in
9393
entities[i] = EndpointEntity{Endpoint: endpoint}
9494
}
9595

96-
// Calculate pagination metadata
97-
totalPages := int((total + int64(limit) - 1) / int64(limit)) // Ceiling division
98-
currentPage := (offset / limit) + 1
99-
hasNext := (offset + limit) < int(total)
100-
hasPrev := offset > 0
96+
pagination := crud.CalculatePagination(total, limit, offset)
10197

10298
result := &PaginatedEndpoints{
103-
Endpoints: entities,
104-
Total: total,
105-
Offset: offset,
106-
Limit: limit,
107-
HasNext: hasNext,
108-
HasPrev: hasPrev,
109-
TotalPages: totalPages,
110-
CurrentPage: currentPage,
111-
}
112-
log.Info("retrieved endpoints", "collection_id", collectionID, "count", len(entities), "total", total, "page", currentPage, "total_pages", totalPages)
99+
Endpoints: entities,
100+
PaginationMetadata: pagination,
101+
}
102+
log.Info("retrieved endpoints", "collection_id", collectionID, "count", len(entities), "total", pagination.Total, "page", pagination.CurrentPage, "total_pages", pagination.TotalPages)
113103
return result, nil
114104
}
115105

internal/endpoints/models.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package endpoints
33
import (
44
"time"
55

6+
"github.com/maniac-en/req/internal/crud"
67
"github.com/maniac-en/req/internal/database"
78
"github.com/maniac-en/req/internal/log"
89
)
@@ -42,14 +43,8 @@ type EndpointsManager struct {
4243
}
4344

4445
type PaginatedEndpoints struct {
45-
Endpoints []EndpointEntity `json:"endpoints"`
46-
Total int64 `json:"total"`
47-
Offset int `json:"offset"`
48-
Limit int `json:"limit"`
49-
HasNext bool `json:"has_next"`
50-
HasPrev bool `json:"has_prev"`
51-
TotalPages int `json:"total_pages"`
52-
CurrentPage int `json:"current_page"`
46+
Endpoints []EndpointEntity `json:"endpoints"`
47+
crud.PaginationMetadata
5348
}
5449

5550
type EndpointData struct {

internal/history/manager.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,14 @@ func (h *HistoryManager) ListByCollection(ctx context.Context, collectionID int6
9494
}}
9595
}
9696

97-
// Calculate pagination metadata
98-
totalPages := int((total + int64(limit) - 1) / int64(limit)) // Ceiling division
99-
currentPage := (offset / limit) + 1
100-
hasNext := (offset + limit) < int(total)
101-
hasPrev := offset > 0
97+
pagination := crud.CalculatePagination(total, limit, offset)
10298

10399
result := PaginatedHistory{
104-
Items: entities,
105-
Total: total,
106-
HasNext: hasNext,
107-
HasPrev: hasPrev,
108-
Limit: limit,
109-
Offset: offset,
110-
TotalPages: totalPages,
111-
CurrentPage: currentPage,
100+
Items: entities,
101+
PaginationMetadata: pagination,
112102
}
113103

114-
log.Info("listed history by collection", "collection_id", collectionID, "count", len(entities), "total", total, "page", currentPage, "total_pages", totalPages)
104+
log.Info("listed history by collection", "collection_id", collectionID, "count", len(entities), "total", pagination.Total, "page", pagination.CurrentPage, "total_pages", pagination.TotalPages)
115105
return result, nil
116106
}
117107

internal/history/models.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package history
44
import (
55
"time"
66

7+
"github.com/maniac-en/req/internal/crud"
78
"github.com/maniac-en/req/internal/database"
89
"github.com/maniac-en/req/internal/log"
910
)
@@ -39,14 +40,8 @@ func (h HistoryEntity) GetUpdatedAt() time.Time {
3940
}
4041

4142
type PaginatedHistory struct {
42-
Items []HistoryEntity `json:"items"`
43-
Total int64 `json:"total"`
44-
HasNext bool `json:"has_next"`
45-
HasPrev bool `json:"has_prev"`
46-
Limit int `json:"limit"`
47-
Offset int `json:"offset"`
48-
TotalPages int `json:"total_pages"`
49-
CurrentPage int `json:"current_page"`
43+
Items []HistoryEntity `json:"items"`
44+
crud.PaginationMetadata
5045
}
5146

5247
type ExecutionData struct {

0 commit comments

Comments
 (0)