feat: implement resolvers

This commit is contained in:
Lakhan Samani
2022-07-10 21:49:33 +05:30
parent 09c3eafe6b
commit e91a819067
87 changed files with 1807 additions and 428 deletions

View File

@@ -13,7 +13,7 @@ import (
)
// AddWebhook to add webhook
func (p *provider) AddWebhook(webhook models.Webhook) (models.Webhook, error) {
func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*model.Webhook, error) {
if webhook.ID == "" {
webhook.ID = uuid.New().String()
}
@@ -21,36 +21,36 @@ func (p *provider) AddWebhook(webhook models.Webhook) (models.Webhook, error) {
webhook.Key = webhook.ID
webhook.CreatedAt = time.Now().Unix()
webhook.UpdatedAt = time.Now().Unix()
webhookCollection, _ := p.db.Collection(nil, models.Collections.Webhook)
_, err := webhookCollection.CreateDocument(nil, webhook)
webhookCollection, _ := p.db.Collection(ctx, models.Collections.Webhook)
_, err := webhookCollection.CreateDocument(ctx, webhook)
if err != nil {
return webhook, err
return nil, err
}
return webhook, nil
return webhook.AsAPIWebhook(), nil
}
// UpdateWebhook to update webhook
func (p *provider) UpdateWebhook(webhook models.Webhook) (models.Webhook, error) {
func (p *provider) UpdateWebhook(ctx context.Context, webhook models.Webhook) (*model.Webhook, error) {
webhook.UpdatedAt = time.Now().Unix()
webhookCollection, _ := p.db.Collection(nil, models.Collections.Webhook)
meta, err := webhookCollection.UpdateDocument(nil, webhook.Key, webhook)
webhookCollection, _ := p.db.Collection(ctx, models.Collections.Webhook)
meta, err := webhookCollection.UpdateDocument(ctx, webhook.Key, webhook)
if err != nil {
return webhook, err
return nil, err
}
webhook.Key = meta.Key
webhook.ID = meta.ID.String()
return webhook, nil
return webhook.AsAPIWebhook(), nil
}
// ListWebhooks to list webhook
func (p *provider) ListWebhook(pagination model.Pagination) (*model.Webhooks, error) {
func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination) (*model.Webhooks, error) {
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)
sctx := driver.WithQueryFullCount(ctx)
cursor, err := p.db.Query(sctx, query, nil)
if err != nil {
return nil, err
}
@@ -61,7 +61,7 @@ func (p *provider) ListWebhook(pagination model.Pagination) (*model.Webhooks, er
for {
var webhook models.Webhook
meta, err := cursor.ReadDocument(nil, &webhook)
meta, err := cursor.ReadDocument(ctx, &webhook)
if arangoDriver.IsNoMoreDocuments(err) {
break
@@ -81,67 +81,67 @@ func (p *provider) ListWebhook(pagination model.Pagination) (*model.Webhooks, er
}
// GetWebhookByID to get webhook by id
func (p *provider) GetWebhookByID(webhookID string) (models.Webhook, error) {
func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model.Webhook, error) {
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)
cursor, err := p.db.Query(ctx, query, bindVars)
if err != nil {
return webhook, err
return nil, err
}
defer cursor.Close()
for {
if !cursor.HasMore() {
if webhook.Key == "" {
return webhook, fmt.Errorf("webhook not found")
return nil, fmt.Errorf("webhook not found")
}
break
}
_, err := cursor.ReadDocument(nil, &webhook)
_, err := cursor.ReadDocument(ctx, &webhook)
if err != nil {
return webhook, err
return nil, err
}
}
return webhook, nil
return webhook.AsAPIWebhook(), nil
}
// GetWebhookByEventName to get webhook by event_name
func (p *provider) GetWebhookByEventName(eventName string) (models.Webhook, error) {
func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string) (*model.Webhook, error) {
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)
cursor, err := p.db.Query(ctx, query, bindVars)
if err != nil {
return webhook, err
return nil, err
}
defer cursor.Close()
for {
if !cursor.HasMore() {
if webhook.Key == "" {
return webhook, fmt.Errorf("webhook not found")
return nil, fmt.Errorf("webhook not found")
}
break
}
_, err := cursor.ReadDocument(nil, &webhook)
_, err := cursor.ReadDocument(ctx, &webhook)
if err != nil {
return webhook, err
return nil, err
}
}
return webhook, nil
return webhook.AsAPIWebhook(), nil
}
// DeleteWebhook to delete webhook
func (p *provider) DeleteWebhook(webhook models.Webhook) error {
webhookCollection, _ := p.db.Collection(nil, models.Collections.Webhook)
_, err := webhookCollection.RemoveDocument(nil, webhook.Key)
func (p *provider) DeleteWebhook(ctx context.Context, webhook *model.Webhook) error {
webhookCollection, _ := p.db.Collection(ctx, models.Collections.Webhook)
_, err := webhookCollection.RemoveDocument(ctx, webhook.ID)
if err != nil {
return err
}