new endpoints, budget adjustment

This commit is contained in:
Gabe Farrell 2023-04-11 22:41:39 -04:00
parent cc36b80e72
commit c4047a36cc
9 changed files with 276 additions and 20 deletions

View file

@ -22,6 +22,8 @@ type BudgetResponse struct {
BudgetCategories map[string]money.Money `json:"budget_categories"`
// categories in the budget
Categories []string `json:"categories"`
// total expenses by category
ExpensesByCategory map[string]money.Money `json:"expenses_by_category"`
// transactions mapped to a category
Expenses map[string][]db.Transaction `json:"expenses"`
}
@ -53,11 +55,13 @@ func GetBudget(w http.ResponseWriter, r *http.Request) {
}
response.Categories = cats
response.Expenses = make(map[string][]db.Transaction)
response.ExpensesByCategory = make(map[string]money.Money)
for _, e := range user.Expenses {
if response.Expenses[e.Category] == nil {
response.Expenses[e.Category] = make([]db.Transaction, 0)
}
response.Expenses[e.Category] = append(response.Expenses[e.Category], e)
response.ExpensesByCategory[e.Category] = money.Add(e.Amount, response.ExpensesByCategory[e.Category])
}
response.Status = 200

View file

@ -41,7 +41,7 @@ func GetMonthExpenses(w http.ResponseWriter, r *http.Request) {
if user.Expenses[i].Timestamp < time.Now().Add(-30*24*time.Hour).Unix() {
break
}
total = money.Add(total, user.Expenses[i].Amount)
total = money.Add(user.Expenses[i].Amount, total)
}
ret, err := json.Marshal(total)

View file

@ -153,7 +153,7 @@ func NewTransaction(w http.ResponseWriter, r *http.Request) {
update := bson.D{primitive.E{Key: "$set", Value: bson.D{primitive.E{Key: r.FormValue("type"), Value: newArr}}}}
userCollection.UpdateOne(context.TODO(), filter, update, opts)
w.Write([]byte("{\"status\": 200}"))
w.Write([]byte("{\"status\":200}"))
}
func NewRecurring(w http.ResponseWriter, r *http.Request) {
@ -243,6 +243,6 @@ func NewRecurring(w http.ResponseWriter, r *http.Request) {
update := bson.D{primitive.E{Key: "$set", Value: bson.D{primitive.E{Key: r.FormValue("type"), Value: newArr}}}}
userCollection.UpdateOne(context.TODO(), filter, update, opts)
w.Write([]byte("{\"status\": 200}"))
w.Write([]byte("{\"status\":200}"))
}