From 5795887b0c3251069d10cf7fd736b0e46ad66b66 Mon Sep 17 00:00:00 2001
From: andmag <andmag@stud.ntnu.no>
Date: Thu, 10 Oct 2019 09:53:16 +0200
Subject: [PATCH] species and speciesKey list

---
 country.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 47 insertions(+), 7 deletions(-)

diff --git a/country.go b/country.go
index a33ed32..c900758 100644
--- a/country.go
+++ b/country.go
@@ -50,7 +50,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
 
 }
 
-/*
 // HandlerOnlySpecies skjdfhkshfkl
 func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
 
@@ -58,6 +57,12 @@ func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
 
 	parts := strings.Split(r.URL.Path, "/")
 
+	if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "spec" || parts[4] == "" {
+		status := http.StatusBadRequest
+		http.Error(w, "Expecting format /conservation/v1/spec/'alphacode'", status)
+		return
+	}
+
 	APIURL += parts[4]
 
 	http.Header.Add(w.Header(), "content-type", "application/json")
@@ -75,11 +80,46 @@ func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
 
 	defer resp.Body.Close()
 
-	var species []SpeciesByCountry
-	//species := []SpeciesByCountry{}
-	//mySpeciesList := make([]SBCList, 0)
-	json.NewDecoder(resp.Body).Decode(&species)
-	json.NewEncoder(w).Encode(species)
+	type Result Results
+
+	var name = &Result{}
+
+	//v, _ := ioutil.ReadAll(resp.Body)
+
+	//fmt.Println("Body: ", string(v))
+
+	err2 := json.NewDecoder(resp.Body).Decode(name)
+	if err2 != nil {
+		fmt.Println("Error", err2.Error())
+	}
+
+	var spec []string
+	var specKey []int
+
+	for k, v := range name.Res1 {
+		fmt.Println(k)
+
+		fmt.Println(v.Species)
+		spec = append(spec, v.Species)
+		specKey = append(specKey, v.SpeciesKey)
+		fmt.Println(v.SpeciesKey)
+		/*for k1, v1 := range v {
+			fmt.Println(k1, v1)
+		}*/
+		fmt.Println(v)
+
+	}
+
+	fmt.Println(spec)
+
+	result := Results2{}
+
+	result.Res2 = spec
+	result.Res3 = specKey
+
+	err3 := json.NewEncoder(w).Encode(result)
+	if err3 != nil {
+		fmt.Println("error2", err3.Error())
+	}
 
 }
-*/
-- 
GitLab