Allow retrieving artists
authorJacob Casper <dev@jacobcasper.com>
Thu, 16 Apr 2020 05:45:46 +0000 (00:45 -0500)
committerJacob Casper <dev@jacobcasper.com>
Thu, 16 Apr 2020 05:45:46 +0000 (00:45 -0500)
main.go
routes/artist/artist.go
types/artist.go [new file with mode: 0644]

diff --git a/main.go b/main.go
index 2cf4a77..898a48d 100644 (file)
--- a/main.go
+++ b/main.go
@@ -14,6 +14,11 @@ func main() {
                log.Fatal("Could not set up Env: ", err.Error())
        }
 
+       http.HandleFunc(
+               "/artist/",
+               artist.Index(env),
+       )
+
        http.HandleFunc(
                "/artist/add",
                artist.Add(env),
index c3029d5..cc09563 100644 (file)
@@ -1,13 +1,73 @@
 package artist
 
 import (
+       "encoding/json"
        "git.jacobcasper.com/brackets/env"
        "git.jacobcasper.com/brackets/routes"
+       "git.jacobcasper.com/brackets/types"
        "github.com/zmb3/spotify"
        "log"
        "net/http"
 )
 
+func Index(env *env.Env) routes.Handler {
+       return func(w http.ResponseWriter, r *http.Request) {
+               if r.Method != "GET" {
+                       http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
+                       return
+               }
+
+               w.Header().Set("Content-Type", "application/json")
+
+               artistId := r.FormValue("id")
+               if artistId != "" {
+                       artist := types.Artist{}
+                       row := env.Db.Db.QueryRow("SELECT ID, NAME FROM ARTIST WHERE ID = ?", artistId)
+                       if err := row.Scan(&artist.ID, &artist.Name); err != nil {
+                               http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                               return
+                       }
+                       b, err := json.Marshal(artist)
+                       if err != nil {
+                               log.Print(err)
+                               http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                               return
+                       }
+                       w.Write(b)
+                       return
+               }
+
+               rows, err := env.Db.Db.Query("SELECT ID, NAME FROM ARTIST LIMIT 20")
+               if err != nil {
+                       http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                       return
+               }
+               defer rows.Close()
+
+               artists := make([]types.Artist, 0)
+               for rows.Next() {
+                       artist := types.Artist{}
+                       if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
+                               http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                               return
+                       }
+                       artists = append(artists, artist)
+               }
+               if err = rows.Err(); err != nil {
+                       log.Print(err)
+                       http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                       return
+               }
+               b, err := json.Marshal(artists)
+               if err != nil {
+                       log.Print(err)
+                       http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                       return
+               }
+               w.Write(b)
+       }
+}
+
 func Add(env *env.Env) routes.Handler {
        return func(w http.ResponseWriter, r *http.Request) {
                if r.Method != "POST" {
diff --git a/types/artist.go b/types/artist.go
new file mode 100644 (file)
index 0000000..07be6a8
--- /dev/null
@@ -0,0 +1,9 @@
+package types
+
+import "github.com/zmb3/spotify"
+
+type Artist struct {
+       ID         spotify.ID `json:"id"`
+       Name       string     `json:"name"`
+       Popularity int        `json:"popularity"`
+}