feat: add mongodb database methods for webhook
This commit is contained in:
parent
bb51775d34
commit
09c3eafe6b
|
@ -6,6 +6,8 @@ import (
|
||||||
"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"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddWebhook to add webhook
|
// AddWebhook to add webhook
|
||||||
|
@ -17,31 +19,95 @@ 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(models.Collections.Webhook, options.Collection())
|
||||||
|
_, err := webhookCollection.InsertOne(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(models.Collections.Webhook, options.Collection())
|
||||||
|
_, err := webhookCollection.UpdateOne(nil, bson.M{"_id": bson.M{"$eq": webhook.ID}}, bson.M{"$set": webhook}, options.MergeUpdateOptions())
|
||||||
|
if err != nil {
|
||||||
|
return webhook, err
|
||||||
|
}
|
||||||
|
|
||||||
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
|
var webhooks []*model.Webhook
|
||||||
|
opts := options.Find()
|
||||||
|
opts.SetLimit(pagination.Limit)
|
||||||
|
opts.SetSkip(pagination.Offset)
|
||||||
|
opts.SetSort(bson.M{"created_at": -1})
|
||||||
|
|
||||||
|
paginationClone := pagination
|
||||||
|
|
||||||
|
webhookCollection := p.db.Collection(models.Collections.Webhook, options.Collection())
|
||||||
|
count, err := webhookCollection.CountDocuments(nil, bson.M{}, options.Count())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
paginationClone.Total = count
|
||||||
|
|
||||||
|
cursor, err := webhookCollection.Find(nil, bson.M{}, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer cursor.Close(nil)
|
||||||
|
|
||||||
|
for cursor.Next(nil) {
|
||||||
|
var webhook models.Webhook
|
||||||
|
err := cursor.Decode(&webhook)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
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
|
||||||
|
webhookCollection := p.db.Collection(models.Collections.Webhook, options.Collection())
|
||||||
|
err := webhookCollection.FindOne(nil, bson.M{"_id": webhookID}).Decode(&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
|
||||||
|
webhookCollection := p.db.Collection(models.Collections.Webhook, options.Collection())
|
||||||
|
err := webhookCollection.FindOne(nil, bson.M{"event_name": eventName}).Decode(&webhook)
|
||||||
|
if err != nil {
|
||||||
|
return webhook, err
|
||||||
|
}
|
||||||
|
return webhook, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteWebhook to delete webhook
|
// DeleteWebhook to delete webhook
|
||||||
func (p *provider) DeleteWebhook(webhook models.Webhook) error {
|
func (p *provider) DeleteWebhook(webhook models.Webhook) error {
|
||||||
|
webhookCollection := p.db.Collection(models.Collections.Webhook, options.Collection())
|
||||||
|
_, err := webhookCollection.DeleteOne(nil, bson.M{"_id": webhook.ID}, options.Delete())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user