mirror of
https://github.com/gabehf/BudgetBuddy.git
synced 2026-03-13 09:20:37 -07:00
api for initial widgets
This commit is contained in:
parent
36f751a69f
commit
4aa8a2f822
14 changed files with 864 additions and 11 deletions
40
widgets/balanceInternal.go
Normal file
40
widgets/balanceInternal.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package widgets
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jacobmveber-01839764/BudgetBuddy/db"
|
||||
"github.com/jacobmveber-01839764/BudgetBuddy/money"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func addToBalance(user db.UserSchema, amount money.Money) bool {
|
||||
|
||||
newBalance := money.Add(amount, user.Balance)
|
||||
|
||||
// get collection handle from db
|
||||
var userCollection = db.Client.Database("budgetbuddy").Collection("users")
|
||||
|
||||
filter := bson.D{primitive.E{Key: "session", Value: user.Session}}
|
||||
opts := options.Update().SetUpsert(true)
|
||||
update := bson.D{primitive.E{Key: "$set", Value: bson.D{primitive.E{Key: "balance", Value: newBalance}}}}
|
||||
_, err := userCollection.UpdateOne(context.TODO(), filter, update, opts)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func subtractFromBalance(user db.UserSchema, amount money.Money) bool {
|
||||
|
||||
// create money object to store in db
|
||||
newBalance := money.Subtract(amount, user.Balance)
|
||||
|
||||
// get collection handle from db
|
||||
var userCollection = db.Client.Database("budgetbuddy").Collection("users")
|
||||
|
||||
filter := bson.D{primitive.E{Key: "session", Value: user.Session}}
|
||||
opts := options.Update().SetUpsert(true)
|
||||
update := bson.D{primitive.E{Key: "$set", Value: bson.D{primitive.E{Key: "balance", Value: newBalance}}}}
|
||||
_, err := userCollection.UpdateOne(context.TODO(), filter, update, opts)
|
||||
return err == nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue