-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_chirps_delete.go
More file actions
48 lines (39 loc) · 1.06 KB
/
Copy pathhandler_chirps_delete.go
File metadata and controls
48 lines (39 loc) · 1.06 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
package main
import (
"net/http"
"github.com/darginmathi/Chirpy/internal/auth"
"github.com/google/uuid"
)
func (cfg *apiConfig) handlerChirpsDelete(w http.ResponseWriter, r *http.Request) {
str := r.PathValue("chirpID")
id, err := uuid.Parse(str)
if err != nil {
respondWithError(w, http.StatusNotFound, "Invalid UUID:", err)
return
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "token field empty", err)
return
}
user_id, err := auth.ValidateJWT(token, cfg.jwt_secret)
if err != nil {
respondWithError(w, http.StatusForbidden, "Forbidden", err)
return
}
chirp, err := cfg.db.GetChirp(r.Context(), id)
if err != nil {
respondWithError(w, http.StatusNotFound, "could not get chirp", err)
return
}
if chirp.UserID != user_id {
respondWithError(w, http.StatusForbidden, "invalid user", err)
return
}
err = cfg.db.DeleteChirp(r.Context(), id)
if err != nil {
respondWithError(w, http.StatusNotFound, "could not get chirp", err)
return
}
w.WriteHeader(http.StatusNoContent)
}