From 65e231b3f1c70a7fcfc049a6894a2878ff0a76aa Mon Sep 17 00:00:00 2001
From: Hans Kristian Hoel <hanskhoe@stud.ntnu.no>
Date: Sat, 2 Nov 2019 03:21:02 +0100
Subject: [PATCH] fixed the read func for fb so i dident create duplicate to
 webhooks slice. And working on get comand for webhooks

---
 assignment2/firebase.go | 12 +++++++++++-
 assignment2/webhook.go  | 24 ++++++++++++++++++++----
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/assignment2/firebase.go b/assignment2/firebase.go
index 713620c..e7833b3 100644
--- a/assignment2/firebase.go
+++ b/assignment2/firebase.go
@@ -64,6 +64,16 @@ func FBRead() {
 		}
 		fmt.Println(doc.Data())
 		doc.DataTo(&webhook)
-		webhooks = append(webhooks, webhook)
+		tempCount := 0
+		for i := range webhooks {
+			if webhooks[i].ID == webhook.ID {
+				tempCount++
+			}
+		}
+
+		if tempCount == 0 {
+			webhooks = append(webhooks, webhook)
+		}
+
 	}
 }
diff --git a/assignment2/webhook.go b/assignment2/webhook.go
index 02d1c02..d16bbc9 100644
--- a/assignment2/webhook.go
+++ b/assignment2/webhook.go
@@ -7,6 +7,7 @@ import (
 	"io/ioutil"
 	"net/http"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -40,11 +41,26 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
 
 	case http.MethodGet:
 		FBRead()
-		err := json.NewEncoder(w).Encode(webhooks)
-		if err != nil {
-			http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
+		http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
+		parts := strings.Split(r.URL.Path, "/")                         //finds url parts
+		fmt.Println(parts)
+		if len(parts) == 5 {
+
+			tempInt, err := strconv.Atoi(parts[4])
+			if err != nil {
+				http.Error(w, err.Error(), http.StatusBadRequest)
+			}
+
+			err = json.NewEncoder(w).Encode(webhooks[tempInt-1])
+			if err != nil {
+				http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
+			}
+		} else {
+			err := json.NewEncoder(w).Encode(webhooks)
+			if err != nil {
+				http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
+			}
 		}
-		//FBRead()
 
 	default:
 		http.Error(w, "Method is invalid "+r.Method, http.StatusBadRequest)
-- 
GitLab