Compare commits
No commits in common. "main" and "v1.0.18" have entirely different histories.
|
@ -14,11 +14,7 @@ func GetTotalPages(database *gorm.DB, paginationLimit int, any interface{}, wher
|
||||||
if whereQuery == nil {
|
if whereQuery == nil {
|
||||||
database.Model(any).
|
database.Model(any).
|
||||||
Count(&totalPages)
|
Count(&totalPages)
|
||||||
} else if len(args) == 0 { // here used with db.Where(&User{Name: "jinzhu", Age: 20}).First(&user)
|
} else {
|
||||||
database.Model(any).
|
|
||||||
Where(whereQuery).
|
|
||||||
Count(&totalPages)
|
|
||||||
} else { // here used with "name = ?", "jinzhu"
|
|
||||||
database.Model(any).
|
database.Model(any).
|
||||||
Where(whereQuery, args).
|
Where(whereQuery, args).
|
||||||
Count(&totalPages)
|
Count(&totalPages)
|
||||||
|
@ -40,22 +36,6 @@ type PageQuery struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DbPageQuery(database *gorm.DB, query PageQuery, paginationLimit int, result any, orderBy string, whereQuery interface{}, args ...interface{}) *gorm.DB {
|
func DbPageQuery(database *gorm.DB, query PageQuery, paginationLimit int, result any, orderBy string, whereQuery interface{}, args ...interface{}) *gorm.DB {
|
||||||
if whereQuery == nil {
|
|
||||||
return database.Limit(paginationLimit).
|
|
||||||
Offset(GetPageOffset(query.Page, paginationLimit)).
|
|
||||||
Order(orderBy).
|
|
||||||
Find(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) == 0 { // here used with db.Where(&User{Name: "jinzhu", Age: 20}).First(&user)
|
|
||||||
return database.Limit(paginationLimit).
|
|
||||||
Offset(GetPageOffset(query.Page, paginationLimit)).
|
|
||||||
Where(whereQuery).
|
|
||||||
Order(orderBy).
|
|
||||||
Find(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// here used with "name = ?", "jinzhu"
|
|
||||||
return database.Limit(paginationLimit).
|
return database.Limit(paginationLimit).
|
||||||
Offset(GetPageOffset(query.Page, paginationLimit)).
|
Offset(GetPageOffset(query.Page, paginationLimit)).
|
||||||
Where(whereQuery, args).
|
Where(whereQuery, args).
|
||||||
|
@ -63,6 +43,13 @@ func DbPageQuery(database *gorm.DB, query PageQuery, paginationLimit int, result
|
||||||
Find(result)
|
Find(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DbPageQueryWithoutWhere(database *gorm.DB, query PageQuery, paginationLimit int, result any, orderBy string) *gorm.DB {
|
||||||
|
return database.Limit(paginationLimit).
|
||||||
|
Offset(GetPageOffset(query.Page, paginationLimit)).
|
||||||
|
Order(orderBy).
|
||||||
|
Find(result)
|
||||||
|
}
|
||||||
|
|
||||||
// GetPage returns start and end for pagination
|
// GetPage returns start and end for pagination
|
||||||
// can be used like this: robots[start:end]
|
// can be used like this: robots[start:end]
|
||||||
func GetPage(lenList int, page int, limit int) (start int, end int) {
|
func GetPage(lenList int, page int, limit int) (start int, end int) {
|
||||||
|
|
Loading…
Reference in New Issue