mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-12-20 12:27:50 +08:00
48 lines
922 B
Go
48 lines
922 B
Go
package repo
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type DBOption func(*gorm.DB) *gorm.DB
|
|
|
|
type ICommonRepo interface {
|
|
WithByID(id uint) DBOption
|
|
WithByName(name string) DBOption
|
|
WithByType(ty string) DBOption
|
|
WithOrderBy(orderStr string) DBOption
|
|
}
|
|
|
|
type CommonRepo struct{}
|
|
|
|
func NewICommonRepo() ICommonRepo {
|
|
return &CommonRepo{}
|
|
}
|
|
|
|
func (c *CommonRepo) WithByID(id uint) DBOption {
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
return g.Where("id = ?", id)
|
|
}
|
|
}
|
|
func (c *CommonRepo) WithByName(name string) DBOption {
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
if len(name) == 0 {
|
|
return g
|
|
}
|
|
return g.Where("`name` = ?", name)
|
|
}
|
|
}
|
|
func (c *CommonRepo) WithByType(ty string) DBOption {
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
if len(ty) == 0 {
|
|
return g
|
|
}
|
|
return g.Where("`type` = ?", ty)
|
|
}
|
|
}
|
|
func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
return g.Order(orderStr)
|
|
}
|
|
}
|