Skip to content

Commit

Permalink
db injected as dependency in minter function
Browse files Browse the repository at this point in the history
  • Loading branch information
PrantaDas committed Sep 12, 2024
1 parent 245ef07 commit ca1b250
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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...")
Expand Down
12 changes: 6 additions & 6 deletions internal/db/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion internal/worker/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit ca1b250

Please sign in to comment.