From 32d3f1331cef924f366694bf8420dcd171520623 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 30 Nov 2024 20:49:26 +0200 Subject: [PATCH] Add filtering expenses based on db.SettingsTypeFilter on statistics/type route handler --- handlers/statistics.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/handlers/statistics.go b/handlers/statistics.go index 640debc..3bbd634 100644 --- a/handlers/statistics.go +++ b/handlers/statistics.go @@ -27,13 +27,22 @@ func StatisticsGetAllSpendingsForTypes(c *gin.Context) { return } dbc := db.Connect() + var settingsTypeFilter []*db.SettingsTypeFilter + if err := dbc.Find(&settingsTypeFilter, db.SettingsTypeFilter{UserID: userID}).Error; err != nil { + c.JSON(500, types.ErrorResponse{Message: err.Error()}) + return + } + var filerTypeIDs []uint + for _, typeFilter := range settingsTypeFilter { + filerTypeIDs = append(filerTypeIDs, typeFilter.TypeID) + } var userTypes []*db.Type if err := dbc.Find(&userTypes, db.Type{UserID: userID}).Error; err != nil { c.JSON(500, types.ErrorResponse{Message: err.Error()}) return } var userExpenses []*db.Expense - if err := dbc.Preload("Card.Currency").Find(&userExpenses, db.Expense{UserID: userID}).Error; err != nil { + if err := dbc.Not(map[string]interface{}{"type_id": filerTypeIDs}).Preload("Card.Currency").Find(&userExpenses, db.Expense{UserID: userID}).Error; err != nil { c.JSON(500, types.ErrorResponse{Message: err.Error()}) return }