package api import ( "encoding/json" "fmt" "music-server/pkg/helpers" "music-server/pkg/models" "music-server/pkg/server" "net/http" ) func MusicHandler(w http.ResponseWriter, r *http.Request) { helpers.SetCorsAndNoCacheHeaders(&w, r) if r.URL.Path == "/music" && r.Method == http.MethodGet { song := r.URL.Query().Get("song") if song == "" { w.WriteHeader(http.StatusBadRequest) _, err := fmt.Fprint(w, "song can't be empty") if err != nil { w.WriteHeader(http.StatusInternalServerError) } } else { s := server.GetSong(song) helpers.SendSong(w, s) } } else if r.URL.Path == "/music/first" && r.Method == http.MethodGet { song := server.GetSoundCheckSong() helpers.SendSong(w, song) } else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet { server.Reset() w.WriteHeader(http.StatusOK) } else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet { song := server.GetRandomSong() helpers.SendSong(w, song) } else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet { chance := server.GetRandomSongLowChance() helpers.SendSong(w, chance) } else if r.URL.Path == "/music/info" && r.Method == http.MethodGet { w.Header().Add("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(server.GetSongInfo()) } else if r.URL.Path == "/music/list" && r.Method == http.MethodGet { w.Header().Add("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(server.GetPlayedSongs()) } else if r.URL.Path == "/music/next" { song := server.GetNextSong() helpers.SendSong(w, song) } else if r.URL.Path == "/music/previous" { song := server.GetPreviousSong() helpers.SendSong(w, song) } else if r.URL.Path == "/music/all" && r.Method == http.MethodGet { w.Header().Add("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(server.GetAllGames()) } else if r.URL.Path == "/music/all/random" && r.Method == http.MethodGet { w.Header().Add("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(server.GetAllGamesRandom()) } else if r.URL.Path == "/music/played" && r.Method == http.MethodPut { var p models.Played err := json.NewDecoder(r.Body).Decode(&p) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } server.SetPlayed(p.Song) w.WriteHeader(http.StatusOK) } else if r.URL.Path == "/music/addQue" && r.Method == http.MethodGet { server.AddLatestToQue() w.WriteHeader(http.StatusOK) } }