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(`
27 SELECT ID, NAME, POPULARITY
32 if err := row.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil {
33 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
36 b, err := json.Marshal(artist)
39 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
46 rows, err := env.Db.Db.Query(`
47 SELECT ID, NAME, POPULARITY
52 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
57 artists := make([]types.Artist, 0)
59 artist := types.Artist{}
60 if err := rows.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil {
61 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
64 artists = append(artists, artist)
66 if err = rows.Err(); err != nil {
68 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
71 b, err := json.Marshal(artists)
74 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
81 func Add(env *env.Env) routes.Handler {
82 return func(w http.ResponseWriter, r *http.Request) {
83 if r.Method != "POST" {
84 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
89 artistId := r.PostForm.Get("id")
92 http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
96 artist, err := env.C.GetArtist(spotify.ID(artistId))
98 log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
99 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
104 defer env.Db.Mu.Unlock()
107 (ID, NAME, POPULARITY)
114 for _, genre := range artist.Genres {
116 row := env.Db.Db.QueryRow(`
119 WHERE NAME = lower(?)
124 err := row.Scan(&genreId)
125 if err == sql.ErrNoRows {
126 result, err := env.Db.Db.Exec(`
133 log.Printf("Failed to insert genre %s: %s", genre, err.Error())
134 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
138 genreId, err = result.LastInsertId()
140 log.Print("Failed to retrieve last insert id: ", err.Error())
141 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
147 INSERT INTO ARTIST_GENRE_XREF
148 (ARTIST_ID, GENRE_ID)
154 w.WriteHeader(http.StatusCreated)
158 func ByGenre(env *env.Env) routes.Handler {
159 return func(w http.ResponseWriter, r *http.Request) {
160 if r.Method != "GET" {
161 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
165 w.Header().Set("Content-Type", "application/json")
166 genreName := r.FormValue("genre_name")
168 rows, err := env.Db.Db.Query(`
171 JOIN ARTIST_GENRE_XREF x ON a.ID = x.ARTIST_ID
172 JOIN GENRE g ON g.ID = x.GENRE_ID
173 WHERE g.NAME = lower(?)
179 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
184 artists := make([]types.Artist, 0)
186 artist := types.Artist{}
187 if err := rows.Scan(&artist.ID, &artist.Name, &artist.Popularity); err != nil {
188 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
191 artists = append(artists, artist)
193 if err = rows.Err(); err != nil {
195 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
198 b, err := json.Marshal(artists)
201 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)