From 6cfa9922de0a6b3454543b5bd23814958820191e Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Fri, 27 Dec 2019 17:22:31 -0800 Subject: [PATCH] email_get.go: clean up SQL --- api/email_get.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/api/email_get.go b/api/email_get.go index c626a33..4e4dbc9 100644 --- a/api/email_get.go +++ b/api/email_get.go @@ -4,16 +4,34 @@ import ( "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) { statement := ` - SELECT email, unsubscribeSecretHex, lastEmailNotificationDate, sendReplyNotifications, sendModeratorNotifications + SELECT ` + emailsRowColumns + ` FROM emails WHERE email = $1; ` row := db.QueryRow(statement, em) - e := email{} - if err := row.Scan(&e.Email, &e.UnsubscribeSecretHex, &e.LastEmailNotificationDate, &e.SendReplyNotifications, &e.SendModeratorNotifications); err != nil { + var e email + if err := emailsRowScan(row, &e); err != nil { // TODO: is this the only error? return e, errorNoSuchEmail } @@ -23,14 +41,14 @@ func emailGet(em string) (email, error) { func emailGetByUnsubscribeSecretHex(unsubscribeSecretHex string) (email, error) { statement := ` - SELECT email, unsubscribeSecretHex, lastEmailNotificationDate, sendReplyNotifications, sendModeratorNotifications + SELECT ` + emailsRowColumns + ` FROM emails WHERE unsubscribeSecretHex = $1; ` row := db.QueryRow(statement, unsubscribeSecretHex) 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? return e, errorNoSuchUnsubscribeSecretHex }