1Panel/backend/app/repo/common.go

52 lines
1.1 KiB
Go
Raw Normal View History

2022-08-16 23:30:23 +08:00
package repo
import "gorm.io/gorm"
type DBOption func(*gorm.DB) *gorm.DB
type ICommonRepo interface {
WithByID(id uint) DBOption
WithByName(name string) DBOption
WithOrderBy(orderStr string) DBOption
WithLikeName(name string) DBOption
WithIdsIn(ids []uint) DBOption
}
type CommonRepo struct{}
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 {
return g.Where("name = ?", name)
}
}
2022-09-19 19:42:06 +08:00
func (c *CommonRepo) WithByType(name string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("type = ?", name)
}
}
2022-08-16 23:30:23 +08:00
func (c *CommonRepo) WithLikeName(name string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("name like ?", "%"+name+"%")
}
}
func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Order(orderStr)
}
}
func (c *CommonRepo) WithIdsIn(ids []uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
}
}