Initial commit
This commit is contained in:
28
storage/events.go
Normal file
28
storage/events.go
Normal file
@@ -0,0 +1,28 @@
|
||||
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"`
|
||||
Subscribers []string `json:"subscribers" bson:"subscribers,omitempty`
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
|
||||
func (e Event) RemainingSubscriptions() int {
|
||||
if e.MaxSubscribers == 0 {
|
||||
return 999
|
||||
}
|
||||
return int(e.MaxSubscribers) - len(e.Subscribers)
|
||||
}
|
||||
122
storage/mongodb.go
Normal file
122
storage/mongodb.go
Normal file
@@ -0,0 +1,122 @@
|
||||
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
|
||||
}
|
||||
|
||||
// fmt.Println(elem)
|
||||
|
||||
// bsonBytes, _ := bson.Marshal(elem)
|
||||
// fmt.Println(string(bsonBytes))
|
||||
// bson.Unmarshal(bsonBytes, &event)
|
||||
|
||||
events = append(events, event)
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) AddSubscriber(eventid string, subscriber string) error {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["events"])
|
||||
|
||||
filter := bson.M{"_id": eventid}
|
||||
push := bson.M{"$push": bson.M{"subscribers": subscriber}}
|
||||
|
||||
_, err := collection.UpdateOne(context.TODO(), filter, push)
|
||||
return err
|
||||
}
|
||||
30
storage/storage.go
Normal file
30
storage/storage.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
CreateEvent(Event) error
|
||||
GetEvent(string) (*Event, error)
|
||||
GetEvents(namespaces []string) ([]Event, error)
|
||||
AddSubscriber(eventid string, subscriber string) error
|
||||
}
|
||||
type StorageImpl struct {
|
||||
}
|
||||
|
||||
func NewStorage(cfg *viper.Viper) (Storage, error) {
|
||||
var (
|
||||
storage_type = cfg.GetString("storage.db.type")
|
||||
)
|
||||
|
||||
switch storage_type {
|
||||
case "mongodb":
|
||||
s, err := NewMongoDBStorage(cfg)
|
||||
return s, err
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user