2022-08-18 18:54:21 +08:00
package repo
import (
2022-10-17 16:32:31 +08:00
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
2022-08-29 18:44:35 +08:00
"gorm.io/gorm"
2022-08-18 18:54:21 +08:00
)
type HostRepo struct { }
type IHostRepo interface {
Get ( opts ... DBOption ) ( model . Host , error )
2022-08-30 18:49:07 +08:00
GetList ( opts ... DBOption ) ( [ ] model . Host , error )
2023-03-01 17:43:28 +08:00
Page ( limit , offset int , opts ... DBOption ) ( int64 , [ ] model . Host , error )
2022-08-29 18:44:35 +08:00
WithByInfo ( info string ) DBOption
2022-12-19 21:32:27 +08:00
WithByPort ( port uint ) DBOption
WithByUser ( user string ) DBOption
WithByAddr ( addr string ) DBOption
2022-08-18 18:54:21 +08:00
Create ( host * model . Host ) error
Update ( id uint , vars map [ string ] interface { } ) error
Delete ( opts ... DBOption ) error
2023-08-15 22:48:11 +08:00
GetFirewallRecord ( opts ... DBOption ) ( model . Firewall , error )
ListFirewallRecord ( ) ( [ ] model . Firewall , error )
SaveFirewallRecord ( firewall * model . Firewall ) error
DeleteFirewallRecordByID ( id uint ) error
2023-08-30 14:56:11 +08:00
DeleteFirewallRecord ( fType , port , protocol , address , strategy string ) error
2022-08-18 18:54:21 +08:00
}
2022-09-26 18:40:00 +08:00
func NewIHostRepo ( ) IHostRepo {
2022-08-18 18:54:21 +08:00
return & HostRepo { }
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) Get ( opts ... DBOption ) ( model . Host , error ) {
2022-08-18 18:54:21 +08:00
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 18:00:06 +08:00
func ( h * HostRepo ) GetList ( opts ... DBOption ) ( [ ] model . Host , error ) {
2022-08-18 18:54:21 +08:00
var hosts [ ] model . Host
db := global . DB . Model ( & model . Host { } )
for _ , opt := range opts {
db = opt ( db )
}
2022-08-30 18:49:07 +08:00
err := db . Find ( & hosts ) . Error
return hosts , err
2022-08-18 18:54:21 +08:00
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) Page ( page , size int , opts ... DBOption ) ( int64 , [ ] model . Host , error ) {
2023-03-01 17:43:28 +08:00
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 18:00:06 +08:00
func ( h * HostRepo ) WithByInfo ( info string ) DBOption {
2022-08-29 18:44:35 +08:00
return func ( g * gorm . DB ) * gorm . DB {
2022-08-30 18:49:07 +08:00
if len ( info ) == 0 {
return g
}
2022-08-29 18:44:35 +08:00
infoStr := "%" + info + "%"
return g . Where ( "name LIKE ? OR addr LIKE ?" , infoStr , infoStr )
}
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) WithByPort ( port uint ) DBOption {
2022-12-19 21:32:27 +08:00
return func ( g * gorm . DB ) * gorm . DB {
return g . Where ( "port = ?" , port )
}
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) WithByUser ( user string ) DBOption {
2022-12-19 21:32:27 +08:00
return func ( g * gorm . DB ) * gorm . DB {
return g . Where ( "user = ?" , user )
}
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) WithByAddr ( addr string ) DBOption {
2022-12-19 21:32:27 +08:00
return func ( g * gorm . DB ) * gorm . DB {
return g . Where ( "addr = ?" , addr )
}
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) WithByGroup ( group string ) DBOption {
2023-03-01 17:43:28 +08:00
return func ( g * gorm . DB ) * gorm . DB {
if len ( group ) == 0 {
return g
}
return g . Where ( "group_belong = ?" , group )
}
}
2022-12-19 21:32:27 +08:00
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) Create ( host * model . Host ) error {
2022-08-18 18:54:21 +08:00
return global . DB . Create ( host ) . Error
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) Update ( id uint , vars map [ string ] interface { } ) error {
2022-08-18 18:54:21 +08:00
return global . DB . Model ( & model . Host { } ) . Where ( "id = ?" , id ) . Updates ( vars ) . Error
}
2023-03-28 18:00:06 +08:00
func ( h * HostRepo ) Delete ( opts ... DBOption ) error {
2022-08-18 18:54:21 +08:00
db := global . DB
for _ , opt := range opts {
db = opt ( db )
}
return db . Delete ( & model . Host { } ) . Error
}
2023-08-15 22:48:11 +08:00
func ( h * HostRepo ) GetFirewallRecord ( opts ... DBOption ) ( model . Firewall , error ) {
var firewall model . Firewall
db := global . DB
for _ , opt := range opts {
db = opt ( db )
}
err := db . First ( & firewall ) . Error
return firewall , err
}
func ( h * HostRepo ) ListFirewallRecord ( ) ( [ ] model . Firewall , error ) {
var datas [ ] model . Firewall
if err := global . DB . Find ( & datas ) . Error ; err != nil {
return datas , nil
}
return datas , nil
}
func ( h * HostRepo ) SaveFirewallRecord ( firewall * model . Firewall ) error {
if firewall . ID != 0 {
return global . DB . Save ( firewall ) . Error
}
var data model . Firewall
2023-08-30 14:56:11 +08:00
if firewall . Type == "port" {
_ = global . DB . Where ( "type = ? AND port = ? AND protocol = ? AND address = ? AND strategy = ?" , "port" , firewall . Port , firewall . Protocol , firewall . Address , firewall . Strategy ) . First ( & data )
if data . ID != 0 {
firewall . ID = data . ID
}
} else {
_ = global . DB . Where ( "type = ? AND address = ? AND strategy = ?" , "address" , firewall . Address , firewall . Strategy ) . First ( & data )
if data . ID != 0 {
firewall . ID = data . ID
}
2023-08-15 22:48:11 +08:00
}
return global . DB . Save ( firewall ) . Error
}
func ( h * HostRepo ) DeleteFirewallRecordByID ( id uint ) error {
return global . DB . Where ( "id = ?" , id ) . Delete ( & model . Firewall { } ) . Error
}
2023-08-30 14:56:11 +08:00
func ( h * HostRepo ) DeleteFirewallRecord ( fType , port , protocol , address , strategy string ) error {
return global . DB . Where ( "type = ? AND port = ? AND protocol = ? AND address = ? AND strategy = ?" , fType , port , protocol , address , strategy ) . Delete ( & model . Firewall { } ) . Error
2023-08-15 22:48:11 +08:00
}