-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
65 lines (52 loc) · 1.55 KB
/
model.go
File metadata and controls
65 lines (52 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package database
import (
"math"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
)
// Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt
// It may be embedded into your model or you may build your own model without it
//
// type User struct {
// database.Model
// }
type Model struct {
ID int64 `json:"id" gorm:"primaryKey"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
DeletedAt soft_delete.DeletedAt `json:"deleted_at,omitempty" gorm:"index"`
}
const (
// DefaultPageSize pagination default items limit size
DefaultPageSize = 30
// MaxPageSize pagination default items max limit size
MaxPageSize = 1000
)
// Pagination model
type Pagination[T any] struct {
Page int `json:"page" form:"page" query:"page"`
PageSize int `json:"page_size" form:"page_size" query:"page_size"`
Total int64 `json:"total"`
Items []T `json:"items"`
}
func (p *Pagination[T]) TotalPages() int {
return int(math.Ceil(float64(p.Total) / float64(p.PageSize)))
}
// Paginate callback
func (p *Pagination[T]) Paginate(pageSize ...int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if p.Page <= 0 {
p.Page = 1
}
switch {
case len(pageSize) > 0:
p.PageSize = pageSize[0]
case p.PageSize > MaxPageSize:
p.PageSize = MaxPageSize
case p.PageSize <= 0:
p.PageSize = DefaultPageSize
}
offset := (p.Page - 1) * p.PageSize
return db.Offset(offset).Limit(p.PageSize)
}
}