improvements

This commit is contained in:
marcel-dempers 2021-10-20 13:44:27 +11:00
parent bb3ef94b2d
commit f48c3bd323
2 changed files with 44 additions and 4 deletions

View File

@ -39,7 +39,29 @@ func main() {
func HandleGetVideos(w http.ResponseWriter, r *http.Request){
redisClient.Ping(ctx)
id, ok := r.URL.Query()["id"]
if ok {
videoID := id[0]
video := getVideo(videoID)
if video.Id == "" { //video not found, or empty ID
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{}"))
return
}
videoBytes, err := json.Marshal(video)
if err != nil {
panic(err)
}
w.Write(videoBytes)
return
}
videos := getVideos()
videoBytes, err := json.Marshal(videos)
@ -59,6 +81,21 @@ func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){
panic(err)
}
_, ok := r.URL.Query()["id"]
if ok {
var video video
err = json.Unmarshal(body, &video)
if err != nil {
w.WriteHeader(400)
fmt.Fprintf(w, "Bad request")
}
saveVideo(video)
return
}
var videos []video
err = json.Unmarshal(body, &videos)
if err != nil {
@ -67,6 +104,7 @@ func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){
}
saveVideos(videos)
return
} else {
w.WriteHeader(405)

View File

@ -33,13 +33,15 @@ func getVideo(id string)(video video) {
value, err := redisClient.Get(ctx, id).Result()
if err == redis.Nil {
return video
}
if err != nil {
panic(err)
}
if err != redis.Nil {
err = json.Unmarshal([]byte(value), &video)
}
json.Unmarshal([]byte(value), &video)
return video
}