From bd5242a57de1db33523b9031b9d0855bfec85310 Mon Sep 17 00:00:00 2001
From: Hans Kristian Hoel <hanskhoe@stud.ntnu.no>
Date: Sat, 2 Nov 2019 03:47:53 +0100
Subject: [PATCH] did some commenting

---
 assignment2/firebase.go | 12 ++++++------
 assignment2/webhook.go  | 19 +++++++++----------
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/assignment2/firebase.go b/assignment2/firebase.go
index e7833b3..d0cf484 100644
--- a/assignment2/firebase.go
+++ b/assignment2/firebase.go
@@ -63,15 +63,15 @@ func FBRead() {
 			log.Fatalf("Failed to iterate: %v", err)
 		}
 		fmt.Println(doc.Data())
-		doc.DataTo(&webhook)
-		tempCount := 0
-		for i := range webhooks {
-			if webhooks[i].ID == webhook.ID {
-				tempCount++
+		doc.DataTo(&webhook)      // sets data in to webhook struct
+		tempCount := 0            // controll for duplicate
+		for i := range webhooks { // lopps true webhooks
+			if webhooks[i].ID == webhook.ID { // cheek it the webhook allready exist
+				tempCount++ //adds 1
 			}
 		}
 
-		if tempCount == 0 {
+		if tempCount == 0 { // if tempCount is 0 it means that there is no duplicats and can add webhook
 			webhooks = append(webhooks, webhook)
 		}
 
diff --git a/assignment2/webhook.go b/assignment2/webhook.go
index d16bbc9..fe12d85 100644
--- a/assignment2/webhook.go
+++ b/assignment2/webhook.go
@@ -25,16 +25,15 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
 	switch r.Method {
 	case http.MethodPost:
 
-		err := json.NewDecoder(r.Body).Decode(&webhook)
+		err := json.NewDecoder(r.Body).Decode(&webhook) //decode to webhook
 		if err != nil {
 			http.Error(w, err.Error(), http.StatusBadRequest)
 		}
 
-		webhook.Time = time.Now()
-		IDNum++
+		webhook.Time = time.Now() // sets time stamp
 
-		FBSave()
-		webhooks = append(webhooks, webhook)
+		FBSave()                             // saves webhook to firebase
+		webhooks = append(webhooks, webhook) // saves webhook to webhooks
 
 		fmt.Fprintln(w, len(webhooks)-1)
 		fmt.Println("Webhooks " + webhook.URL + " has been regstrerd")
@@ -44,19 +43,19 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
 		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 {
+		if len(parts) == 5 { // this need to be fixed
 
-			tempInt, err := strconv.Atoi(parts[4])
+			tempInt, err := strconv.Atoi(parts[4]) // gets the numid of choosen webhook
 			if err != nil {
 				http.Error(w, err.Error(), http.StatusBadRequest)
 			}
 
-			err = json.NewEncoder(w).Encode(webhooks[tempInt-1])
+			err = json.NewEncoder(w).Encode(webhooks[tempInt]) // encode choosen webhook
 			if err != nil {
 				http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
 			}
-		} else {
-			err := json.NewEncoder(w).Encode(webhooks)
+		} else { // if not choosen any webhooks
+			err := json.NewEncoder(w).Encode(webhooks) // encode all webhooks
 			if err != nil {
 				http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
 			}
-- 
GitLab