commento/api/email_get.go

78 lines
1.6 KiB
Go
Raw Normal View History

2019-02-19 00:23:44 +08:00
package main
2019-02-19 05:30:54 +08:00
import (
"net/http"
)
2019-02-19 00:23:44 +08:00
2019-12-28 09:22:31 +08:00
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,
)
}
2019-02-19 00:23:44 +08:00
func emailGet(em string) (email, error) {
statement := `
2019-12-28 09:22:31 +08:00
SELECT ` + emailsRowColumns + `
2019-02-19 00:23:44 +08:00
FROM emails
WHERE email = $1;
`
row := db.QueryRow(statement, em)
2019-12-28 09:22:31 +08:00
var e email
if err := emailsRowScan(row, &e); err != nil {
2019-02-19 00:23:44 +08:00
// TODO: is this the only error?
return e, errorNoSuchEmail
}
return e, nil
}
2019-02-19 05:30:54 +08:00
func emailGetByUnsubscribeSecretHex(unsubscribeSecretHex string) (email, error) {
statement := `
2019-12-28 09:22:31 +08:00
SELECT ` + emailsRowColumns + `
2019-02-19 05:30:54 +08:00
FROM emails
WHERE unsubscribeSecretHex = $1;
`
row := db.QueryRow(statement, unsubscribeSecretHex)
e := email{}
2019-12-28 09:22:31 +08:00
if err := emailsRowScan(row, &e); err != nil {
2019-02-19 05:30:54 +08:00
// TODO: is this the only error?
return e, errorNoSuchUnsubscribeSecretHex
}
return e, nil
}
func emailGetHandler(w http.ResponseWriter, r *http.Request) {
type request struct {
UnsubscribeSecretHex *string `json:"unsubscribeSecretHex"`
}
var x request
if err := bodyUnmarshal(r, &x); err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
e, err := emailGetByUnsubscribeSecretHex(*x.UnsubscribeSecretHex)
if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
bodyMarshal(w, response{"success": true, "email": e})
}