api,frontend: add unsubscribe
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import ()
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func emailGet(em string) (email, error) {
|
||||
statement := `
|
||||
@@ -18,3 +20,40 @@ func emailGet(em string) (email, error) {
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func emailGetByUnsubscribeSecretHex(unsubscribeSecretHex string) (email, error) {
|
||||
statement := `
|
||||
SELECT email, unsubscribeSecretHex, lastEmailNotificationDate, pendingEmails, sendReplyNotifications, sendModeratorNotifications
|
||||
FROM emails
|
||||
WHERE unsubscribeSecretHex = $1;
|
||||
`
|
||||
row := db.QueryRow(statement, unsubscribeSecretHex)
|
||||
|
||||
e := email{}
|
||||
if err := row.Scan(&e.Email, &e.UnsubscribeSecretHex, &e.LastEmailNotificationDate, &e.PendingEmails, &e.SendReplyNotifications, &e.SendModeratorNotifications); err != nil {
|
||||
// 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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user