-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds subscription types and db setters/getters
- Loading branch information
Showing
7 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func (ms *MongoStorage) SetSubscription(subscription *Subscription) error { | ||
ms.keysLock.Lock() | ||
defer ms.keysLock.Unlock() | ||
// create a context with a timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// prepare the document to be updated in the database modifying only the | ||
// fields that have changed | ||
// define 'active' parameter to be updated always to update it even its new | ||
// value is false | ||
updateDoc, err := dynamicUpdateDocument(subscription, []string{"active"}) | ||
if err != nil { | ||
return err | ||
} | ||
// set upsert to true to create the document if it doesn't exist | ||
opts := options.Update().SetUpsert(true) | ||
if _, err := ms.organizations.UpdateOne(ctx, bson.M{"_id": subscription.ID}, updateDoc, opts); err != nil { | ||
return err | ||
} | ||
if err != nil { | ||
log.Printf("Failed to add subscription: %v", err) | ||
return errors.New("failed to add subscription") | ||
} | ||
return nil | ||
} | ||
|
||
func (ms *MongoStorage) Subscription(subscriptionID string) (*Subscription, error) { | ||
ms.keysLock.RLock() | ||
defer ms.keysLock.RUnlock() | ||
// create a context with a timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// find the subscription in the database | ||
filter := bson.M{"_id": subscriptionID} | ||
subscription := &Subscription{} | ||
err := ms.subscriptions.FindOne(ctx, filter).Decode(subscription) | ||
if err != nil { | ||
if err == mongo.ErrNoDocuments { | ||
return nil, ErrNotFound // Subscription not found | ||
} | ||
return nil, errors.New("failed to get subscription") | ||
} | ||
return subscription, nil | ||
} | ||
|
||
func (ms *MongoStorage) DelSubscription(subscription *Subscription) error { | ||
ms.keysLock.Lock() | ||
defer ms.keysLock.Unlock() | ||
// create a context with a timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// delete the organization from the database | ||
_, err := ms.organizations.DeleteOne(ctx, bson.M{"_id": subscription.ID}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters