-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsubscribeHandler.go
47 lines (39 loc) · 1.01 KB
/
unsubscribeHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"encoding/json"
"net/http"
"google.golang.org/appengine"
)
// Unsubscribe handles a request to be removed from the notification listing.
func Unsubscribe(w http.ResponseWriter, r *http.Request) {
// Only allow POST requests since we're saving data
if r.Method != "POST" {
http.Error(w, "Invalid Method Type", http.StatusMethodNotAllowed)
return
}
// Parse out email param
_ = r.ParseForm()
email := r.FormValue("email")
s := Subscription{Email: email}
if valid, err := s.ValidEmail(); !valid {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Unsubscribe using email
ctx := appengine.NewContext(r)
if err := s.Unsubscribe(ctx); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Woop
w.Header().Set("Content-Type", "application/json")
m := message{
"Successfully unsubscribed",
s.Email,
}
err := json.NewEncoder(w).Encode(m)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}