From ca1b250b864c08b62d99b0db16755899bb2f2356 Mon Sep 17 00:00:00 2001 From: Prantadas Date: Thu, 12 Sep 2024 15:51:57 +0600 Subject: [PATCH] db injected as dependency in minter function --- cmd/app/main.go | 6 ++++-- internal/db/mongodb.go | 12 ++++++------ internal/worker/minter.go | 3 ++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index de8c03e..19b0fb7 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -23,11 +23,13 @@ func main() { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() - _, err = db.ConnectToMongoDB(ctx) + client, err := db.ConnectToMongoDB(ctx) if err != nil { log.Fatal("Error connecting to MongoDB", err) } + persiter := db.NewMongoDBPersister(client) + dataChannel := make(chan []api.Collection, 4) defer close(dataChannel) @@ -38,7 +40,7 @@ func main() { go worker.FetchWorker(ctx, dataChannel) go worker.TaskProcessor(ctx, dataChannel, txChannel) - go worker.Minter(ctx, txChannel) + go worker.Minter(ctx, persiter, txChannel) <-ctx.Done() log.Println("Shuttiing down gracefully...") diff --git a/internal/db/mongodb.go b/internal/db/mongodb.go index 48c4bde..a69db71 100644 --- a/internal/db/mongodb.go +++ b/internal/db/mongodb.go @@ -53,8 +53,8 @@ func ConnectToMongoDB(ctx context.Context) (*mongo.Client, error) { return client, nil } -func GetTransactionsFromDb(ctx context.Context, client *mongo.Client, address string) (bool, error) { - coll := client.Database(os.Getenv("MONGODB_DATABASE")).Collection(os.Getenv("MONGODB_COLLECTION_TRANSACTION")) +func (client *MongoDBPersister) GetTransactionsFromDb(ctx context.Context, address string) (bool, error) { + coll := client.MongoClient.Database(os.Getenv("MONGODB_DATABASE")).Collection(os.Getenv("MONGODB_COLLECTION_TRANSACTION")) filter := bson.D{{Key: "address", Value: address}} result := coll.FindOne(ctx, filter) @@ -68,8 +68,8 @@ func GetTransactionsFromDb(ctx context.Context, client *mongo.Client, address st return true, nil } -func InsertTransactionToDb(ctx context.Context, client *mongo.Client, tx TransactionData) error { - coll := client.Database(os.Getenv("MONGODB_DATABASE")).Collection(os.Getenv("MONGODB_COLLECTION_TRANSACTION")) +func (client *MongoDBPersister) InsertTransactionToDb(ctx context.Context, tx TransactionData) error { + coll := client.MongoClient.Database(os.Getenv("MONGODB_DATABASE")).Collection(os.Getenv("MONGODB_COLLECTION_TRANSACTION")) document := bson.M{ "name": tx.Name, @@ -84,8 +84,8 @@ func InsertTransactionToDb(ctx context.Context, client *mongo.Client, tx Transac return nil } -func LogError(ctx context.Context, client *mongo.Client, err error) { - coll := client.Database(os.Getenv("MONGODB_DATABASE")).Collection(os.Getenv("MONGODB_COLLECTION_ERROR")) +func (client *MongoDBPersister) LogError(ctx context.Context, err error) { + coll := client.MongoClient.Database(os.Getenv("MONGODB_DATABASE")).Collection(os.Getenv("MONGODB_COLLECTION_ERROR")) document := bson.M{ "error": err.Error(), diff --git a/internal/worker/minter.go b/internal/worker/minter.go index d84fc39..cdc8d26 100644 --- a/internal/worker/minter.go +++ b/internal/worker/minter.go @@ -4,10 +4,11 @@ import ( "context" "fmt" "log" + "mintfun/internal/db" "mintfun/internal/helpers" ) -func Minter(ctx context.Context, txChan <-chan helpers.ProcessedData) { +func Minter(ctx context.Context, db *db.MongoDBPersister, txChan <-chan helpers.ProcessedData) { for { select { case <-ctx.Done():