Skip to content

Commit

Permalink
feat(core): add bridge-database dep
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanshkc committed Jul 12, 2022
1 parent 6aa1502 commit e3b4f77
Showing 1 changed file with 54 additions and 5 deletions.
59 changes: 54 additions & 5 deletions src/impl/bridges/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/shivanshkc/rosenbridge/src/core/models"
"github.com/shivanshkc/rosenbridge/src/mongodb"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

Expand All @@ -15,23 +16,71 @@ type Database struct{}

// NewDatabase is a constructor for *Database.
func NewDatabase() *Database {
return nil
return &Database{}
}

func (d *Database) InsertBridge(ctx context.Context, doc *models.BridgeDoc) error {
panic("implement me")
callCtx, cancelFunc := mongodb.GetTimeoutContext(ctx)
defer cancelFunc()

// Database call.
if _, err := mongodb.GetBridgesColl().InsertOne(callCtx, doc); err != nil {
return fmt.Errorf("error in GetBridgesColl().InsertOne call: %w", err)
}

return nil
}

func (d *Database) GetBridgesForClients(ctx context.Context, clientIDs []string) ([]*models.BridgeDoc, error) {
panic("implement me")
callCtx, cancelFunc := mongodb.GetTimeoutContext(ctx)
defer cancelFunc()

// Creating the required filter.
filter := bson.M{"client_id": bson.M{"$in": clientIDs}}

// Database call.
cursor, err := mongodb.GetBridgesColl().Find(callCtx, filter)
if err != nil {
return nil, fmt.Errorf("error in GetBridgesColl().Find call: %w", err)
}

// Getting documents from the cursor into the slice.
var docs []*models.BridgeDoc
if err := cursor.All(ctx, &docs); err != nil {
return nil, fmt.Errorf("error in cursor.All call: %w", err)
}

return docs, nil
}

func (d *Database) DeleteBridgeForNode(ctx context.Context, bridgeID string, nodeAddr string) error {
panic("implement me")
callCtx, cancelFunc := mongodb.GetTimeoutContext(ctx)
defer cancelFunc()

// Creating the required filter.
filter := bson.M{"bridge_id": bridgeID, "node_addr": nodeAddr}

// Database call.
if _, err := mongodb.GetBridgesColl().DeleteOne(callCtx, filter); err != nil {
return fmt.Errorf("error in GetBridgesColl().DeleteOne call: %w", err)
}

return nil
}

func (d *Database) DeleteBridgesForNode(ctx context.Context, bridgeIDs []string, nodeAddr string) error {
panic("implement me")
callCtx, cancelFunc := mongodb.GetTimeoutContext(ctx)
defer cancelFunc()

// Creating the required filter. Note that these conditions use the '&&' operator.
filter := bson.M{"node_addr": nodeAddr, "bridge_id": bson.M{"$in": bridgeIDs}}

// Database call.
if _, err := mongodb.GetBridgesColl().DeleteMany(callCtx, filter); err != nil {
return fmt.Errorf("error in GetBridgesColl().DeleteMany call: %w", err)
}

return nil
}

// CreateIndexes creates indexes as per the provided data on the "bridges" collection/table.
Expand Down

0 comments on commit e3b4f77

Please sign in to comment.