add logic to delete and srore sub

This commit is contained in:
soukainna
2023-02-01 16:04:10 +01:00
parent ffa341b13d
commit 1ce72b2f88
10 changed files with 1751 additions and 103 deletions

View File

@@ -3,21 +3,22 @@ package storage
import "time"
type Event struct {
ID string `json:"id" bson:"_id"`
Namespace string `json:"namespace"`
Owners []string `json:"owners"`
RestrictedTo []string `json:"restricted_to"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Startdate time.Time `json:"startdate"`
Enddate time.Time `json:"enddate"`
Starttime string `json:"starttime"`
Endtime string `json:"endtime"`
Allday bool `json:"allday"`
MaxSubscribers int64 `json:"max_subscribers"`
Subscriptions []Subscription `json:"subscriptions" bson:"subscriptions,omitempty"`
Data map[string]any `json:"data"`
ID string `json:"id" bson:"_id"`
Namespace string `json:"namespace"`
Owners []string `json:"owners"`
RestrictedTo []string `json:"restricted_to"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Startdate time.Time `json:"startdate"`
Enddate time.Time `json:"enddate"`
Starttime string `json:"starttime"`
Endtime string `json:"endtime"`
Allday bool `json:"allday"`
MaxSubscribers int64 `json:"max_subscribers"`
Subscriptions []Subscription `json:"subscriptions" bson:"subscriptions,omitempty"`
DeletedSubscription []Subscription `json:"deletedsubscriptions" bson:"deletedsubscriptions,omitempty"`
Data map[string]any `json:"data"`
}
type Subscription struct {

View File

@@ -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
}

View File

@@ -11,6 +11,7 @@ type Storage interface {
GetEvent(string) (*Event, error)
GetEvents(namespaces []string) ([]Event, error)
AddSubscription(eventid string, subscription Subscription) error
UpdateSubscription(eventid string, subscriber string, deletesubscription Subscription) error
}
type StorageImpl struct {
}