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("SELECT ID, NAME FROM GENRE WHERE NAME = lower(?)", genreName)
25 if err := row.Scan(&genre.ID, &genre.Name); err != nil {
26 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
29 b, err := json.Marshal(genre)
32 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
39 rows, err := env.Db.Db.Query("SELECT ID, NAME FROM GENRE LIMIT 20")
41 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
46 genres := make([]types.Genre, 0)
48 genre := types.Genre{}
49 if err := rows.Scan(&genre.ID, &genre.Name); err != nil {
50 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
53 genres = append(genres, genre)
55 if err = rows.Err(); err != nil {
57 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
60 b, err := json.Marshal(genres)
63 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)