-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.go
More file actions
51 lines (40 loc) · 1.14 KB
/
webhook.go
File metadata and controls
51 lines (40 loc) · 1.14 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
package main
import (
"encoding/json"
"net/http"
"github.com/SandeshNarayan/chirpy/internal/auth"
"github.com/google/uuid"
)
func (cfg *apiConfig) handlerWebhooksUpdate(w http.ResponseWriter, r *http.Request){
apiKey, err:= auth.GetAPIKey(r.Header)
if err!=nil || apiKey!=cfg.apiKey{
respondWithError(w, http.StatusUnauthorized, "Invalid API key", err)
return
}
type Parameters struct{
Event string `json:"event"`
Data struct{
UserID uuid.UUID `json:"user_id"`
} `json:"data"`
}
var params Parameters
if err:= json.NewDecoder(r.Body).Decode(¶ms); err!=nil{
respondWithError(w, http.StatusInternalServerError, err.Error(), err)
return
}
if params.Event!="user.upgraded"{
w.WriteHeader(http.StatusNoContent)
return
}
_, err= cfg.dbQueries.GetUserByID(r.Context(), params.Data.UserID)
if err!=nil{
respondWithError(w, http.StatusNotFound, "user not found", err)
return
}
err = cfg.dbQueries.RichGuy(r.Context(), params.Data.UserID)
if err!=nil{
respondWithError(w, http.StatusInternalServerError, "Couldnt upgrade user", err)
return
}
w.WriteHeader(http.StatusNoContent)
}