Files
EdgeAPI/internal/db/models/dns_domain_dao.go

198 lines
4.3 KiB
Go
Raw Normal View History

2020-11-12 14:41:28 +08:00
package models
import (
"encoding/json"
2020-11-15 11:57:49 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
2020-11-12 14:41:28 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
2020-11-14 21:28:07 +08:00
"time"
2020-11-12 14:41:28 +08:00
)
const (
DNSDomainStateEnabled = 1 // 已启用
DNSDomainStateDisabled = 0 // 已禁用
)
type DNSDomainDAO dbs.DAO
func NewDNSDomainDAO() *DNSDomainDAO {
return dbs.NewDAO(&DNSDomainDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeDNSDomains",
Model: new(DNSDomain),
PkName: "id",
},
}).(*DNSDomainDAO)
}
var SharedDNSDomainDAO *DNSDomainDAO
func init() {
dbs.OnReady(func() {
SharedDNSDomainDAO = NewDNSDomainDAO()
})
}
// 启用条目
func (this *DNSDomainDAO) EnableDNSDomain(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", DNSDomainStateEnabled).
Update()
return err
}
// 禁用条目
func (this *DNSDomainDAO) DisableDNSDomain(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", DNSDomainStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *DNSDomainDAO) FindEnabledDNSDomain(id int64) (*DNSDomain, error) {
result, err := this.Query().
Pk(id).
Attr("state", DNSDomainStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*DNSDomain), err
}
// 根据主键查找名称
func (this *DNSDomainDAO) FindDNSDomainName(id int64) (string, error) {
return this.Query().
Pk(id).
Result("name").
FindStringCol("")
}
// 创建域名
func (this *DNSDomainDAO) CreateDomain(providerId int64, name string) (int64, error) {
op := NewDNSDomainOperator()
op.ProviderId = providerId
op.Name = name
op.State = DNSDomainStateEnabled
op.IsOn = true
_, err := this.Save(op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
// 修改域名
func (this *DNSDomainDAO) UpdateDomain(domainId int64, name string, isOn bool) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
op := NewDNSDomainOperator()
op.Id = domainId
op.Name = name
op.IsOn = isOn
_, err := this.Save(op)
if err != nil {
return err
}
return nil
}
// 查询一个服务商下面的所有域名
func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(providerId int64) (result []*DNSDomain, err error) {
_, err = this.Query().
State(DNSDomainStateEnabled).
Attr("providerId", providerId).
AscPk().
Slice(&result).
FindAll()
return
}
// 计算某个服务商下的域名数量
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(providerId int64) (int64, error) {
return this.Query().
State(DNSDomainStateEnabled).
Attr("providerId", providerId).
Count()
}
// 更新域名数据
func (this *DNSDomainDAO) UpdateDomainData(domainId int64, data string) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
op := NewDNSDomainOperator()
op.Id = domainId
op.Data = data
_, err := this.Save(op)
return err
}
// 更新域名解析记录
func (this *DNSDomainDAO) UpdateDomainRecords(domainId int64, recordsJSON []byte) error {
2020-11-12 14:41:28 +08:00
if domainId <= 0 {
return errors.New("invalid domainId")
}
op := NewDNSDomainOperator()
op.Id = domainId
op.Records = recordsJSON
2020-11-14 21:28:07 +08:00
op.DataUpdatedAt = time.Now().Unix()
2020-11-12 14:41:28 +08:00
_, err := this.Save(op)
return err
}
// 更新线路
func (this *DNSDomainDAO) UpdateDomainRoutes(domainId int64, routesJSON []byte) error {
2020-11-12 14:41:28 +08:00
if domainId <= 0 {
return errors.New("invalid domainId")
}
op := NewDNSDomainOperator()
op.Id = domainId
op.Routes = routesJSON
2020-11-14 21:28:07 +08:00
op.DataUpdatedAt = time.Now().Unix()
2020-11-12 14:41:28 +08:00
_, err := this.Save(op)
return err
}
// 查找域名线路
2020-11-15 11:57:49 +08:00
func (this *DNSDomainDAO) FindDomainRoutes(domainId int64) ([]*dnsclients.Route, error) {
routes, err := this.Query().
Pk(domainId).
Result("routes").
FindStringCol("")
if err != nil {
return nil, err
2020-11-12 14:41:28 +08:00
}
if len(routes) == 0 || routes == "null" {
return nil, nil
}
2020-11-15 11:57:49 +08:00
result := []*dnsclients.Route{}
err = json.Unmarshal([]byte(routes), &result)
if err != nil {
return nil, err
}
return result, nil
2020-11-12 14:41:28 +08:00
}
2020-11-15 11:57:49 +08:00
// 查找线路名称
func (this *DNSDomainDAO) FindDomainRouteName(domainId int64, routeCode string) (string, error) {
routes, err := this.FindDomainRoutes(domainId)
if err != nil {
return "", err
}
for _, route := range routes {
if route.Code == routeCode {
return route.Name, nil
}
}
return "", nil
}