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("Content-Type", "application/json")
21 genreName := r.FormValue("name")
23 genre := types.Genre{}
24 row := env.Db.Db.QueryRow(`
27 WHERE NAME = lower(?)`,
30 if err := row.Scan(&genre.ID, &genre.Name); err != nil {
31 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
34 b, err := json.Marshal(genre)
37 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
44 rows, err := env.Db.Db.Query(`
50 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
55 genres := make([]types.Genre, 0)
57 genre := types.Genre{}
58 if err := rows.Scan(&genre.ID, &genre.Name); err != nil {
59 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
62 genres = append(genres, genre)
64 if err = rows.Err(); err != nil {
66 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
69 b, err := json.Marshal(genres)
72 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)