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,22 +12,26 @@ 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
comments.path = $2 comments.path = $2
` `
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,16 +58,24 @@ 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
} }
if commenterHex != "anonymous" { if commenterHex != "anonymous" {
statement = ` statement = `
SELECT direction SELECT direction
FROM votes FROM votes
WHERE commentHex=$1 AND commenterHex=$2; WHERE commentHex=$1 AND commenterHex=$2;
` `
row := db.QueryRow(statement, c.CommentHex, commenterHex) row := db.QueryRow(statement, c.CommentHex, commenterHex)
if err = row.Scan(&c.Direction); err != nil { if err = row.Scan(&c.Direction); err != nil {