Skip to content
Snippets Groups Projects
Commit c053002a authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

added limit query param to countries

parent ccd22151
No related branches found
No related tags found
No related merge requests found
......@@ -4,10 +4,9 @@ import (
"assignment-1/models"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
......@@ -15,9 +14,23 @@ func main() {
r := mux.NewRouter()
c := r.PathPrefix("/conservation/v1").Methods("GET").Subrouter()
c.HandleFunc("/country/{country_identifier:[a-zA-Z][a-zA-Z]}", cHandler)
c.HandleFunc("/species/{speciesKey}", sHandler)
c.HandleFunc("/diag/", dHandler)
c.Path("/country/{country_identifier:[a-zA-Z][a-zA-Z]}").
Queries("limit", "{limit}").
HandlerFunc(cHandler).
Name("Handel")
c.Path("/country/{country_identifier:[a-zA-Z][a-zA-Z]}").
HandlerFunc(cHandler)
c.Path("/species/{speciesKey}").
HandlerFunc(sHandler)
c.Path("/diag/").
HandlerFunc(dHandler)
//c.HandleFunc("/country/{country_identifier:[a-zA-Z][a-zA-Z]}", cHandler).Queries("limit", "{limit}")
//c.HandleFunc("/species/{speciesKey}", sHandler)
//c.HandleFunc("/diag/", dHandler)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":5000", nil))
......@@ -37,14 +50,15 @@ func dHandler(w http.ResponseWriter, r *http.Request) {
func cHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
limit := r.FormValue("limit")
if len(limit) == 0 {
limit = "5"
}
c := models.GetCountryByCode(vars["country_identifier"])
fmt.Fprintln(w, c)
//country := models.GetCountryByCode(vars["country_identifier"])
country := models.GetCountryByCode(vars["country_identifier"], limit)
//_, _ = fmt.Fprintln(w, country)
c, _ := json.Marshal(country)
fmt.Fprintln(w, string(c) + limit)
}
......
......@@ -19,9 +19,12 @@ type Country struct {
}
func GetCountryByCode(code string) Country {
func GetCountryByCode(code, limit string) Country {
var x Country
url := countryApi + code + "/"
url := countryApi + code
if len(limit) > 0 {
url += "?limit=" + limit
}
resp, err:= http.Get(url)
if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment