comment_list.go: refactor SQL statements

This commit is contained in:
Adhityaa Chandrasekar 2019-05-01 18:45:14 -04:00
parent 9d4ed4ca9f
commit feeda79923

View File

@ -12,7 +12,15 @@ func commentList(commenterHex string, domain string, path string, includeUnappro
} }
statement := ` statement := `
SELECT commentHex, commenterHex, markdown, html, parentHex, score, state, creationDate SELECT
commentHex,
commenterHex,
markdown,
html,
parentHex,
score,
state,
creationDate
FROM comments FROM comments
WHERE WHERE
comments.domain = $1 AND comments.domain = $1 AND
@ -21,13 +29,9 @@ func commentList(commenterHex string, domain string, path string, includeUnappro
if !includeUnapproved { if !includeUnapproved {
if commenterHex == "anonymous" { if commenterHex == "anonymous" {
statement += ` statement += `AND state = 'approved'`
AND state = 'approved'
`
} else { } else {
statement += ` statement += `AND (state = 'approved' OR commenterHex = $3)`
AND (state = 'approved' OR commenterHex = $3)
`
} }
} }
@ -54,7 +58,15 @@ func commentList(commenterHex string, domain string, path string, includeUnappro
comments := []comment{} comments := []comment{}
for rows.Next() { for rows.Next() {
c := comment{} c := comment{}
if err = rows.Scan(&c.CommentHex, &c.CommenterHex, &c.Markdown, &c.Html, &c.ParentHex, &c.Score, &c.State, &c.CreationDate); err != nil { if err = rows.Scan(
&c.CommentHex,
&c.CommenterHex,
&c.Markdown,
&c.Html,
&c.ParentHex,
&c.Score,
&c.State,
&c.CreationDate); err != nil {
return nil, nil, errorInternal return nil, nil, errorInternal
} }