Move routes to packages
authorJacob Casper <dev@jacobcasper.com>
Thu, 16 Apr 2020 04:32:32 +0000 (23:32 -0500)
committerJacob Casper <dev@jacobcasper.com>
Thu, 16 Apr 2020 04:32:32 +0000 (23:32 -0500)
main.go
routes/artist/artist.go [new file with mode: 0644]
routes/routes.go [new file with mode: 0644]

diff --git a/main.go b/main.go
index f0fb3ac..2cf4a77 100644 (file)
--- a/main.go
+++ b/main.go
@@ -2,14 +2,12 @@ package main
 
 import (
        "git.jacobcasper.com/brackets/env"
+       "git.jacobcasper.com/brackets/routes/artist"
        _ "github.com/mattn/go-sqlite3"
-       "github.com/zmb3/spotify"
        "log"
        "net/http"
 )
 
-type handler func(http.ResponseWriter, *http.Request)
-
 func main() {
        env, err := env.New()
        if err != nil {
@@ -18,55 +16,8 @@ func main() {
 
        http.HandleFunc(
                "/artist/add",
-               artistAddHandler(env),
+               artist.Add(env),
        )
 
        log.Fatal(http.ListenAndServe(":8080", nil))
 }
-
-func artistAddHandler(env *env.Env) handler {
-       return func(w http.ResponseWriter, r *http.Request) {
-               if r.Method != "POST" {
-                       http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
-                       return
-               }
-
-               r.ParseForm()
-               artistId := r.PostForm.Get("id")
-
-               if artistId == "" {
-                       http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
-                       return
-               }
-
-               artist, err := env.C.GetArtist(spotify.ID(artistId))
-               if err != nil {
-                       log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
-                       http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
-                       return
-               }
-
-               env.Db.Mu.Lock()
-               defer env.Db.Mu.Unlock()
-               env.Db.Db.Exec("INSERT INTO ARTIST (ID, NAME) VALUES (?, ?)", artist.ID, artist.Name)
-
-               for _, genre := range artist.Genres {
-                       result, err := env.Db.Db.Exec("REPLACE INTO GENRE (NAME) VALUES (?)", genre)
-                       if err != nil {
-                               log.Printf("Failed to insert genre %s: %s", genre, err.Error())
-                               http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
-                               return
-                       }
-
-                       genreId, err := result.LastInsertId()
-                       if err != nil {
-                               log.Print("Failed to retrieve last insert id: ", err.Error())
-                               http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
-                               return
-                       }
-
-                       env.Db.Db.Exec("INSERT INTO ARTIST_GENRE_XREF (ARTIST_ID, GENRE_ID) VALUES (?, ?)", artist.ID, genreId)
-               }
-               w.WriteHeader(http.StatusCreated)
-       }
-}
diff --git a/routes/artist/artist.go b/routes/artist/artist.go
new file mode 100644 (file)
index 0000000..c3029d5
--- /dev/null
@@ -0,0 +1,56 @@
+package artist
+
+import (
+       "git.jacobcasper.com/brackets/env"
+       "git.jacobcasper.com/brackets/routes"
+       "github.com/zmb3/spotify"
+       "log"
+       "net/http"
+)
+
+func Add(env *env.Env) routes.Handler {
+       return func(w http.ResponseWriter, r *http.Request) {
+               if r.Method != "POST" {
+                       http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
+                       return
+               }
+
+               r.ParseForm()
+               artistId := r.PostForm.Get("id")
+
+               if artistId == "" {
+                       http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
+                       return
+               }
+
+               artist, err := env.C.GetArtist(spotify.ID(artistId))
+               if err != nil {
+                       log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
+                       http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                       return
+               }
+
+               env.Db.Mu.Lock()
+               defer env.Db.Mu.Unlock()
+               env.Db.Db.Exec("INSERT INTO ARTIST (ID, NAME) VALUES (?, ?)", artist.ID, artist.Name)
+
+               for _, genre := range artist.Genres {
+                       result, err := env.Db.Db.Exec("REPLACE INTO GENRE (NAME) VALUES (?)", genre)
+                       if err != nil {
+                               log.Printf("Failed to insert genre %s: %s", genre, err.Error())
+                               http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                               return
+                       }
+
+                       genreId, err := result.LastInsertId()
+                       if err != nil {
+                               log.Print("Failed to retrieve last insert id: ", err.Error())
+                               http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+                               return
+                       }
+
+                       env.Db.Db.Exec("INSERT INTO ARTIST_GENRE_XREF (ARTIST_ID, GENRE_ID) VALUES (?, ?)", artist.ID, genreId)
+               }
+               w.WriteHeader(http.StatusCreated)
+       }
+}
diff --git a/routes/routes.go b/routes/routes.go
new file mode 100644 (file)
index 0000000..2df045d
--- /dev/null
@@ -0,0 +1,7 @@
+package routes
+
+import (
+       "net/http"
+)
+
+type Handler func(http.ResponseWriter, *http.Request)