2022-09-05 05:27:52 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MongoDBStorage struct {
|
|
|
|
*mongo.Client
|
|
|
|
DbName string
|
|
|
|
Collections map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
|
|
|
var (
|
|
|
|
mongodb_host = cfg.GetString("storage.db.mongodb.host")
|
|
|
|
mongodb_port = cfg.GetString("storage.db.mongodb.port")
|
|
|
|
mongodb_dbname = cfg.GetString("storage.db.mongodb.db_name")
|
|
|
|
mongodb_events = cfg.GetString("storage.db.mongodb.collections.events")
|
|
|
|
)
|
|
|
|
|
|
|
|
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port))
|
|
|
|
if err != nil {
|
|
|
|
return MongoDBStorage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Connect(context.TODO())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return MongoDBStorage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := MongoDBStorage{
|
|
|
|
Client: client,
|
|
|
|
DbName: mongodb_dbname,
|
|
|
|
Collections: map[string]string{
|
|
|
|
"events": mongodb_events,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
//TODO Indexes
|
|
|
|
return storage, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s MongoDBStorage) CreateEvent(event Event) error {
|
|
|
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
|
|
|
if _, err := collection.InsertOne(context.TODO(), event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s MongoDBStorage) GetEvent(id string) (*Event, error) {
|
|
|
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
|
|
|
|
|
|
|
event := &Event{}
|
|
|
|
if err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(event); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s MongoDBStorage) GetEvents(namespaces []string) (events []Event, err error) {
|
|
|
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
|
|
|
|
|
|
|
var cur *mongo.Cursor
|
|
|
|
|
|
|
|
findOptions := options.Find()
|
|
|
|
//findOptions.SetSort(bson.D{{"startdate", 1}})
|
|
|
|
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
cur, err = collection.Find(context.TODO(), bson.D{}, findOptions)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("...")
|
|
|
|
fmt.Println(err)
|
|
|
|
return events, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cur, err = collection.Find(context.TODO(), bson.M{"namespace": bson.M{"$in": namespaces}}, findOptions)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error here")
|
|
|
|
fmt.Println(err)
|
|
|
|
return events, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for cur.Next(context.TODO()) {
|
|
|
|
var event Event
|
|
|
|
//var elem bson.M
|
|
|
|
|
|
|
|
if err := cur.Decode(&event); err != nil {
|
|
|
|
return events, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// bsonBytes, _ := bson.Marshal(elem)
|
|
|
|
// fmt.Println(string(bsonBytes))
|
|
|
|
// bson.Unmarshal(bsonBytes, &event)
|
|
|
|
|
|
|
|
events = append(events, event)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:00:35 +00:00
|
|
|
func (s MongoDBStorage) AddSubscription(eventid string, subscription Subscription) error {
|
2022-09-05 05:27:52 +00:00
|
|
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
|
|
|
|
|
|
|
filter := bson.M{"_id": eventid}
|
2022-10-17 03:00:35 +00:00
|
|
|
push := bson.M{"$push": bson.M{"subscriptions": subscription}}
|
2022-09-05 05:27:52 +00:00
|
|
|
|
|
|
|
_, err := collection.UpdateOne(context.TODO(), filter, push)
|
|
|
|
return err
|
|
|
|
}
|
2023-02-01 09:09:43 +00:00
|
|
|
|
2023-02-01 15:04:10 +00:00
|
|
|
func (s MongoDBStorage) UpdateSubscription(eventid string, subscriber string, deletesubscription Subscription) error {
|
2023-02-01 09:09:43 +00:00
|
|
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
|
|
|
|
2023-02-01 15:04:10 +00:00
|
|
|
event := &Event{}
|
|
|
|
if errr := collection.FindOne(context.TODO(), bson.M{"_id": eventid}).Decode(event); errr != nil {
|
|
|
|
return errr
|
|
|
|
}
|
2023-02-01 09:09:43 +00:00
|
|
|
|
2023-02-01 15:04:10 +00:00
|
|
|
for i := range event.Subscriptions {
|
|
|
|
if event.Subscriptions[i].Subscriber == subscriber {
|
|
|
|
filter := bson.M{"_id": eventid}
|
2023-02-15 09:14:06 +00:00
|
|
|
push := bson.M{"$push": bson.M{"deletedsubscriptions": deletesubscription}}
|
2023-02-01 15:04:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 13:57:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-01 08:58:37 +00:00
|
|
|
func (s MongoDBStorage) UpdateEvent(event Event) error {
|
|
|
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
|
|
|
if _, err := collection.ReplaceOne(context.TODO(), bson.M{"_id": event.ID}, event); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:04:10 +00:00
|
|
|
return nil
|
2023-02-01 09:09:43 +00:00
|
|
|
}
|
2023-09-06 11:34:43 +00:00
|
|
|
|
2023-12-06 15:12:29 +00:00
|
|
|
func (s MongoDBStorage) GetSubscriber(subscriber string) ([]Subscription, error) {
|
2024-09-02 19:40:14 +00:00
|
|
|
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
|
2023-12-08 12:33:13 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return subscriptions, err
|
|
|
|
// }
|
2024-09-02 19:40:14 +00:00
|
|
|
|
|
|
|
// for cur.Next(context.TODO()) {
|
2023-12-08 12:33:13 +00:00
|
|
|
// var event Event
|
2024-09-02 19:40:14 +00:00
|
|
|
// var elem bson.M
|
|
|
|
|
|
|
|
// if err := cur.Decode(&elem); err != nil {
|
2023-12-08 12:33:13 +00:00
|
|
|
// return subscriptions, err
|
|
|
|
// }
|
|
|
|
// for i := range event.Subscriptions {
|
|
|
|
// if event.Subscriptions[i].Subscriber == subscriber {
|
|
|
|
// subscriptions = append(subscriptions, event.Subscriptions[i])
|
|
|
|
// }
|
|
|
|
// }
|
2024-09-02 19:40:14 +00:00
|
|
|
// // bsonBytes, _ := bson.Marshal(elem)
|
|
|
|
// // bson.Unmarshal(bsonBytes, &event)
|
|
|
|
|
|
|
|
// // subscriptions = append(subscriptions, event.Subscriptions[1])
|
|
|
|
|
2023-12-08 12:33:13 +00:00
|
|
|
// }
|
2024-09-02 19:40:14 +00:00
|
|
|
|
2023-12-08 12:33:13 +00:00
|
|
|
// return subscriptions, nil
|
2023-09-06 11:34:43 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 15:12:29 +00:00
|
|
|
func (s MongoDBStorage) GetSubscriptionByUser(subscriber string) ([]Subscription, error) {
|
2024-09-02 19:40:14 +00:00
|
|
|
events, err := s.GetSubscriber(subscriber)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(events)
|
|
|
|
return events, nil
|
|
|
|
//return nil, nil
|
2023-09-06 11:34:43 +00:00
|
|
|
}
|