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(`
32 if err := row.Scan(&artist.ID, &artist.Name); 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(`
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); 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()
113 for _, genre := range artist.Genres {
115 row := env.Db.Db.QueryRow(`
118 WHERE NAME = lower(?)
123 err := row.Scan(&genreId)
124 if err == sql.ErrNoRows {
125 result, err := env.Db.Db.Exec(`
132 log.Printf("Failed to insert genre %s: %s", genre, err.Error())
133 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
137 genreId, err = result.LastInsertId()
139 log.Print("Failed to retrieve last insert id: ", err.Error())
140 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
146 INSERT INTO ARTIST_GENRE_XREF
147 (ARTIST_ID, GENRE_ID)
153 w.WriteHeader(http.StatusCreated)
157 func ByGenre(env *env.Env) routes.Handler {
158 return func(w http.ResponseWriter, r *http.Request) {
159 if r.Method != "GET" {
160 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
164 w.Header().Set("Content-Type", "application/json")
165 genreName := r.FormValue("genre_name")
167 rows, err := env.Db.Db.Query(`
170 JOIN ARTIST_GENRE_XREF x ON a.ID = x.ARTIST_ID
171 JOIN GENRE g ON g.ID = x.GENRE_ID
172 WHERE g.NAME = lower(?)
178 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
183 artists := make([]types.Artist, 0)
185 artist := types.Artist{}
186 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
187 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
190 artists = append(artists, artist)
192 if err = rows.Err(); err != nil {
194 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
197 b, err := json.Marshal(artists)
200 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)