-
Notifications
You must be signed in to change notification settings - Fork 160
/
handler_video_meta.go
120 lines (102 loc) · 3.15 KB
/
handler_video_meta.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/json"
"net/http"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
"github.com/google/uuid"
)
func (cfg *apiConfig) handlerVideoMetaCreate(w http.ResponseWriter, r *http.Request) {
type parameters struct {
database.CreateVideoParams
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT", err)
return
}
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't validate JWT", err)
return
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
err = decoder.Decode(¶ms)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err)
return
}
params.UserID = userID
video, err := cfg.db.CreateVideo(params.CreateVideoParams)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create video", err)
return
}
respondWithJSON(w, http.StatusCreated, video)
}
func (cfg *apiConfig) handlerVideoMetaDelete(w http.ResponseWriter, r *http.Request) {
videoIDString := r.PathValue("videoID")
videoID, err := uuid.Parse(videoIDString)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid ID", err)
return
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT", err)
return
}
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't validate JWT", err)
return
}
video, err := cfg.db.GetVideo(videoID)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't get video", err)
return
}
if video.UserID != userID {
respondWithError(w, http.StatusForbidden, "You can't delete this video", err)
return
}
err = cfg.db.DeleteVideo(videoID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't delete video", err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (cfg *apiConfig) handlerVideoGet(w http.ResponseWriter, r *http.Request) {
videoIDString := r.PathValue("videoID")
videoID, err := uuid.Parse(videoIDString)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid video ID", err)
return
}
video, err := cfg.db.GetVideo(videoID)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't get video", err)
return
}
respondWithJSON(w, http.StatusOK, video)
}
func (cfg *apiConfig) handlerVideosRetrieve(w http.ResponseWriter, r *http.Request) {
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT", err)
return
}
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't validate JWT", err)
return
}
videos, err := cfg.db.GetVideos(userID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't retrieve videos", err)
return
}
respondWithJSON(w, http.StatusOK, videos)
}