diff --git a/assignment2/cmd/main.go b/assignment2/cmd/main.go index 8f4e0b09f762cd7cde559de4176c796b9d64be7b..5746a86a9d1da3324b81bcdb568a17ea97a56e50 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 027689d89a2fd6b6c312ef504c7bce4aa9f62b42..713620c41aeebb8affab03fce735b8256d54de07 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 f06f46d60e761dccc233a68debeae15c72a39cdf..1c1ec20459fe11f073f77cc70fa0c2fb05c3f3bb 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 8dd77bab9c8e381e63e1ccf94da5f658b240649e..02d1c020851fc42b6ec957379dcbc67b0205a972 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)