36 lines
538 B
Go
36 lines
538 B
Go
package storage
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
type Scoper interface {
|
|
Scope(db *gorm.DB) *gorm.DB
|
|
}
|
|
|
|
type PageQuery struct {
|
|
Page int `query:"page"`
|
|
Size int `query:"size"`
|
|
}
|
|
|
|
func (p *PageQuery) Scope(db *gorm.DB) *gorm.DB {
|
|
if p.Size <= 0 {
|
|
p.Size = 10
|
|
}
|
|
|
|
if p.Page <= 0 {
|
|
p.Page = 1
|
|
}
|
|
return db.Offset((p.Page - 1) * p.Size).Limit(p.Size)
|
|
}
|
|
|
|
type PageListQuery struct {
|
|
Scoper
|
|
PageQuery
|
|
}
|
|
|
|
func (p *PageListQuery) Scope(db *gorm.DB) *gorm.DB {
|
|
if p.Scoper != nil {
|
|
db = p.Scoper.Scope(db)
|
|
}
|
|
return p.PageQuery.Scope(db)
|
|
}
|