2024-03-07 17:07:41 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"wireguard-dashboard/client"
|
2024-03-08 16:30:29 +08:00
|
|
|
"wireguard-dashboard/http/param"
|
2024-03-07 17:07:41 +08:00
|
|
|
"wireguard-dashboard/model/entity"
|
|
|
|
"wireguard-dashboard/model/vo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
*gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func Server() server {
|
|
|
|
return server{
|
|
|
|
client.DB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServer
|
|
|
|
// @description: 获取服务端信息
|
|
|
|
// @receiver r
|
|
|
|
// @return data
|
|
|
|
// @return err
|
|
|
|
func (r server) GetServer() (data *vo.Server, err error) {
|
2024-05-24 15:19:26 +08:00
|
|
|
err = r.Model(&entity.Server{}).Select("id", "ip_scope", "listen_port", "private_key", "public_key", "post_up_script", "pre_down_script", "post_down_script").First(&data).Error
|
2024-03-07 17:07:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 11:30:36 +08:00
|
|
|
// GetServerWithClient
|
|
|
|
// @description: 获取服务端信息以及所属客户端
|
|
|
|
// @receiver r
|
|
|
|
// @param data
|
|
|
|
// @param err
|
|
|
|
func (r server) GetServerWithClient(id string) (data *entity.Server, err error) {
|
|
|
|
err = r.Model(&entity.Server{}).Preload("Clients").Preload("Clients.User").Where("id = ?", id).First(&data).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-07 17:07:41 +08:00
|
|
|
// Save
|
2024-03-08 16:30:29 +08:00
|
|
|
// @description: 新增服务端信息
|
2024-03-07 17:07:41 +08:00
|
|
|
// @receiver r
|
|
|
|
// @param ent
|
|
|
|
// @return err
|
|
|
|
func (r server) Save(ent *entity.Server) (err error) {
|
|
|
|
return r.Model(&entity.Server{}).Create(&ent).Error
|
|
|
|
}
|
2024-03-08 16:30:29 +08:00
|
|
|
|
|
|
|
// Update
|
|
|
|
// @description: 更新服务端信息
|
|
|
|
// @receiver r
|
|
|
|
// @param p
|
|
|
|
// @return err
|
|
|
|
func (r server) Update(p param.SaveServer) (err error) {
|
|
|
|
update := map[string]any{
|
|
|
|
"ip_scope": p.IpScope,
|
|
|
|
"listen_port": p.ListenPort,
|
|
|
|
"post_up_script": p.PostUpScript,
|
|
|
|
"pre_down_script": p.PreDownScript,
|
|
|
|
"post_down_script": p.PostDownScript,
|
2024-06-07 11:59:44 +08:00
|
|
|
"private_key": p.PrivateKey,
|
|
|
|
"public_key": p.PublicKey,
|
2024-03-08 16:30:29 +08:00
|
|
|
}
|
|
|
|
return r.Model(&entity.Server{}).Where("id = ?", p.Id).Updates(&update).Error
|
|
|
|
}
|