-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_polka.go
56 lines (42 loc) · 1.23 KB
/
handler_polka.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
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"net/http"
"github.com/jcsmurph/chirpy/internal/auth"
)
func (cfg *apiConfig) handlerUpgradeUser(w http.ResponseWriter, r *http.Request) {
type parameters struct {
Data map[string]int `json:"data"`
Event string `json:"event"`
}
polkaKey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Polka API key is missing")
return
}
if polkaKey != cfg.polkaSecret {
respondWithError(w, http.StatusUnauthorized, "Polka key is incorrect")
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
decodeErr := decoder.Decode(¶ms)
if decodeErr != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters for User Update handler")
return
}
user, err := cfg.DB.GetUserID(params.Data["user_id"])
if err != nil {
respondWithError(w, http.StatusNotFound, "Unable to find User")
return
}
if params.Event != "user.upgraded" {
respondWithStatus(w, http.StatusOK)
return
}
upgradeErr := cfg.DB.UpgradeUser(user.ID)
if upgradeErr != nil {
respondWithError(w, http.StatusInternalServerError, "Unable to upgrade user")
return
}
respondWithStatus(w, http.StatusOK)
}