-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_chirps_delete.go
More file actions
49 lines (38 loc) · 1.13 KB
/
Copy pathhandler_chirps_delete.go
File metadata and controls
49 lines (38 loc) · 1.13 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
package main
import (
"net/http"
"github.com/google/uuid"
"github.com/e-300/http-server-go/internal/auth"
)
func (cfg *apiConfig) handlerDeleteChirp(w http.ResponseWriter, r *http.Request) {
chirpId, err := uuid.Parse(r.PathValue("chirpID"))
if err != nil{
respondWithError(w, http.StatusBadRequest, "Invalid Chirp ID", err)
return
}
accessToken, err := auth.GetBearerToken(r.Header)
if err != nil{
respondWithError(w, http.StatusUnauthorized, "Couldnt find Token", err)
return
}
userID, err := auth.ValidateJWT(accessToken, cfg.jwtSecret)
if err != nil{
respondWithError(w, http.StatusUnauthorized, "Couldnt validate jwt", err)
return
}
dbChirp, err := cfg.db.GetChirp(r.Context(), chirpId)
if err != nil{
respondWithError(w, http.StatusNotFound, "Chirp Could not be Found", err)
return
}
if dbChirp.UserID != userID{
respondWithError(w, http.StatusForbidden, "You cant delete this chirp", err)
return
}
err = cfg.db.DeleteChirp(r.Context(), chirpId)
if err != nil{
respondWithError(w, http.StatusInternalServerError, "Chirp could not be deleted", err)
return
}
w.WriteHeader(http.StatusNoContent)
}