func NewReadAllHandler(fs *firestore.Client) http.HandlerFunc {
	return func(rw http.ResponseWriter, r *http.Request) {
		// Get refs to all the documents in the collection
		docrefs, err := fs.Collection(WebhookCollection).DocumentRefs(r.Context()).GetAll()
		if err != nil {
			log.Println(err.Error())
			http.Error(rw, "Something went wrong trying to get the webhooks", http.StatusInternalServerError)
			return
		}

		// For all the docrefs, get their document snapshot,
		// and read the data into a struct, then append that struct to `body`
		// NOTE: There might be docrefs for which there is no document, therefore we can not preallocate the body slice.
		body := make([]Webhook, 0)
		for _, docref := range docrefs {
			docsnap, err := docref.Get(r.Context())
			if status.Code(err) == codes.NotFound {
				http.Error(rw, "Invalid webhook id; No webhook registered by that id", http.StatusBadRequest)
				return
			} else if err != nil {
				log.Println(err.Error())
				http.Error(rw, "Something went wrong trying to get the webhook", http.StatusInternalServerError)
				return
			}

			var data Webhook
			err = docsnap.DataTo(&data)
			if err != nil {
				log.Println(err.Error())
				http.Error(rw, "Something went wrong trying to read the webhook", http.StatusInternalServerError)
				return
			}

			body = append(body, data)
		}

		_ = json.NewEncoder(rw).Encode(&body)
	}
}
// NewReadAllHandler creates a HttpHandler that reads all the webhooks from the database and returns them.
NewReadAllHandler creates a HttpHandler that reads all the webhooks from the database and returns them.
// NewReadAllHandler creates a HttpHandler that reads all the webhooks from the database and returns them .
func NewReadAllHandler(fs *firestore.Client) http.HandlerFunc {
	return func(rw http.ResponseWriter, r *http.Request) {
		docrefs, err := fs.Collection(WebhookCollection).DocumentRefs(r.Context()).GetAll()
		if err != nil {
			log.Println(err.Error())
			http.Error(rw, "Something went wrong trying to get the webhooks", http.StatusInternalServerError)
			return
		}
		body := make([]Webhook, 0)
		for _, docref := range docrefs {
			docsnap, err := docref.Get(r.Context())
			if status.Code(err) == codes.NotFound {
				http.Error(rw, "Invalid webhook id; No webhook registered by that id", http.StatusBadRequest)
				return
			} else if err != nil {
				log.Println(err.Error())
				http.Error(rw, "Something went wrong trying to get the webhook", http.StatusInternalServerError)
				return
			}
			var data Webhook
			err = docsnap.DataTo(&data)
			if err != nil {
				log.Println(err.Error())
				http.Error(rw, "Something went wrong trying to read the webhook", http.StatusInternalServerError)
				return
			}
			body = append(body, data)
		}
		_ = json.NewEncoder(rw).Encode(&body)
	}
}
func NewReadAllHandler ( fs * firestore . Client ) http . HandlerFunc { return func ( rw http . ResponseWriter , r * http . Request ) { docrefs , err := fs . Collection ( WebhookCollection ) . DocumentRefs ( r . Context ( ) ) . GetAll ( ) if err != nil { log . Println ( err . Error ( ) ) http . Error ( rw , " " , http . StatusInternalServerError ) return } body := make ( [ ] Webhook , 0 ) for _ , docref := range docrefs { docsnap , err := docref . Get ( r . Context ( ) ) if status . Code ( err ) == codes . NotFound { http . Error ( rw , " " , http . StatusBadRequest ) return } else if err != nil { log . Println ( err . Error ( ) ) http . Error ( rw , " " , http . StatusInternalServerError ) return } var data Webhook err = docsnap . DataTo ( & data ) if err != nil { log . Println ( err . Error ( ) ) http . Error ( rw , " " , http . StatusInternalServerError ) return } body = append ( body , data ) } _ = json . NewEncoder ( rw ) . Encode ( & body ) } }
NewReadAllHandler creates a HttpHandler that reads all the webhooks from the database and returns them.
NewReadAllHandler creates a HttpHandler that reads all the webhooks from the database and returns them .
// Get refs to all the documents in the collection // For all the docrefs, get their document snapshot, // and read the data into a struct, then append that struct to `body` // NOTE: There might be docrefs for which there is no document, therefore we can not preallocate the body slice.