1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-20 16:59:17 +08:00
1Panel/core/app/repo/common.go

96 lines
2.2 KiB
Go
Raw Normal View History

2024-07-19 19:04:11 +08:00
package repo
import (
"fmt"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global"
2024-07-19 19:04:11 +08:00
"gorm.io/gorm"
)
type ICommonRepo interface {
WithByID(id uint) global.DBOption
WithByGroupID(id uint) global.DBOption
WithByName(name string) global.DBOption
WithByType(ty string) global.DBOption
WithByKey(key string) global.DBOption
WithOrderBy(orderStr string) global.DBOption
WithByStatus(status string) global.DBOption
WithByGroupBelong(group string) global.DBOption
WithByIDs(ids []uint) global.DBOption
WithOrderRuleBy(orderBy, order string) global.DBOption
2024-07-19 19:04:11 +08:00
}
type CommonRepo struct{}
2024-07-25 14:43:41 +08:00
func NewICommonRepo() ICommonRepo {
2024-07-19 19:04:11 +08:00
return &CommonRepo{}
}
2024-07-25 14:43:41 +08:00
func (c *CommonRepo) WithByID(id uint) global.DBOption {
2024-07-25 14:43:41 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("id = ?", id)
}
}
func (c *CommonRepo) WithByGroupID(id uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("group_id = ?", id)
}
}
func (c *CommonRepo) WithByIDs(ids []uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
}
}
func (c *CommonRepo) WithByName(name string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`name` = ?", name)
}
}
func (c *CommonRepo) WithByType(ty string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`type` = ?", ty)
}
}
func (c *CommonRepo) WithByKey(key string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("key = ?", key)
}
}
func (c *CommonRepo) WithByStatus(status string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("status = ?", status)
}
}
func (c *CommonRepo) WithByGroupBelong(group string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("group_belong = ?", group)
}
}
func (c *CommonRepo) WithOrderBy(orderStr string) global.DBOption {
2024-07-19 19:04:11 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Order(orderStr)
}
}
func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) global.DBOption {
switch order {
case constant.OrderDesc:
order = "desc"
case constant.OrderAsc:
order = "asc"
default:
orderBy = "created_at"
order = "desc"
}
return func(g *gorm.DB) *gorm.DB {
return g.Order(fmt.Sprintf("%s %s", orderBy, order))
}
}