feat: add arangodb database methods for webhook

This commit is contained in:
Lakhan Samani 2022-07-09 11:44:14 +05:30
parent e8eb62769e
commit 6d586b16e4
7 changed files with 113 additions and 11 deletions

View File

@ -1,8 +1,12 @@
package arangodb package arangodb
import ( import (
"context"
"fmt"
"time" "time"
"github.com/arangodb/go-driver"
arangoDriver "github.com/arangodb/go-driver"
"github.com/authorizerdev/authorizer/server/db/models" "github.com/authorizerdev/authorizer/server/db/models"
"github.com/authorizerdev/authorizer/server/graph/model" "github.com/authorizerdev/authorizer/server/graph/model"
"github.com/google/uuid" "github.com/google/uuid"
@ -17,31 +21,129 @@ func (p *provider) AddWebhook(webhook models.Webhook) (models.Webhook, error) {
webhook.Key = webhook.ID webhook.Key = webhook.ID
webhook.CreatedAt = time.Now().Unix() webhook.CreatedAt = time.Now().Unix()
webhook.UpdatedAt = time.Now().Unix() webhook.UpdatedAt = time.Now().Unix()
webhookCollection, _ := p.db.Collection(nil, models.Collections.Webhook)
_, err := webhookCollection.CreateDocument(nil, webhook)
if err != nil {
return webhook, err
}
return webhook, nil return webhook, nil
} }
// UpdateWebhook to update webhook // UpdateWebhook to update webhook
func (p *provider) UpdateWebhook(webhook models.Webhook) (models.Webhook, error) { func (p *provider) UpdateWebhook(webhook models.Webhook) (models.Webhook, error) {
webhook.UpdatedAt = time.Now().Unix() webhook.UpdatedAt = time.Now().Unix()
webhookCollection, _ := p.db.Collection(nil, models.Collections.Webhook)
meta, err := webhookCollection.UpdateDocument(nil, webhook.Key, webhook)
if err != nil {
return webhook, err
}
webhook.Key = meta.Key
webhook.ID = meta.ID.String()
return webhook, nil return webhook, nil
} }
// ListWebhooks to list webhook // ListWebhooks to list webhook
func (p *provider) ListWebhook(pagination model.Pagination) (*model.Webhooks, error) { func (p *provider) ListWebhook(pagination model.Pagination) (*model.Webhooks, error) {
return nil, nil webhooks := []*model.Webhook{}
query := fmt.Sprintf("FOR d in %s SORT d.created_at DESC LIMIT %d, %d RETURN d", models.Collections.Webhook, pagination.Offset, pagination.Limit)
ctx := driver.WithQueryFullCount(context.Background())
cursor, err := p.db.Query(ctx, query, nil)
if err != nil {
return nil, err
}
defer cursor.Close()
paginationClone := pagination
paginationClone.Total = cursor.Statistics().FullCount()
for {
var webhook models.Webhook
meta, err := cursor.ReadDocument(nil, &webhook)
if arangoDriver.IsNoMoreDocuments(err) {
break
} else if err != nil {
return nil, err
}
if meta.Key != "" {
webhooks = append(webhooks, webhook.AsAPIWebhook())
}
}
return &model.Webhooks{
Pagination: &paginationClone,
Webhooks: webhooks,
}, nil
} }
// GetWebhookByID to get webhook by id // GetWebhookByID to get webhook by id
func (p *provider) GetWebhookByID(webhookID string) (models.Webhook, error) { func (p *provider) GetWebhookByID(webhookID string) (models.Webhook, error) {
return models.Webhook{}, nil var webhook models.Webhook
query := fmt.Sprintf("FOR d in %s FILTER d._id == @webhook_id RETURN d", models.Collections.Webhook)
bindVars := map[string]interface{}{
"webhook_id": webhookID,
}
cursor, err := p.db.Query(nil, query, bindVars)
if err != nil {
return webhook, err
}
defer cursor.Close()
for {
if !cursor.HasMore() {
if webhook.Key == "" {
return webhook, fmt.Errorf("webhook not found")
}
break
}
_, err := cursor.ReadDocument(nil, &webhook)
if err != nil {
return webhook, err
}
}
return webhook, nil
} }
// GetWebhookByEventName to get webhook by event_name // GetWebhookByEventName to get webhook by event_name
func (p *provider) GetWebhookByEventName(eventName string) (models.Webhook, error) { func (p *provider) GetWebhookByEventName(eventName string) (models.Webhook, error) {
return models.Webhook{}, nil var webhook models.Webhook
query := fmt.Sprintf("FOR d in %s FILTER d.event_name == @event_name RETURN d", models.Collections.Webhook)
bindVars := map[string]interface{}{
"event_name": eventName,
}
cursor, err := p.db.Query(nil, query, bindVars)
if err != nil {
return webhook, err
}
defer cursor.Close()
for {
if !cursor.HasMore() {
if webhook.Key == "" {
return webhook, fmt.Errorf("webhook not found")
}
break
}
_, err := cursor.ReadDocument(nil, &webhook)
if err != nil {
return webhook, err
}
}
return webhook, nil
} }
// DeleteWebhook to delete webhook // DeleteWebhook to delete webhook
func (p *provider) DeleteWebhook(webhookID string) error { func (p *provider) DeleteWebhook(webhook models.Webhook) error {
webhookCollection, _ := p.db.Collection(nil, models.Collections.Webhook)
_, err := webhookCollection.RemoveDocument(nil, webhook.Key)
if err != nil {
return err
}
return nil return nil
} }

View File

@ -39,7 +39,7 @@ func (p *provider) ListWebhookLogs(pagination model.Pagination, webhookID string
if webhookID != "" { if webhookID != "" {
query = fmt.Sprintf("FOR d in %s FILTER d.webhook_id == @webhookID SORT d.created_at DESC LIMIT %d, %d RETURN d", models.Collections.WebhookLog, pagination.Offset, pagination.Limit) query = fmt.Sprintf("FOR d in %s FILTER d.webhook_id == @webhookID SORT d.created_at DESC LIMIT %d, %d RETURN d", models.Collections.WebhookLog, pagination.Offset, pagination.Limit)
bindVariables = map[string]interface{}{ bindVariables = map[string]interface{}{
webhookID: webhookID, "webhook_id": webhookID,
} }
} }
ctx := driver.WithQueryFullCount(context.Background()) ctx := driver.WithQueryFullCount(context.Background())

View File

@ -42,6 +42,6 @@ func (p *provider) GetWebhookByEventName(eventName string) (models.Webhook, erro
} }
// DeleteWebhook to delete webhook // DeleteWebhook to delete webhook
func (p *provider) DeleteWebhook(webhookID string) error { func (p *provider) DeleteWebhook(webhook models.Webhook) error {
return nil return nil
} }

View File

@ -42,6 +42,6 @@ func (p *provider) GetWebhookByEventName(eventName string) (models.Webhook, erro
} }
// DeleteWebhook to delete webhook // DeleteWebhook to delete webhook
func (p *provider) DeleteWebhook(webhookID string) error { func (p *provider) DeleteWebhook(webhook models.Webhook) error {
return nil return nil
} }

View File

@ -42,6 +42,6 @@ func (p *provider) GetWebhookByEventName(eventName string) (models.Webhook, erro
} }
// DeleteWebhook to delete webhook // DeleteWebhook to delete webhook
func (p *provider) DeleteWebhook(webhookID string) error { func (p *provider) DeleteWebhook(webhook models.Webhook) error {
return nil return nil
} }

View File

@ -53,7 +53,7 @@ type Provider interface {
// GetWebhookByEventName to get webhook by event_name // GetWebhookByEventName to get webhook by event_name
GetWebhookByEventName(eventName string) (models.Webhook, error) GetWebhookByEventName(eventName string) (models.Webhook, error)
// DeleteWebhook to delete webhook // DeleteWebhook to delete webhook
DeleteWebhook(webhookID string) error DeleteWebhook(webhook models.Webhook) error
// AddWebhookLog to add webhook log // AddWebhookLog to add webhook log
AddWebhookLog(webhookLog models.WebhookLog) (models.WebhookLog, error) AddWebhookLog(webhookLog models.WebhookLog) (models.WebhookLog, error)

View File

@ -87,8 +87,8 @@ func (p *provider) GetWebhookByEventName(eventName string) (models.Webhook, erro
} }
// DeleteWebhook to delete webhook // DeleteWebhook to delete webhook
func (p *provider) DeleteWebhook(webhookID string) error { func (p *provider) DeleteWebhook(webhook models.Webhook) error {
result := p.db.Delete(&models.Webhook{}, webhookID) result := p.db.Delete(&webhook)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }