-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlewebhook.go
More file actions
57 lines (45 loc) · 1.21 KB
/
Copy pathhandlewebhook.go
File metadata and controls
57 lines (45 loc) · 1.21 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
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"encoding/json"
"net/http"
"github.com/frozendolphin/Chirpy/internal/auth"
"github.com/google/uuid"
)
func (cfg *apiConfig) upgradeUserChirpyRed(w http.ResponseWriter, r *http.Request) {
type event struct {
Event string `json:"event"`
Data struct {
User_id string `json:"user_id"`
} `json:"data"`
}
param := event{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(¶m)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't decode response", err)
}
apikey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "no apikey in header", err)
return
}
if cfg.polka_key != apikey {
respondWithError(w, http.StatusUnauthorized, "apikey didn't match", err)
return
}
if param.Event != "user.upgraded" {
w.WriteHeader(http.StatusNoContent)
return
}
u_id, err := uuid.Parse(param.Data.User_id)
if err != nil {
respondWithError(w, http.StatusBadRequest, "couldn't convert id into uuid", err)
return
}
err = cfg.db.UpgradeUserChirpyRed(r.Context(), u_id)
if err != nil {
respondWithError(w, http.StatusNotFound, "couldn't find the user", err)
return
}
w.WriteHeader(http.StatusNoContent)
}