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("Access-Control-Allow-Origin", env.CorsOrigin)
22 w.Header().Set("Vary", "Origin")
23 w.Header().Set("Content-Type", "application/json")
25 artistId := r.FormValue("id")
27 artist := types.Artist{}
28 row := env.Db.Db.QueryRow(`
29 SELECT ID, NAME, POPULARITY
34 if err := row.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil {
35 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
38 b, err := json.Marshal(artist)
41 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
48 rows, err := env.Db.Db.Query(`
49 SELECT ID, NAME, POPULARITY
54 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
59 artists := make([]types.Artist, 0)
61 artist := types.Artist{}
62 if err := rows.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil {
63 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
66 artists = append(artists, artist)
68 if err = rows.Err(); err != nil {
70 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
73 b, err := json.Marshal(artists)
76 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
83 func Add(env *env.Env) routes.Handler {
84 return func(w http.ResponseWriter, r *http.Request) {
85 if r.Method != "POST" {
86 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
91 artistId := r.PostForm.Get("id")
94 http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
98 artist, err := env.C.GetArtist(spotify.ID(artistId))
100 log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
101 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
106 defer env.Db.Mu.Unlock()
109 (ID, NAME, POPULARITY)
116 for _, genre := range artist.Genres {
118 row := env.Db.Db.QueryRow(`
121 WHERE NAME = lower(?)
126 err := row.Scan(&genreId)
127 if err == sql.ErrNoRows {
128 result, err := env.Db.Db.Exec(`
135 log.Printf("Failed to insert genre %s: %s", genre, err.Error())
136 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
140 genreId, err = result.LastInsertId()
142 log.Print("Failed to retrieve last insert id: ", err.Error())
143 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
149 INSERT INTO ARTIST_GENRE_XREF
150 (ARTIST_ID, GENRE_ID)
156 w.WriteHeader(http.StatusCreated)
160 func ByGenre(env *env.Env) routes.Handler {
161 return func(w http.ResponseWriter, r *http.Request) {
162 if r.Method != "GET" {
163 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
167 w.Header().Set("Access-Control-Allow-Origin", env.CorsOrigin)
168 w.Header().Set("Vary", "Origin")
169 w.Header().Set("Content-Type", "application/json")
171 genreName := r.FormValue("genre_name")
173 rows, err := env.Db.Db.Query(`
174 SELECT a.ID, a.NAME, a.POPULARITY
176 JOIN ARTIST_GENRE_XREF x ON a.ID = x.ARTIST_ID
177 JOIN GENRE g ON g.ID = x.GENRE_ID
178 WHERE g.NAME = lower(?)
184 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
189 artists := make([]types.Artist, 0)
191 artist := types.Artist{}
192 if err := rows.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil {
194 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
197 artists = append(artists, artist)
199 if err = rows.Err(); err != nil {
201 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
204 b, err := json.Marshal(artists)
207 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)