2022-08-18 10:54:21 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-10-17 08:32:31 +00:00
|
|
|
"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"
|
2022-08-18 10:54:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HostRepo struct{}
|
|
|
|
|
|
|
|
type IHostRepo interface {
|
|
|
|
Get(opts ...DBOption) (model.Host, error)
|
2022-08-30 10:49:07 +00:00
|
|
|
GetList(opts ...DBOption) ([]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
|
2022-08-18 10:54:21 +00:00
|
|
|
Create(host *model.Host) error
|
2022-08-31 15:16:10 +00:00
|
|
|
ChangeGroup(oldGroup, newGroup string) error
|
2022-08-18 10:54:21 +00:00
|
|
|
Update(id uint, vars map[string]interface{}) error
|
|
|
|
Delete(opts ...DBOption) error
|
|
|
|
}
|
|
|
|
|
2022-09-26 10:40:00 +00:00
|
|
|
func NewIHostRepo() IHostRepo {
|
2022-08-18 10:54:21 +00:00
|
|
|
return &HostRepo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *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
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:49:07 +00:00
|
|
|
func (u *HostRepo) GetList(opts ...DBOption) ([]model.Host, error) {
|
2022-08-18 10:54:21 +00:00
|
|
|
var hosts []model.Host
|
|
|
|
db := global.DB.Model(&model.Host{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
2022-08-30 10:49:07 +00:00
|
|
|
err := db.Find(&hosts).Error
|
|
|
|
return hosts, err
|
2022-08-18 10:54:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 10:44:35 +00:00
|
|
|
func (c *HostRepo) WithByInfo(info string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2022-08-30 10:49:07 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 13:32:27 +00:00
|
|
|
func (u *HostRepo) WithByPort(port uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("port = ?", port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (u *HostRepo) WithByUser(user string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("user = ?", user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (u *HostRepo) WithByAddr(addr string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("addr = ?", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:54:21 +00:00
|
|
|
func (u *HostRepo) Create(host *model.Host) error {
|
|
|
|
return global.DB.Create(host).Error
|
|
|
|
}
|
|
|
|
|
2022-08-31 15:16:10 +00:00
|
|
|
func (u *HostRepo) ChangeGroup(oldGroup, newGroup string) error {
|
|
|
|
return global.DB.Model(&model.Host{}).Where("group_belong = ?", oldGroup).Updates(map[string]interface{}{"group_belong": newGroup}).Error
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:54:21 +00:00
|
|
|
func (u *HostRepo) Update(id uint, vars map[string]interface{}) error {
|
|
|
|
return global.DB.Model(&model.Host{}).Where("id = ?", id).Updates(vars).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *HostRepo) Delete(opts ...DBOption) error {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Delete(&model.Host{}).Error
|
|
|
|
}
|