email_get.go: clean up SQL

This commit is contained in:
Adhityaa Chandrasekar 2019-12-27 17:22:31 -08:00
parent 72a3f87c28
commit 6cfa9922de

View File

@ -4,16 +4,34 @@ import (
"net/http" "net/http"
) )
var emailsRowColumns = `
emails.email,
emails.unsubscribeSecretHex,
emails.lastEmailNotificationDate,
emails.sendReplyNotifications,
emails.sendModeratorNotifications
`
func emailsRowScan(s sqlScanner, e *email) error {
return s.Scan(
&e.Email,
&e.UnsubscribeSecretHex,
&e.LastEmailNotificationDate,
&e.SendReplyNotifications,
&e.SendModeratorNotifications,
)
}
func emailGet(em string) (email, error) { func emailGet(em string) (email, error) {
statement := ` statement := `
SELECT email, unsubscribeSecretHex, lastEmailNotificationDate, sendReplyNotifications, sendModeratorNotifications SELECT ` + emailsRowColumns + `
FROM emails FROM emails
WHERE email = $1; WHERE email = $1;
` `
row := db.QueryRow(statement, em) row := db.QueryRow(statement, em)
e := email{} var e email
if err := row.Scan(&e.Email, &e.UnsubscribeSecretHex, &e.LastEmailNotificationDate, &e.SendReplyNotifications, &e.SendModeratorNotifications); err != nil { if err := emailsRowScan(row, &e); err != nil {
// TODO: is this the only error? // TODO: is this the only error?
return e, errorNoSuchEmail return e, errorNoSuchEmail
} }
@ -23,14 +41,14 @@ func emailGet(em string) (email, error) {
func emailGetByUnsubscribeSecretHex(unsubscribeSecretHex string) (email, error) { func emailGetByUnsubscribeSecretHex(unsubscribeSecretHex string) (email, error) {
statement := ` statement := `
SELECT email, unsubscribeSecretHex, lastEmailNotificationDate, sendReplyNotifications, sendModeratorNotifications SELECT ` + emailsRowColumns + `
FROM emails FROM emails
WHERE unsubscribeSecretHex = $1; WHERE unsubscribeSecretHex = $1;
` `
row := db.QueryRow(statement, unsubscribeSecretHex) row := db.QueryRow(statement, unsubscribeSecretHex)
e := email{} e := email{}
if err := row.Scan(&e.Email, &e.UnsubscribeSecretHex, &e.LastEmailNotificationDate, &e.SendReplyNotifications, &e.SendModeratorNotifications); err != nil { if err := emailsRowScan(row, &e); err != nil {
// TODO: is this the only error? // TODO: is this the only error?
return e, errorNoSuchUnsubscribeSecretHex return e, errorNoSuchUnsubscribeSecretHex
} }