store friendly

This commit is contained in:
Sense T
2024-04-07 13:08:30 +08:00
parent d90e949472
commit 156bf651dd
4 changed files with 23 additions and 11 deletions

View File

@@ -10,13 +10,13 @@ import (
"gorm.io/gorm"
)
func (c *Controller) CreateDomain(d *models.Domain) error {
func (c *Controller) CreateDomain(d *models.Domain) (*models.Domain, error) {
nss, err := c.GetDNS()
if err != nil {
return err
return nil, err
}
return c.DB.Transaction(func(tx *gorm.DB) error {
if err := c.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(d).Error; err != nil {
return err
}
@@ -49,7 +49,11 @@ func (c *Controller) CreateDomain(d *models.Domain) error {
}
return nil
})
}); err != nil {
return nil, err
}
return d, err
}
func (c *Controller) GetDomains(domain string) ([]models.Domain, error) {

View File

@@ -7,21 +7,25 @@ import (
"gorm.io/gorm"
)
func (c *Controller) CreateRecord(r *models.Record) error {
func (c *Controller) CreateRecord(r *models.Record) (*models.Record, error) {
if r.RecordType != models.RecordTypeSOA {
domains, err := c.GetDomains(r.Zone)
if err != nil {
return err
return nil, err
}
if len(domains) == 0 || domains[0].DomainName == r.Zone {
return fmt.Errorf("no such domain")
return nil, fmt.Errorf("no such domain")
}
}
return c.DB.Transaction(func(tx *gorm.DB) error {
if err := c.DB.Transaction(func(tx *gorm.DB) error {
return tx.Create(r).Error
})
}); err != nil {
return nil, err
}
return r, nil
}
func (c *Controller) CreateRecords(rs []*models.Record) error {