add id and fix err

This commit is contained in:
soukainna 2024-09-02 21:40:14 +02:00
parent 23c7b50d9b
commit 0e42bd7f79
3 changed files with 74 additions and 13 deletions

View File

@ -59,6 +59,7 @@ func (h AgendaHandler) SubscribeEvent(eventid string, subscriber string, data ma
subscription := storage.Subscription{ subscription := storage.Subscription{
ID: id, ID: id,
Subscriber: subscriber, Subscriber: subscriber,
EventID: eventid,
Tags: []string{}, Tags: []string{},
CreatedAt: now, CreatedAt: now,
Data: map[string]any{}, Data: map[string]any{},
@ -80,6 +81,7 @@ func (h AgendaHandler) DeleteSubscription(eventid string, subscriber string, dat
deletesubscription := storage.Subscription{ deletesubscription := storage.Subscription{
ID: id, ID: id,
Subscriber: subscriber, Subscriber: subscriber,
EventID: eventid,
Tags: []string{}, Tags: []string{},
CreatedAt: now, CreatedAt: now,
Data: map[string]any{}, Data: map[string]any{},
@ -101,6 +103,17 @@ func (h AgendaHandler) UpdateEvent(event storage.Event) (*storage.Event, error)
return &event, nil 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) { func (h AgendaHandler) GetSubscriptionByUser(subscriber string) (results []storage.Subscription, err error) {
if subscriber == "" { if subscriber == "" {
return nil, errors.New("missing subscriber") return nil, errors.New("missing subscriber")

View File

@ -155,15 +155,41 @@ func (s MongoDBStorage) UpdateEvent(event Event) error {
} }
func (s MongoDBStorage) GetSubscriber(subscriber string) ([]Subscription, error) { func (s MongoDBStorage) GetSubscriber(subscriber string) ([]Subscription, error) {
// collection := s.Client.Database(s.DbName).Collection(s.Collections["events"]) collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
// subscriptions := []Subscription{} subscriptions := []Subscription{}
// cur, err := collection.Find(context.TODO(), bson.M{"subscriptions": bson.M{"$elemMatch": bson.M{"subscriber": subscriber}}})
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 { // if err != nil {
// return subscriptions, err // return subscriptions, err
// } // }
// for cur.Next(context.Background()) {
// for cur.Next(context.TODO()) {
// var event Event // var event Event
// if err := cur.Decode(&event); err != nil { // var elem bson.M
// if err := cur.Decode(&elem); err != nil {
// return subscriptions, err // return subscriptions, err
// } // }
// for i := range event.Subscriptions { // for i := range event.Subscriptions {
@ -171,17 +197,38 @@ func (s MongoDBStorage) GetSubscriber(subscriber string) ([]Subscription, error)
// subscriptions = append(subscriptions, event.Subscriptions[i]) // subscriptions = append(subscriptions, event.Subscriptions[i])
// } // }
// } // }
// // bsonBytes, _ := bson.Marshal(elem)
// // bson.Unmarshal(bsonBytes, &event)
// // subscriptions = append(subscriptions, event.Subscriptions[1])
// } // }
// return subscriptions, nil // return subscriptions, nil
return nil, nil
} }
func (s MongoDBStorage) GetSubscriptionByUser(subscriber string) ([]Subscription, error) { func (s MongoDBStorage) GetSubscriptionByUser(subscriber string) ([]Subscription, error) {
// events, err := s.GetSubscriber(subscriber) events, err := s.GetSubscriber(subscriber)
// if err != nil { if err != nil {
// panic(err) panic(err)
// } }
// fmt.Println(events) fmt.Println(events)
// return events, nil return events, nil
return nil, 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

View File

@ -13,6 +13,7 @@ type Storage interface {
AddSubscription(eventid string, subscription Subscription) error AddSubscription(eventid string, subscription Subscription) error
UpdateSubscription(eventid string, subscriber string, deletesubscription Subscription) error UpdateSubscription(eventid string, subscriber string, deletesubscription Subscription) error
UpdateEvent(Event) error UpdateEvent(Event) error
GetSubscriber(subscriber string) ([]Subscription, error)
GetSubscriptionByUser(subscriber string) ([]Subscription, error) GetSubscriptionByUser(subscriber string) ([]Subscription, error)
} }
type StorageImpl struct { type StorageImpl struct {