5 "git.jacobcasper.com/brackets/env"
6 "git.jacobcasper.com/brackets/routes"
7 "git.jacobcasper.com/brackets/types"
8 "github.com/zmb3/spotify"
13 func Index(env *env.Env) routes.Handler {
14 return func(w http.ResponseWriter, r *http.Request) {
15 if r.Method != "GET" {
16 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
20 w.Header().Set("Content-Type", "application/json")
22 artistId := r.FormValue("id")
24 artist := types.Artist{}
25 row := env.Db.Db.QueryRow("SELECT ID, NAME FROM ARTIST WHERE ID = ?", artistId)
26 if err := row.Scan(&artist.ID, &artist.Name); err != nil {
27 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
30 b, err := json.Marshal(artist)
33 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
40 rows, err := env.Db.Db.Query("SELECT ID, NAME FROM ARTIST LIMIT 20")
42 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
47 artists := make([]types.Artist, 0)
49 artist := types.Artist{}
50 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
51 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
54 artists = append(artists, artist)
56 if err = rows.Err(); err != nil {
58 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
61 b, err := json.Marshal(artists)
64 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
71 func Add(env *env.Env) routes.Handler {
72 return func(w http.ResponseWriter, r *http.Request) {
73 if r.Method != "POST" {
74 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
79 artistId := r.PostForm.Get("id")
82 http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
86 artist, err := env.C.GetArtist(spotify.ID(artistId))
88 log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
89 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
94 defer env.Db.Mu.Unlock()
95 env.Db.Db.Exec("INSERT INTO ARTIST (ID, NAME) VALUES (?, ?)", artist.ID, artist.Name)
97 for _, genre := range artist.Genres {
98 result, err := env.Db.Db.Exec("INSERT OR IGNORE INTO GENRE (NAME) VALUES (?)", genre)
100 log.Printf("Failed to insert genre %s: %s", genre, err.Error())
101 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
105 genreId, err := result.LastInsertId()
107 log.Print("Failed to retrieve last insert id: ", err.Error())
108 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
112 env.Db.Db.Exec("INSERT INTO ARTIST_GENRE_XREF (ARTIST_ID, GENRE_ID) VALUES (?, ?)", artist.ID, genreId)
114 w.WriteHeader(http.StatusCreated)
118 func ByGenre(env *env.Env) routes.Handler {
119 return func(w http.ResponseWriter, r *http.Request) {
120 if r.Method != "GET" {
121 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
125 w.Header().Set("Content-Type", "application/json")
126 genreName := r.FormValue("genre_name")
128 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)
131 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
136 artists := make([]types.Artist, 0)
138 artist := types.Artist{}
139 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
140 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
143 artists = append(artists, artist)
145 if err = rows.Err(); err != nil {
147 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
150 b, err := json.Marshal(artists)
153 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)