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

have manage to creat function to save struct to database

parent d13382cc
Branches
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ func main() {
assignment2.StartTime = StartTime // sends StartTime
assignment2.FBInit()
assignment2.FBSave()
//assignment2.FBSave()
assignment2.FBRead()
defer assignment2.FBClose()
......
......@@ -4,7 +4,6 @@ import (
"fmt"
"log"
"cloud.google.com/go/firestore"
"golang.org/x/net/context"
firebase "firebase.google.com/go" // Same as python's import dependency as alias.
......@@ -15,11 +14,6 @@ import (
var fb = FireBase{}
type FireBase struct {
Ctx context.Context
Client *firestore.Client
}
func FBInit() {
// Firebase initialisation
fb.Ctx = context.Background()
......@@ -48,22 +42,13 @@ func FBClose() {
func FBSave() {
_, _, err := fb.Client.Collection("users").Add(fb.Ctx, map[string]interface{}{
"first": "Per",
"last": "Lovelace",
"born": 1815,
})
ref := fb.Client.Collection(webhook.Event).NewDoc()
webhook.ID = ref.ID
_, err := ref.Set(fb.Ctx, webhook)
if err != nil {
log.Fatalf("Failed adding alovelace: %v", err)
}
// ref := fb.Client.Collection(fb.CollectionName).NewDoc()
// s.ID = ref.ID
// _, err := ref.Set(fb.Ctx, s)
// if err != nil {
// fmt.Println("ERROR saving student to Firestore DB: ", err)
fmt.Println("ERROR saving student to Firestore DB: ", err)
//return errors.Wrap(err, "Error in FirebaseDatabase.Save()")
// }
}
// return nil
}
......
package assignment2
import "time"
import (
"time"
"cloud.google.com/go/firestore"
"golang.org/x/net/context"
)
//************* Commits ******************
......@@ -50,7 +55,7 @@ type Status struct {
//************ Webhook ************
type WebhookRegistration struct {
ID int `json:"id"`
ID string `json:"id"`
Event string `json:"Event"`
URL string `json:"url"`
Time time.Time `json:"time"`
......@@ -68,3 +73,9 @@ type WebhooksInvocation struct {
Params string `json:"params"`
Time time.Time `json:"time"`
}
//************ Firebase ***********
type FireBase struct {
Ctx context.Context
Client *firestore.Client
}
......@@ -17,21 +17,22 @@ var IDNum = 1
Handles webhook registration (POST) and lookup (GET) requests.
Expects WebhookRegistration struct body in request.
*/
var webhook = WebhookRegistration{}
func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
webhook := WebhookRegistration{}
err := json.NewDecoder(r.Body).Decode(&webhook)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
webhook.ID = IDNum
webhook.Time = time.Now()
IDNum++
FBSave()
webhooks = append(webhooks, webhook)
fmt.Fprintln(w, len(webhooks)-1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment