comment_domain_path_get.go: include path in return

This commit is contained in:
Adhityaa Chandrasekar 2018-07-25 00:04:42 +05:30
parent 9fb33fbd9d
commit afeef81b29
5 changed files with 36 additions and 30 deletions

View File

@ -42,7 +42,7 @@ func commentApproveHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
domain, err := commentDomainGet(*x.CommentHex) domain, _, err := commentDomainPathGet(*x.CommentHex)
if err != nil { if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return

View File

@ -41,7 +41,7 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
domain, err := commentDomainGet(*x.CommentHex) domain, _, err := commentDomainPathGet(*x.CommentHex)
if err != nil { if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return

View File

@ -1,24 +0,0 @@
package main
import ()
func commentDomainGet(commentHex string) (string, error) {
if commentHex == "" {
return "", errorMissingField
}
statement := `
SELECT domain
FROM comments
WHERE commentHex = $1;
`
row := db.QueryRow(statement, commentHex)
var domain string
var err error
if err = row.Scan(&domain); err != nil {
return "", errorNoSuchDomain
}
return domain, nil
}

View File

@ -0,0 +1,25 @@
package main
import ()
func commentDomainPathGet(commentHex string) (string, string, error) {
if commentHex == "" {
return "", "", errorMissingField
}
statement := `
SELECT domain, path
FROM comments
WHERE commentHex = $1;
`
row := db.QueryRow(statement, commentHex)
var domain string
var path string
var err error
if err = row.Scan(&domain, &path); err != nil {
return "", "", errorNoSuchDomain
}
return domain, path, nil
}

View File

@ -5,19 +5,24 @@ import (
"time" "time"
) )
func TestCommentDomainGetBasics(t *testing.T) { func TestCommentDomainPathGetBasics(t *testing.T) {
failTestOnError(t, setupTestEnv()) failTestOnError(t, setupTestEnv())
commentHex, _ := commentNew("temp-commenter-hex", "example.com", "/path.html", "root", "**foo**", "approved", time.Now().UTC()) commentHex, _ := commentNew("temp-commenter-hex", "example.com", "/path.html", "root", "**foo**", "approved", time.Now().UTC())
domain, err := commentDomainGet(commentHex) domain, path, err := commentDomainPathGet(commentHex)
if err != nil { if err != nil {
t.Errorf("unexpected error getting domain by hex: %v", err) t.Errorf("unexpected error getting domain by hex: %v", err)
return return
} }
if domain != "example.com" { if domain != "example.com" {
t.Errorf("expected domain = example.com got domain = %s", domain) t.Errorf("expected domain=example.com got domain=%s", domain)
return
}
if path != "/path.html" {
t.Errorf("expected path=/path.html got path=%s", path)
return return
} }
} }
@ -25,7 +30,7 @@ func TestCommentDomainGetBasics(t *testing.T) {
func TestCommentDomainGetEmpty(t *testing.T) { func TestCommentDomainGetEmpty(t *testing.T) {
failTestOnError(t, setupTestEnv()) failTestOnError(t, setupTestEnv())
if _, err := commentDomainGet(""); err == nil { if _, _, err := commentDomainPathGet(""); err == nil {
t.Errorf("expected error not found getting domain with empty commentHex") t.Errorf("expected error not found getting domain with empty commentHex")
return return
} }