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

changed how i read fro fb, and fixed so that i could show webhook by id

parent a422bf1a
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,7 @@ func main() {
http.HandleFunc("/repocheck/v1/languages", assignment2.HandlerLanguages) // runs handelr function
http.HandleFunc("/repocheck/v1/status", assignment2.HandlerStatus) // runs handelr function
http.HandleFunc("/repocheck/v1/webhooks", assignment2.WebhookHandeler) // runs handelr function
http.HandleFunc("/repocheck/v1/webhooks/", assignment2.WebhookHandeler2) // runs handelr function
fmt.Println("Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
......@@ -57,7 +57,9 @@ func FBSave() error {
return nil
}
func FBRead() error {
func FBRead() ([]WebhookRegistration, error) {
var Temp []WebhookRegistration
iter := fb.Client.Collection("Webhooks").Documents(fb.Ctx)
for {
doc, err := iter.Next()
......@@ -72,19 +74,24 @@ func FBRead() error {
if err != nil {
fmt.Println("Error when converting retrieved document to webhook struct: ", err)
}
tempCount := 0 // controll for duplicate
for i := range webhooks { // lopps true webhooks
if webhooks[i].ID == webhook.ID { // cheek it the webhook allready exist
tempCount++ //adds 1
Temp = append(Temp, webhook)
/*
tempCount := 0 // controll for duplicate
for i := range webhooks { // lopps true webhooks
if webhooks[i].ID == webhook.ID { // cheek it the webhook allready exist
tempCount++ //adds 1
}
}
}
if tempCount == 0 { // if tempCount is 0 it means that there is no duplicats and can add webhook
webhooks = append(webhooks, webhook)
}
if tempCount == 0 { // if tempCount is 0 it means that there is no duplicats and can add webhook
webhooks = append(webhooks, webhook)
}
*/
}
return nil
return Temp, nil
}
func FBDelete(ID string) error {
......
......@@ -92,6 +92,7 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271 h1:N66aaryRB3Ax92gH0v3hp1QYZ3zWWCCUR/j8Ifh45Ss=
golang.org/x/net v0.0.0-20191101175033-0deb6923b6d9 h1:DPz9iiH3YoKiKhX/ijjoZvT0VFwK2c6CWYWQ7Zyr8TU=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
......
......@@ -4,4 +4,23 @@ import "testing"
func TestMockDatabase(t *testing.T) {
err := FBInit()
if err != nil {
t.Error(err)
}
err = FBDelete("")
if err != nil {
t.Error(err)
}
_, err = FBRead()
if err != nil {
t.Error(err)
}
err = FBSave()
if err != nil {
t.Error(err)
}
}
......@@ -11,7 +11,6 @@ import (
"time"
)
var webhooks []WebhookRegistration //Webhook DB
var webhook = WebhookRegistration{}
/*
......@@ -37,33 +36,20 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
}
//webhooks = append(webhooks, webhook) // saves webhook to webhooks ***************** look at this one
fmt.Fprintln(w, len(webhooks)-1)
//fmt.Fprintln(w, len(webhooks)-1)
fmt.Println("Webhooks " + webhook.URL + " has been regstrerd")
case http.MethodGet:
err := FBRead()
var webhooks []WebhookRegistration //Webhook DB
webhooks, err := FBRead()
if err != nil {
fmt.Println("Error: ", err)
}
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 { // this need to be fixed
tempInt, err := strconv.Atoi(parts[4]) // gets the numid of choosen webhook
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
err = json.NewEncoder(w).Encode(webhooks[tempInt]) // encode choosen webhook
if err != nil {
http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
}
} else { // if not choosen any webhooks
err := json.NewEncoder(w).Encode(webhooks) // encode all webhooks
if err != nil {
http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
}
err = json.NewEncoder(w).Encode(webhooks) // encode all webhooks
if err != nil {
http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
}
case http.MethodDelete:
......@@ -84,11 +70,48 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
}
func WebhookHandeler2(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
var webhooks []WebhookRegistration //Webhook DB
webhooks, err := FBRead()
if err != nil {
fmt.Println("Error: ", err)
}
http.Header.Add(w.Header(), "Content-Type", "application/json")
parts := strings.Split(r.URL.Path, "/") //finds url parts
fmt.Println(parts)
// tempInt, err := strconv.Atoi(parts[4]) // gets the numid of choosen webhook
// if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// }
for i := range webhooks {
if webhooks[i].ID == parts[4] {
err = json.NewEncoder(w).Encode(webhooks[i]) // encode choosen webhook
if err != nil {
http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
}
}
}
default:
http.Error(w, "Method is invalid "+r.Method, http.StatusBadRequest)
}
}
/*
Invokes the web service to trigger event. Currently only responds to POST requests.
*/
func ServiceHandler(w http.ResponseWriter, r *http.Request) {
var webhooks []WebhookRegistration //Webhook DB
switch r.Method {
case http.MethodPost:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment