mirror of
https://github.com/marcel-dempers/docker-development-youtube-series.git
synced 2025-06-06 17:01:30 +00:00
improvements
This commit is contained in:
parent
bb3ef94b2d
commit
f48c3bd323
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user