Skip to content
Snippets Groups Projects
Commit f3cd7b0b authored by Hans Kristian Hoel's avatar Hans Kristian Hoel
Browse files

Implementet firebase read func and conneceted it with webhooks get

parent b53eab04
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ func main() { ...@@ -16,7 +16,7 @@ func main() {
assignment2.FBInit() assignment2.FBInit()
//assignment2.FBSave() //assignment2.FBSave()
assignment2.FBRead() //assignment2.FBRead()
defer assignment2.FBClose() defer assignment2.FBClose()
port := os.Getenv("PORT") port := os.Getenv("PORT")
......
...@@ -42,18 +42,18 @@ func FBClose() { ...@@ -42,18 +42,18 @@ func FBClose() {
func FBSave() { func FBSave() {
ref := fb.Client.Collection(webhook.Event).NewDoc() ref := fb.Client.Collection("Webhooks").NewDoc()
webhook.ID = ref.ID webhook.ID = ref.ID
_, err := ref.Set(fb.Ctx, webhook) _, err := ref.Set(fb.Ctx, webhook)
if err != nil { 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 errors.Wrap(err, "Error in FirebaseDatabase.Save()")
} }
// return nil // return nil
} }
func FBRead() { func FBRead() {
iter := fb.Client.Collection("users").Documents(fb.Ctx) iter := fb.Client.Collection("Webhooks").Documents(fb.Ctx)
for { for {
doc, err := iter.Next() doc, err := iter.Next()
if err == iterator.Done { if err == iterator.Done {
...@@ -63,5 +63,7 @@ func FBRead() { ...@@ -63,5 +63,7 @@ func FBRead() {
log.Fatalf("Failed to iterate: %v", err) log.Fatalf("Failed to iterate: %v", err)
} }
fmt.Println(doc.Data()) fmt.Println(doc.Data())
doc.DataTo(&webhook)
webhooks = append(webhooks, webhook)
} }
} }
...@@ -56,7 +56,7 @@ type Status struct { ...@@ -56,7 +56,7 @@ type Status struct {
type WebhookRegistration struct { type WebhookRegistration struct {
ID string `json:"id"` ID string `json:"id"`
Event string `json:"Event"` Event string `json:"event"`
URL string `json:"url"` URL string `json:"url"`
Time time.Time `json:"time"` Time time.Time `json:"time"`
} }
......
...@@ -39,10 +39,12 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) { ...@@ -39,10 +39,12 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Webhooks " + webhook.URL + " has been regstrerd") fmt.Println("Webhooks " + webhook.URL + " has been regstrerd")
case http.MethodGet: case http.MethodGet:
FBRead()
err := json.NewEncoder(w).Encode(webhooks) err := json.NewEncoder(w).Encode(webhooks)
if err != nil { if err != nil {
http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError) http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
} }
//FBRead()
default: default:
http.Error(w, "Method is invalid "+r.Method, http.StatusBadRequest) http.Error(w, "Method is invalid "+r.Method, http.StatusBadRequest)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment