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 := `
SELECT commentHex, commenterHex, markdown, html, parentHex, score, state, creationDate
SELECT
commentHex,
commenterHex,
markdown,
html,
parentHex,
score,
state,
creationDate
FROM comments
WHERE
comments.domain = $1 AND
@ -21,13 +29,9 @@ func commentList(commenterHex string, domain string, path string, includeUnappro
if !includeUnapproved {
if commenterHex == "anonymous" {
statement += `
AND state = 'approved'
`
statement += `AND state = 'approved'`
} else {
statement += `
AND (state = 'approved' OR commenterHex = $3)
`
statement += `AND (state = 'approved' OR commenterHex = $3)`
}
}
@ -54,7 +58,15 @@ func commentList(commenterHex string, domain string, path string, includeUnappro
comments := []comment{}
for rows.Next() {
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
}