From 156bf651dd8b770a277eaa7087a2364467931001 Mon Sep 17 00:00:00 2001 From: Sense T Date: Sun, 7 Apr 2024 13:08:30 +0800 Subject: [PATCH] store friendly --- controllers/domain.go | 12 ++++++++---- controllers/record.go | 14 +++++++++----- server/handlers_domains.go | 4 +++- server/handlers_records.go | 4 +++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/controllers/domain.go b/controllers/domain.go index 6c2878b..be1d321 100644 --- a/controllers/domain.go +++ b/controllers/domain.go @@ -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) { diff --git a/controllers/record.go b/controllers/record.go index ea650cd..3e6f5e0 100644 --- a/controllers/record.go +++ b/controllers/record.go @@ -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 { diff --git a/server/handlers_domains.go b/server/handlers_domains.go index 9f4fb01..af1758c 100644 --- a/server/handlers_domains.go +++ b/server/handlers_domains.go @@ -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, }) } diff --git a/server/handlers_records.go b/server/handlers_records.go index 8ba795e..ad5c855 100644 --- a/server/handlers_records.go +++ b/server/handlers_records.go @@ -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, }) }