initial commit on github
This commit is contained in:
Executable
+38
@@ -0,0 +1,38 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func RegisterGenresRoutes(r *mux.Router) {
|
||||
r.HandleFunc("/genres", resolveGenres).Methods("GET", "OPTIONS", "POST") // GET, POST
|
||||
r.HandleFunc("/genres/{id:[0-9]+}", resolveGenre).Methods("GET", "OPTIONS", "PUT", "DELETE") // GET, PUT, DELETE
|
||||
r.HandleFunc("/genres/{id:[0-9]+}/novels", resolveGenreNovels).Methods("GET", "OPTIONS") // GET
|
||||
}
|
||||
|
||||
func resolveGenres(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
genres, err := FetchGenres()
|
||||
Response(w, err, genres)
|
||||
} else if r.Method == http.MethodPost {
|
||||
RawError(w, "method not implemented")
|
||||
}
|
||||
}
|
||||
|
||||
func resolveGenre(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id, _ := strconv.Atoi(vars["id"])
|
||||
|
||||
genre, err := FetchGenre(id)
|
||||
Response(w, err, genre)
|
||||
}
|
||||
|
||||
func resolveGenreNovels(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id, _ := strconv.Atoi(vars["id"])
|
||||
|
||||
novels, err := FetchGenreNovels(id)
|
||||
Response(w, err, novels)
|
||||
}
|
||||
Reference in New Issue
Block a user