wireguard-dashboard/repository/server.go
2024-05-24 15:19:26 +08:00

65 lines
1.6 KiB
Go

package repository
import (
"gorm.io/gorm"
"wireguard-dashboard/client"
"wireguard-dashboard/http/param"
"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) {
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
return
}
// 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
}
// Save
// @description: 新增服务端信息
// @receiver r
// @param ent
// @return err
func (r server) Save(ent *entity.Server) (err error) {
return r.Model(&entity.Server{}).Create(&ent).Error
}
// 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,
}
return r.Model(&entity.Server{}).Where("id = ?", p.Id).Updates(&update).Error
}