Skip to content

Commit

Permalink
Small cosmetics fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinB authored and noqqe committed May 22, 2023
1 parent f0fc422 commit 510e7fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/serra/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func getMongoDBURI() string {
if uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/#environment-variable")
}

return uri
}

Expand All @@ -22,7 +23,9 @@ func getCurrency() string {
case "USD":
return "$"
}

// default
LogMessage("Warning: You did not configure SERRA_CURRENCY. Assuming \"USD\"", "yellow")

return "USD"
}
33 changes: 14 additions & 19 deletions src/serra/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func getCurrencyField(foil bool) string {
}

func storageConnect() *mongo.Client {

uri := getMongoDBURI()

client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
LogMessage(fmt.Sprintf("Could not connect to mongodb at %s", uri), "red")
Expand Down Expand Up @@ -102,11 +102,10 @@ func (coll Collection) storageAddTotal(p PriceEntry) error {
}

func (coll Collection) storageFind(filter, sort bson.D) ([]Card, error) {

opts := options.Find().SetSort(sort)
cursor, err := coll.Find(context.TODO(), filter, opts)
if err != nil {
LogMessage("Could not query data due to connection errors to database", "red")
LogMessage(fmt.Sprintf("Could not query data due to connection errors to database: %s", err.Error()), "red")
os.Exit(1)
}

Expand All @@ -120,11 +119,11 @@ func (coll Collection) storageFind(filter, sort bson.D) ([]Card, error) {
}

func (coll Collection) storageFindSet(filter, sort bson.D) ([]Set, error) {

opts := options.Find().SetSort(sort)

cursor, err := coll.Find(context.TODO(), filter, opts)
if err != nil {
LogMessage("Could not query set data due to connection errors to database", "red")
LogMessage(fmt.Sprintf("Could not query set data due to connection errors to database: %s", err.Error()), "red")
os.Exit(1)
}

Expand All @@ -133,44 +132,41 @@ func (coll Collection) storageFindSet(filter, sort bson.D) ([]Set, error) {
log.Fatal(err)
return []Set{}, err
}
return results, nil

return results, nil
}

func (coll Collection) storageFindTotal() (Total, error) {

var total Total
err := coll.FindOne(context.TODO(), bson.D{{"_id", "1"}}).Decode(&total)

err := coll.FindOne(context.TODO(), bson.D{{"_id", "1"}}).Decode(&total)
if err != nil {
LogMessage("Could not query total data due to connection errors to database", "red")
LogMessage(fmt.Sprintf("Could not query total data due to connection errors to database: %s", err.Error()), "red")
os.Exit(1)
}
return total, nil

return total, nil
}

func (coll Collection) storageRemove(filter bson.M) error {

_, err := coll.DeleteOne(context.TODO(), filter)
if err != nil {
LogMessage("Could remove card data due to connection errors to database", "red")
LogMessage(fmt.Sprintf("Could remove card data due to connection errors to database: %s", err.Error()), "red")
os.Exit(1)
}
return nil

return nil
}

func (coll Collection) storageAggregate(pipeline mongo.Pipeline) ([]primitive.M, error) {

opts := options.Aggregate()

cursor, err := coll.Aggregate(
context.TODO(),
pipeline,
opts)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
LogMessage("Could not aggregate data due to connection errors to database", "red")
LogMessage(fmt.Sprintf("Could not aggregate data due to connection errors to database: %s", err.Error()), "red")
os.Exit(1)
}

Expand All @@ -180,20 +176,19 @@ func (coll Collection) storageAggregate(pipeline mongo.Pipeline) ([]primitive.M,
if err = cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
}
return results, nil

return results, nil
}

func (coll Collection) storageUpdate(filter, update bson.M) error {

// Call the driver's UpdateOne() method and pass filter and update to it
_, err := coll.UpdateOne(
context.Background(),
filter,
update,
)
if err != nil {
LogMessage("Could not update data due to connection errors to database", "red")
LogMessage(fmt.Sprintf("Could not update data due to connection errors to database: %s", err.Error()), "red")
os.Exit(1)
}

Expand Down

0 comments on commit 510e7fe

Please sign in to comment.