From f3cd7b0bc2ee17d4271b38a1ec4d40b44261ec57 Mon Sep 17 00:00:00 2001
From: Hans Kristian Hoel <hanskhoe@stud.ntnu.no>
Date: Sat, 2 Nov 2019 01:44:03 +0100
Subject: [PATCH] Implementet firebase read func and conneceted it with
 webhooks get

---
 assignment2/cmd/main.go | 2 +-
 assignment2/firebase.go | 8 +++++---
 assignment2/structs.go  | 2 +-
 assignment2/webhook.go  | 2 ++
 4 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/assignment2/cmd/main.go b/assignment2/cmd/main.go
index 8f4e0b0..5746a86 100644
--- a/assignment2/cmd/main.go
+++ b/assignment2/cmd/main.go
@@ -16,7 +16,7 @@ func main() {
 
 	assignment2.FBInit()
 	//assignment2.FBSave()
-	assignment2.FBRead()
+	//assignment2.FBRead()
 	defer assignment2.FBClose()
 
 	port := os.Getenv("PORT")
diff --git a/assignment2/firebase.go b/assignment2/firebase.go
index 027689d..713620c 100644
--- a/assignment2/firebase.go
+++ b/assignment2/firebase.go
@@ -42,18 +42,18 @@ func FBClose() {
 
 func FBSave() {
 
-	ref := fb.Client.Collection(webhook.Event).NewDoc()
+	ref := fb.Client.Collection("Webhooks").NewDoc()
 	webhook.ID = ref.ID
 	_, err := ref.Set(fb.Ctx, webhook)
 	if err != nil {
-		fmt.Println("ERROR saving student to Firestore DB: ", err)
+		fmt.Println("ERROR saving webhook to Firestore DB: ", err)
 		//return errors.Wrap(err, "Error in FirebaseDatabase.Save()")
 	}
 	// return nil
 }
 
 func FBRead() {
-	iter := fb.Client.Collection("users").Documents(fb.Ctx)
+	iter := fb.Client.Collection("Webhooks").Documents(fb.Ctx)
 	for {
 		doc, err := iter.Next()
 		if err == iterator.Done {
@@ -63,5 +63,7 @@ func FBRead() {
 			log.Fatalf("Failed to iterate: %v", err)
 		}
 		fmt.Println(doc.Data())
+		doc.DataTo(&webhook)
+		webhooks = append(webhooks, webhook)
 	}
 }
diff --git a/assignment2/structs.go b/assignment2/structs.go
index f06f46d..1c1ec20 100644
--- a/assignment2/structs.go
+++ b/assignment2/structs.go
@@ -56,7 +56,7 @@ type Status struct {
 
 type WebhookRegistration struct {
 	ID    string    `json:"id"`
-	Event string    `json:"Event"`
+	Event string    `json:"event"`
 	URL   string    `json:"url"`
 	Time  time.Time `json:"time"`
 }
diff --git a/assignment2/webhook.go b/assignment2/webhook.go
index 8dd77ba..02d1c02 100644
--- a/assignment2/webhook.go
+++ b/assignment2/webhook.go
@@ -39,10 +39,12 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
 		fmt.Println("Webhooks " + webhook.URL + " has been regstrerd")
 
 	case http.MethodGet:
+		FBRead()
 		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