diff --git a/handlers/events.go b/handlers/events.go index 6a6e907..9808c57 100644 --- a/handlers/events.go +++ b/handlers/events.go @@ -59,6 +59,7 @@ func (h AgendaHandler) SubscribeEvent(eventid string, subscriber string, data ma subscription := storage.Subscription{ ID: id, Subscriber: subscriber, + EventID: eventid, Tags: []string{}, CreatedAt: now, Data: map[string]any{}, @@ -80,6 +81,7 @@ func (h AgendaHandler) DeleteSubscription(eventid string, subscriber string, dat deletesubscription := storage.Subscription{ ID: id, Subscriber: subscriber, + EventID: eventid, Tags: []string{}, CreatedAt: now, Data: map[string]any{}, @@ -101,6 +103,17 @@ func (h AgendaHandler) UpdateEvent(event storage.Event) (*storage.Event, error) return &event, nil } +func (h AgendaHandler) GetSubscriber(subscriber string) (results []storage.Subscription, err error) { + if subscriber == "" { + return nil, errors.New("missing subscriber") + } + results, err = h.storage.GetSubscriber(subscriber) + if err != nil { + return nil, err + } + return results, nil +} + func (h AgendaHandler) GetSubscriptionByUser(subscriber string) (results []storage.Subscription, err error) { if subscriber == "" { return nil, errors.New("missing subscriber") diff --git a/storage/mongodb.go b/storage/mongodb.go index 74a6e36..3205622 100644 --- a/storage/mongodb.go +++ b/storage/mongodb.go @@ -155,15 +155,41 @@ func (s MongoDBStorage) UpdateEvent(event Event) error { } func (s MongoDBStorage) GetSubscriber(subscriber string) ([]Subscription, error) { - // collection := s.Client.Database(s.DbName).Collection(s.Collections["events"]) - // subscriptions := []Subscription{} - // cur, err := collection.Find(context.TODO(), bson.M{"subscriptions": bson.M{"$elemMatch": bson.M{"subscriber": subscriber}}}) + collection := s.Client.Database(s.DbName).Collection(s.Collections["events"]) + subscriptions := []Subscription{} + + var cur *mongo.Cursor + // findOptions := options.Find() + + // cur, err := collection.Find(context.TODO(), bson.M{"subscriptions": subscriber}, findOptions) + + cur, err := collection.Find(context.TODO(), bson.M{"subscriptions": bson.M{"$elemMatch": bson.M{"subscriber": subscriber}}}) + if err != nil { + return subscriptions, err + } + for cur.Next(context.Background()) { + var event Event + if err := cur.Decode(&event); err != nil { + return subscriptions, err + } + for i := range event.Subscriptions { + if event.Subscriptions[i].Subscriber == subscriber { + subscriptions = append(subscriptions, event.Subscriptions[i]) + } + } + } + fmt.Println(subscriptions) + return subscriptions, nil + //return nil, nil // if err != nil { // return subscriptions, err // } - // for cur.Next(context.Background()) { + + // for cur.Next(context.TODO()) { // var event Event - // if err := cur.Decode(&event); err != nil { + // var elem bson.M + + // if err := cur.Decode(&elem); err != nil { // return subscriptions, err // } // for i := range event.Subscriptions { @@ -171,17 +197,38 @@ func (s MongoDBStorage) GetSubscriber(subscriber string) ([]Subscription, error) // subscriptions = append(subscriptions, event.Subscriptions[i]) // } // } + // // bsonBytes, _ := bson.Marshal(elem) + // // bson.Unmarshal(bsonBytes, &event) + + // // subscriptions = append(subscriptions, event.Subscriptions[1]) + // } + // return subscriptions, nil - return nil, nil } func (s MongoDBStorage) GetSubscriptionByUser(subscriber string) ([]Subscription, error) { - // events, err := s.GetSubscriber(subscriber) - // if err != nil { - // panic(err) - // } - // fmt.Println(events) - // return events, nil - return nil, nil + events, err := s.GetSubscriber(subscriber) + if err != nil { + panic(err) + } + fmt.Println(events) + return events, nil + //return nil, nil } + +// [{32b91cca-da17-41ce-83fa-515a17a90fcf subscription id dans event + +// ca7f0a47-59d3-4bb8-ab12-756017d5ec9b id benef + +// [] 2024-07-23 12:53:45.409 +0000 UTC + +// map[subscribed_by:map[group:map[id:483280d0-db2d-4f06-b361-02e4be5012d2 name:COOPGO] + +// user:map[display_name:Soukaina lafdili + +// email:soukaina.lafdili@coopgo.fr + +// id:979888c6-656f-4af7-b7a4-97a02a3cc13d]]]}] + +// mongo: no documents in result diff --git a/storage/storage.go b/storage/storage.go index cba82e7..ffabb0f 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -13,6 +13,7 @@ type Storage interface { AddSubscription(eventid string, subscription Subscription) error UpdateSubscription(eventid string, subscriber string, deletesubscription Subscription) error UpdateEvent(Event) error + GetSubscriber(subscriber string) ([]Subscription, error) GetSubscriptionByUser(subscriber string) ([]Subscription, error) } type StorageImpl struct {