From 2400eea4a5d5851748d9281b71c8736c9d4fdd52 Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Wed, 15 Apr 2020 23:32:32 -0500 Subject: [PATCH] Move routes to packages --- main.go | 53 ++------------------------------------ routes/artist/artist.go | 56 +++++++++++++++++++++++++++++++++++++++++ routes/routes.go | 7 ++++++ 3 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 routes/artist/artist.go create mode 100644 routes/routes.go diff --git a/main.go b/main.go index f0fb3ac..2cf4a77 100644 --- 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 index 0000000..c3029d5 --- /dev/null +++ b/routes/artist/artist.go @@ -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 index 0000000..2df045d --- /dev/null +++ b/routes/routes.go @@ -0,0 +1,7 @@ +package routes + +import ( + "net/http" +) + +type Handler func(http.ResponseWriter, *http.Request) -- 2.20.1