diff --git a/internal/handler/dashboard.go b/internal/handler/dashboard.go index 1a920d3..e3f29ce 100644 --- a/internal/handler/dashboard.go +++ b/internal/handler/dashboard.go @@ -1,18 +1,45 @@ package handler import ( + "log/slog" "net/http" + "git.juancwu.dev/juancwu/budgit/internal/ctxkeys" + "git.juancwu.dev/juancwu/budgit/internal/service" "git.juancwu.dev/juancwu/budgit/internal/ui" "git.juancwu.dev/juancwu/budgit/internal/ui/pages" ) -type dashboardHandler struct{} +type dashboardHandler struct { + spaceService *service.SpaceService + expenseService *service.ExpenseService +} -func NewDashboardHandler() *dashboardHandler { - return &dashboardHandler{} +func NewDashboardHandler(ss *service.SpaceService, es *service.ExpenseService) *dashboardHandler { + return &dashboardHandler{ + spaceService: ss, + expenseService: es, + } } func (h *dashboardHandler) DashboardPage(w http.ResponseWriter, r *http.Request) { - ui.Render(w, r, pages.Dashboard()) + user := ctxkeys.User(r.Context()) + spaces, err := h.spaceService.GetSpacesForUser(user.ID) + if err != nil { + slog.Error("failed to get spaces for user", "error", err, "user_id", user.ID) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + + var totalBalance int + for _, space := range spaces { + balance, err := h.expenseService.GetBalanceForSpace(space.ID) + if err != nil { + slog.Error("failed to get balance for space", "error", err, "space_id", space.ID) + continue + } + totalBalance += balance + } + + ui.Render(w, r, pages.Dashboard(spaces, totalBalance)) } diff --git a/internal/routes/routes.go b/internal/routes/routes.go index 00a3cb2..35d115c 100644 --- a/internal/routes/routes.go +++ b/internal/routes/routes.go @@ -13,7 +13,7 @@ import ( func SetupRoutes(a *app.App) http.Handler { auth := handler.NewAuthHandler(a.AuthService, a.InviteService) home := handler.NewHomeHandler() - dashboard := handler.NewDashboardHandler() + dashboard := handler.NewDashboardHandler(a.SpaceService, a.ExpenseService) space := handler.NewSpaceHandler(a.SpaceService, a.TagService, a.ShoppingListService, a.ExpenseService, a.InviteService, a.EventBus) mux := http.NewServeMux() diff --git a/internal/ui/layouts/app.templ b/internal/ui/layouts/app.templ index 1586b0b..48f55a4 100644 --- a/internal/ui/layouts/app.templ +++ b/internal/ui/layouts/app.templ @@ -32,7 +32,6 @@ templ App(title string) { @icon.LayoutDashboard()