add logic to delete and srore sub
This commit is contained in:
@@ -121,12 +121,25 @@ func (s MongoDBStorage) AddSubscription(eventid string, subscription Subscriptio
|
||||
return err
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) RemoveSubscription(subscriptionid string, subscription Subscription) error {
|
||||
func (s MongoDBStorage) UpdateSubscription(eventid string, subscriber string, deletesubscription Subscription) error {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
||||
|
||||
filter := bson.M{"_id": subscriptionid}
|
||||
push := bson.M{"$pull": bson.M{"subscriptions": subscription}}
|
||||
event := &Event{}
|
||||
if errr := collection.FindOne(context.TODO(), bson.M{"_id": eventid}).Decode(event); errr != nil {
|
||||
return errr
|
||||
}
|
||||
|
||||
_, err := collection.UpdateOne(context.TODO(), filter, push)
|
||||
return err
|
||||
for i := range event.Subscriptions {
|
||||
if event.Subscriptions[i].Subscriber == subscriber {
|
||||
filter := bson.M{"_id": eventid}
|
||||
push := bson.M{"$push": bson.M{"deletesubscriptions": deletesubscription}}
|
||||
pull := bson.M{"$pull": bson.M{"subscriptions": bson.M{"subscriber": subscriber}}}
|
||||
_, er := collection.UpdateOne(context.TODO(), filter, push)
|
||||
if _, err := collection.UpdateOne(context.TODO(), filter, pull); err != nil {
|
||||
return err
|
||||
}
|
||||
return er
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user