5 "git.jacobcasper.com/brackets/env"
6 "git.jacobcasper.com/brackets/routes"
7 "git.jacobcasper.com/brackets/types"
12 func Index(env *env.Env) routes.Handler {
13 return func(w http.ResponseWriter, r *http.Request) {
14 if r.Method != "GET" {
15 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
19 w.Header().Set("Access-Control-Allow-Origin", env.CorsOrigin)
20 w.Header().Set("Vary", "Origin")
21 w.Header().Set("Content-Type", "application/json")
23 genreName := r.FormValue("name")
25 genre := types.Genre{}
26 row := env.Db.Db.QueryRow(`
29 WHERE NAME = lower(?)`,
32 if err := row.Scan(&genre.ID, &genre.Name); err != nil {
33 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
36 b, err := json.Marshal(genre)
39 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
46 rows, err := env.Db.Db.Query(`
51 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
56 genres := make([]types.Genre, 0)
58 genre := types.Genre{}
59 if err := rows.Scan(&genre.ID, &genre.Name); err != nil {
60 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
63 genres = append(genres, genre)
65 if err = rows.Err(); err != nil {
67 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
70 b, err := json.Marshal(genres)
73 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)