124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
package models
|
|
|
|
import (
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"github.com/iwind/TeaGo/types"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
HTTPDNSDomainStateEnabled = 1
|
|
HTTPDNSDomainStateDisabled = 0
|
|
)
|
|
|
|
type HTTPDNSDomainDAO dbs.DAO
|
|
|
|
func NewHTTPDNSDomainDAO() *HTTPDNSDomainDAO {
|
|
return dbs.NewDAO(&HTTPDNSDomainDAO{
|
|
DAOObject: dbs.DAOObject{
|
|
DB: Tea.Env,
|
|
Table: "edgeHTTPDNSDomains",
|
|
Model: new(HTTPDNSDomain),
|
|
PkName: "id",
|
|
},
|
|
}).(*HTTPDNSDomainDAO)
|
|
}
|
|
|
|
var SharedHTTPDNSDomainDAO *HTTPDNSDomainDAO
|
|
|
|
func init() {
|
|
dbs.OnReady(func() {
|
|
SharedHTTPDNSDomainDAO = NewHTTPDNSDomainDAO()
|
|
})
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) CreateDomain(tx *dbs.Tx, appDbId int64, domain string, isOn bool) (int64, error) {
|
|
domain = strings.ToLower(strings.TrimSpace(domain))
|
|
var op = NewHTTPDNSDomainOperator()
|
|
op.AppId = appDbId
|
|
op.Domain = domain
|
|
op.IsOn = isOn
|
|
op.CreatedAt = time.Now().Unix()
|
|
op.UpdatedAt = time.Now().Unix()
|
|
op.State = HTTPDNSDomainStateEnabled
|
|
err := this.Save(tx, op)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return types.Int64(op.Id), nil
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) DisableDomain(tx *dbs.Tx, domainId int64) error {
|
|
_, err := this.Query(tx).
|
|
Pk(domainId).
|
|
Set("state", HTTPDNSDomainStateDisabled).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) DisableDomainsWithAppId(tx *dbs.Tx, appDbId int64) error {
|
|
_, err := this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSDomainStateEnabled).
|
|
Set("state", HTTPDNSDomainStateDisabled).
|
|
Set("updatedAt", time.Now().Unix()).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) UpdateDomainStatus(tx *dbs.Tx, domainId int64, isOn bool) error {
|
|
_, err := this.Query(tx).
|
|
Pk(domainId).
|
|
State(HTTPDNSDomainStateEnabled).
|
|
Set("isOn", isOn).
|
|
Set("updatedAt", time.Now().Unix()).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) FindEnabledDomain(tx *dbs.Tx, domainId int64) (*HTTPDNSDomain, error) {
|
|
one, err := this.Query(tx).
|
|
Pk(domainId).
|
|
State(HTTPDNSDomainStateEnabled).
|
|
Find()
|
|
if one == nil {
|
|
return nil, err
|
|
}
|
|
return one.(*HTTPDNSDomain), nil
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) FindEnabledDomainWithAppAndName(tx *dbs.Tx, appDbId int64, domain string) (*HTTPDNSDomain, error) {
|
|
one, err := this.Query(tx).
|
|
State(HTTPDNSDomainStateEnabled).
|
|
Attr("appId", appDbId).
|
|
Attr("domain", strings.ToLower(strings.TrimSpace(domain))).
|
|
Find()
|
|
if one == nil {
|
|
return nil, err
|
|
}
|
|
return one.(*HTTPDNSDomain), nil
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) ListEnabledDomainsWithAppId(tx *dbs.Tx, appDbId int64, keyword string) (result []*HTTPDNSDomain, err error) {
|
|
query := this.Query(tx).
|
|
State(HTTPDNSDomainStateEnabled).
|
|
Attr("appId", appDbId).
|
|
AscPk()
|
|
if len(keyword) > 0 {
|
|
query = query.Where("domain LIKE :kw").Param("kw", "%"+keyword+"%")
|
|
}
|
|
_, err = query.Slice(&result).FindAll()
|
|
return
|
|
}
|
|
|
|
func (this *HTTPDNSDomainDAO) CountEnabledDomains(tx *dbs.Tx, appDbId int64) (int64, error) {
|
|
query := this.Query(tx).State(HTTPDNSDomainStateEnabled)
|
|
if appDbId > 0 {
|
|
query = query.Attr("appId", appDbId)
|
|
}
|
|
return query.Count()
|
|
}
|