Initial commit

This commit is contained in:
yan.y
2024-03-27 23:25:08 +08:00
commit 0884384e91
127 changed files with 9353 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package scope
import "gorm.io/gorm"
// IdScope
// - id uint
func IdScope(id uint) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("id = ?", id)
}
}
// InIdsScope
// - ids []uint
func InIdsScope(ids []uint) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("id in ?", ids)
}
}
// InNamesScope
// - names []string
func InNamesScope(names []string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("name in ?", names)
}
}
// InUuidsScope
// - uuids []string
func InUuidsScope(uuids []string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("uuid in ?", uuids)
}
}
// NeIdScope
// - id uint
func NeIdScope(id uint) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("id != ?", id)
}
}
+55
View File
@@ -0,0 +1,55 @@
package scope
import (
"gorm.io/gorm"
"pet-house.com/core/helper/str"
)
// PaginateScope return paginate scope for gorm
// - page int
// - pageSize int
// - sort string
// - orderBy string
func PaginateScope(page, pageSize int, sort, orderBy string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
pageSize := getPageSize(pageSize)
offset := getOffset(page, pageSize)
return db.Order(getOrderBy(sort, orderBy)).Offset(offset).Limit(pageSize)
}
}
// getOffset
func getOffset(page, pageSize int) int {
if page == 0 {
page = 1
}
offset := (page - 1) * pageSize
if page < 0 {
offset = -1
}
return offset
}
// getPageSize
func getPageSize(pageSize int) int {
switch {
case pageSize > 100:
pageSize = 100
case pageSize < 0:
pageSize = -1
case pageSize == 0:
pageSize = 10
}
return pageSize
}
// getOrderBy
func getOrderBy(sort, orderBy string) string {
if sort == "" {
sort = "desc"
}
if orderBy == "" {
orderBy = "created_at"
}
return str.Join(orderBy, " ", sort)
}