6 "git.jacobcasper.com/brackets/env"
7 "git.jacobcasper.com/brackets/routes"
8 "git.jacobcasper.com/brackets/types"
9 "github.com/zmb3/spotify"
14 func Index(env *env.Env) routes.Handler {
15 return func(w http.ResponseWriter, r *http.Request) {
16 if r.Method != "GET" {
17 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
21 w.Header().Set("Content-Type", "application/json")
23 artistId := r.FormValue("id")
25 artist := types.Artist{}
26 row := env.Db.Db.QueryRow("SELECT ID, NAME FROM ARTIST WHERE ID = ?", artistId)
27 if err := row.Scan(&artist.ID, &artist.Name); err != nil {
28 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
31 b, err := json.Marshal(artist)
34 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
41 rows, err := env.Db.Db.Query("SELECT ID, NAME FROM ARTIST LIMIT 20")
43 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
48 artists := make([]types.Artist, 0)
50 artist := types.Artist{}
51 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
52 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
55 artists = append(artists, artist)
57 if err = rows.Err(); err != nil {
59 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
62 b, err := json.Marshal(artists)
65 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
72 func Add(env *env.Env) routes.Handler {
73 return func(w http.ResponseWriter, r *http.Request) {
74 if r.Method != "POST" {
75 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
80 artistId := r.PostForm.Get("id")
83 http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
87 artist, err := env.C.GetArtist(spotify.ID(artistId))
89 log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
90 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
95 defer env.Db.Mu.Unlock()
96 env.Db.Db.Exec("INSERT INTO ARTIST (ID, NAME) VALUES (?, ?)", artist.ID, artist.Name)
98 for _, genre := range artist.Genres {
100 row := env.Db.Db.QueryRow(`
103 WHERE NAME = lower(?)
107 err := row.Scan(&genreId)
108 if err == sql.ErrNoRows {
109 result, err := env.Db.Db.Exec("INSERT INTO GENRE (NAME) VALUES (?)", genre)
111 log.Printf("Failed to insert genre %s: %s", genre, err.Error())
112 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
116 genreId, err = result.LastInsertId()
118 log.Print("Failed to retrieve last insert id: ", err.Error())
119 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
124 env.Db.Db.Exec("INSERT INTO ARTIST_GENRE_XREF (ARTIST_ID, GENRE_ID) VALUES (?, ?)", artist.ID, genreId)
126 w.WriteHeader(http.StatusCreated)
130 func ByGenre(env *env.Env) routes.Handler {
131 return func(w http.ResponseWriter, r *http.Request) {
132 if r.Method != "GET" {
133 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
137 w.Header().Set("Content-Type", "application/json")
138 genreName := r.FormValue("genre_name")
140 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)
143 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
148 artists := make([]types.Artist, 0)
150 artist := types.Artist{}
151 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
152 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
155 artists = append(artists, artist)
157 if err = rows.Err(); err != nil {
159 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
162 b, err := json.Marshal(artists)
165 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)