diff --git a/api/domain_get.go b/api/domain_get.go index 00c2b89..edf715c 100644 --- a/api/domain_get.go +++ b/api/domain_get.go @@ -2,41 +2,31 @@ package main import () -func domainGet(dmn string) (domain, error) { - if dmn == "" { - return domain{}, errorMissingField - } +var domainsRowColumns = ` + domains.domain, + domains.ownerHex, + domains.name, + domains.creationDate, + domains.state, + domains.importedComments, + domains.autoSpamFilter, + domains.requireModeration, + domains.requireIdentification, + domains.moderateAllAnonymous, + domains.emailNotificationPolicy, + domains.commentoProvider, + domains.googleProvider, + domains.twitterProvider, + domains.githubProvider, + domains.gitlabProvider, + domains.ssoProvider, + domains.ssoSecret, + domains.ssoUrl, + domains.defaultSortPolicy +` - statement := ` - SELECT - domain, - ownerHex, - name, - creationDate, - state, - importedComments, - autoSpamFilter, - requireModeration, - requireIdentification, - moderateAllAnonymous, - emailNotificationPolicy, - commentoProvider, - googleProvider, - twitterProvider, - githubProvider, - gitlabProvider, - ssoProvider, - ssoSecret, - ssoUrl, - defaultSortPolicy - FROM domains - WHERE domain = $1; - ` - row := db.QueryRow(statement, dmn) - - var err error - d := domain{} - if err = row.Scan( +func domainsRowScan(s sqlScanner, d *domain) error { + return s.Scan( &d.Domain, &d.OwnerHex, &d.Name, @@ -56,7 +46,25 @@ func domainGet(dmn string) (domain, error) { &d.SsoProvider, &d.SsoSecret, &d.SsoUrl, - &d.DefaultSortPolicy); err != nil { + &d.DefaultSortPolicy, + ) +} + +func domainGet(dmn string) (domain, error) { + if dmn == "" { + return domain{}, errorMissingField + } + + statement := ` + SELECT ` + domainsRowColumns + ` + FROM domains + WHERE domain = $1; + ` + row := db.QueryRow(statement, dmn) + + var err error + d := domain{} + if err = domainsRowScan(row, &d); err != nil { return d, errorNoSuchDomain } diff --git a/api/domain_list.go b/api/domain_list.go index d09995b..aa50414 100644 --- a/api/domain_list.go +++ b/api/domain_list.go @@ -10,27 +10,7 @@ func domainList(ownerHex string) ([]domain, error) { } statement := ` - SELECT - domain, - ownerHex, - name, - creationDate, - state, - importedComments, - autoSpamFilter, - requireModeration, - requireIdentification, - moderateAllAnonymous, - emailNotificationPolicy, - commentoProvider, - googleProvider, - twitterProvider, - githubProvider, - gitlabProvider, - ssoProvider, - ssoSecret, - ssoUrl, - defaultSortPolicy + SELECT ` + domainsRowColumns + ` FROM domains WHERE ownerHex=$1; ` @@ -43,28 +23,8 @@ func domainList(ownerHex string) ([]domain, error) { domains := []domain{} for rows.Next() { - d := domain{} - if err = rows.Scan( - &d.Domain, - &d.OwnerHex, - &d.Name, - &d.CreationDate, - &d.State, - &d.ImportedComments, - &d.AutoSpamFilter, - &d.RequireModeration, - &d.RequireIdentification, - &d.ModerateAllAnonymous, - &d.EmailNotificationPolicy, - &d.CommentoProvider, - &d.GoogleProvider, - &d.TwitterProvider, - &d.GithubProvider, - &d.GitlabProvider, - &d.SsoProvider, - &d.SsoSecret, - &d.SsoUrl, - &d.DefaultSortPolicy); err != nil { + var d domain + if err = domainsRowScan(rows, &d); err != nil { logger.Errorf("cannot Scan domain: %v", err) return nil, errorInternal }