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

109 lines
2.6 KiB
Go
Raw Normal View History

package repo
import (
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
2022-08-29 10:44:35 +00:00
"gorm.io/gorm"
)
type HostRepo struct{}
type IHostRepo interface {
Get(opts ...DBOption) (model.Host, error)
GetList(opts ...DBOption) ([]model.Host, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.Host, error)
2022-08-29 10:44:35 +00:00
WithByInfo(info string) DBOption
2022-12-19 13:32:27 +00:00
WithByPort(port uint) DBOption
WithByUser(user string) DBOption
WithByAddr(addr string) DBOption
Create(host *model.Host) error
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
}
func NewIHostRepo() IHostRepo {
return &HostRepo{}
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) Get(opts ...DBOption) (model.Host, error) {
var host model.Host
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.First(&host).Error
return host, err
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) GetList(opts ...DBOption) ([]model.Host, error) {
var hosts []model.Host
db := global.DB.Model(&model.Host{})
for _, opt := range opts {
db = opt(db)
}
err := db.Find(&hosts).Error
return hosts, err
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) Page(page, size int, opts ...DBOption) (int64, []model.Host, error) {
var users []model.Host
db := global.DB.Model(&model.Host{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&users).Error
return count, users, err
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) WithByInfo(info string) DBOption {
2022-08-29 10:44:35 +00:00
return func(g *gorm.DB) *gorm.DB {
if len(info) == 0 {
return g
}
2022-08-29 10:44:35 +00:00
infoStr := "%" + info + "%"
return g.Where("name LIKE ? OR addr LIKE ?", infoStr, infoStr)
}
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) WithByPort(port uint) DBOption {
2022-12-19 13:32:27 +00:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("port = ?", port)
}
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) WithByUser(user string) DBOption {
2022-12-19 13:32:27 +00:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("user = ?", user)
}
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) WithByAddr(addr string) DBOption {
2022-12-19 13:32:27 +00:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("addr = ?", addr)
}
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) WithByGroup(group string) DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(group) == 0 {
return g
}
return g.Where("group_belong = ?", group)
}
}
2022-12-19 13:32:27 +00:00
2023-03-28 10:00:06 +00:00
func (h *HostRepo) Create(host *model.Host) error {
return global.DB.Create(host).Error
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) Update(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.Host{}).Where("id = ?", id).Updates(vars).Error
}
2023-03-28 10:00:06 +00:00
func (h *HostRepo) Delete(opts ...DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
}
return db.Delete(&model.Host{}).Error
}