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 {

View File

@ -31,13 +31,15 @@ func (s *Server) createDomain(c *gin.Context) {
return
}
if err := s.controller.CreateDomain(domain); err != nil {
domain, err := s.controller.CreateDomain(domain)
if err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusCreated, Response{
Succeed: true,
Data: domain,
})
}

View File

@ -50,13 +50,15 @@ func (s *Server) createRecord(c *gin.Context) {
return
}
if err := s.controller.CreateRecord(record); err != nil {
record, err := s.controller.CreateRecord(record)
if err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusCreated, Response{
Succeed: true,
Data: record,
})
}