Compare commits

..

No commits in common. "main" and "v1.0.18" have entirely different histories.

1 changed files with 8 additions and 21 deletions

View File

@ -14,11 +14,7 @@ func GetTotalPages(database *gorm.DB, paginationLimit int, any interface{}, wher
if whereQuery == nil {
database.Model(any).
Count(&totalPages)
} else if len(args) == 0 { // here used with db.Where(&User{Name: "jinzhu", Age: 20}).First(&user)
database.Model(any).
Where(whereQuery).
Count(&totalPages)
} else { // here used with "name = ?", "jinzhu"
} else {
database.Model(any).
Where(whereQuery, args).
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 {
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).
Offset(GetPageOffset(query.Page, paginationLimit)).
Where(whereQuery, args).
@ -63,6 +43,13 @@ func DbPageQuery(database *gorm.DB, query PageQuery, paginationLimit int, 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
// can be used like this: robots[start:end]
func GetPage(lenList int, page int, limit int) (start int, end int) {