X-Git-Url: https://git.jacobcasper.com/?p=brackets.git;a=blobdiff_plain;f=routes%2Fartist%2Fartist.go;h=e6eb320aa89d49e9e09b3176946f0f8205f545bb;hp=09788ada7a0d8e0e83ba6d57944e5fb1520cf9b3;hb=eac0dfd66703dffd39c925fdba612f8aceb7b510;hpb=47d8493fa8fc92295ad04b93ab38bca70b8b6dc2 diff --git a/routes/artist/artist.go b/routes/artist/artist.go index 09788ad..e6eb320 100644 --- a/routes/artist/artist.go +++ b/routes/artist/artist.go @@ -114,3 +114,46 @@ func Add(env *env.Env) routes.Handler { w.WriteHeader(http.StatusCreated) } } + +func ByGenre(env *env.Env) routes.Handler { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return + } + + w.Header().Set("Content-Type", "application/json") + genreName := r.FormValue("genre_name") + if genreName != "" { + rows, err := env.Db.Db.Query("SELECT ID, NAME FROM ARTIST WHERE ID IN (SELECT ARTIST_ID FROM ARTIST_GENRE_XREF WHERE GENRE_ID IN (SELECT ID FROM GENRE WHERE NAME = lower(?))) LIMIT 20", genreName) + if err != nil { + log.Print(err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + defer rows.Close() + + artists := make([]types.Artist, 0) + for rows.Next() { + artist := types.Artist{} + if err := rows.Scan(&artist.ID, &artist.Name); err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + artists = append(artists, artist) + } + if err = rows.Err(); err != nil { + log.Print(err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + b, err := json.Marshal(artists) + if err != nil { + log.Print(err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + w.Write(b) + } + } +}