-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_webhooks.go
More file actions
45 lines (42 loc) · 1.12 KB
/
Copy pathhandler_webhooks.go
File metadata and controls
45 lines (42 loc) · 1.12 KB
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
package main
import (
"encoding/json"
"github.com/acramatte/Chirpy/internal/auth"
"github.com/google/uuid"
"net/http"
)
func (cfg *apiConfig) handlerUpgradeRed(w http.ResponseWriter, r *http.Request) {
apiKey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find api key", err)
return
}
if apiKey != cfg.polkaWebhookKey {
respondWithError(w, http.StatusUnauthorized, "API key not authorized", err)
return
}
type parameters struct {
Event string `json:"event"`
Data struct {
UserID uuid.UUID `json:"user_id"`
} `json:"data"`
}
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
params := parameters{}
err = decoder.Decode(¶ms)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err)
return
}
if params.Event != "user.upgraded" {
w.WriteHeader(http.StatusNoContent)
return
}
_, err = cfg.db.UpgradeToRed(r.Context(), params.Data.UserID)
if err != nil {
respondWithError(w, http.StatusNotFound, "User not found", err)
return
}
respondWithJSON(w, http.StatusNoContent, struct{}{})
}