diff --git a/assignment2/firebase.go b/assignment2/firebase.go index 713620c41aeebb8affab03fce735b8256d54de07..e7833b380ddc9a30491ff3398d09d4e450d64b20 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 02d1c020851fc42b6ec957379dcbc67b0205a972..d16bbc95d91220f3437d2e9dfed74d491eaab763 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)