From 2a6f96d7870be6b4c31c10de78ef3f24ad1697f8 Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Sun, 8 Jun 2025 15:35:21 +0200 Subject: [PATCH] wip --- internal/handler/root_and_404.go | 3 +- internal/service/dashboard.go | 59 +++++++++++++++++++++ internal/template/dashboard.templ | 9 ---- internal/template/dashboard/dashboard.templ | 23 ++++++++ internal/template/dashboard/default.go | 2 + internal/types/dashboard.go | 8 +++ 6 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 internal/service/dashboard.go delete mode 100644 internal/template/dashboard.templ create mode 100644 internal/template/dashboard/dashboard.templ create mode 100644 internal/template/dashboard/default.go create mode 100644 internal/types/dashboard.go diff --git a/internal/handler/root_and_404.go b/internal/handler/root_and_404.go index d346101..5073b31 100644 --- a/internal/handler/root_and_404.go +++ b/internal/handler/root_and_404.go @@ -4,6 +4,7 @@ import ( "net/http" "spend-sparrow/internal/handler/middleware" "spend-sparrow/internal/template" + "spend-sparrow/internal/template/dashboard" "github.com/a-h/templ" ) @@ -41,7 +42,7 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc { status = http.StatusNotFound } else { if user != nil { - comp = template.Dashboard() + comp = dashboard.Dashboard() } else { comp = template.Index() } diff --git a/internal/service/dashboard.go b/internal/service/dashboard.go new file mode 100644 index 0000000..f5eb770 --- /dev/null +++ b/internal/service/dashboard.go @@ -0,0 +1,59 @@ +package service + +import ( + "context" + "spend-sparrow/internal/types" + "time" + + "github.com/jmoiron/sqlx" +) + +type Dashboard struct { + db *sqlx.DB +} + +func NewDashboard(db *sqlx.DB) TreasureChest { + return TreasureChestImpl{ + db: db, + } +} + +func (s Dashboard) Summary(ctx context.Context, user *types.User, month time.Time) (*types.DashboardSummary, error) { + if user == nil { + return nil, ErrUnauthorized + } + + var summary types.DashboardSummary + + s.db.SelectContext(ctx, &summary.Expenses, ` + SELECT + FROM "transaction" + WHERE user_id = $1 + AND value < 0 + AND account_id IS NOT NULL + AND error IS NULL + AND date_trunc('month', date) = date_trund('month', $2)`, + user.Id, month) + + s.db.SelectContext(ctx, &summary.Income, ` + SELECT + FROM "transaction" + WHERE user_id = $1 + AND value > 0 + AND account_id IS NOT NULL + AND error IS NULL + AND date_trunc('month', date) = date_trund('month', $2)`, + user.Id, month) + + s.db.SelectContext(ctx, &summary.Savings, ` + SELECT + FROM "transaction" + WHERE user_id = $1 + AND value > 0 + AND treasure_chest_id IS NOT NULL + AND error IS NULL + AND date_trunc('month', date) = date_trund('month', $2)`, + user.Id, month) + + return &summary, nil +} diff --git a/internal/template/dashboard.templ b/internal/template/dashboard.templ deleted file mode 100644 index 54ef258..0000000 --- a/internal/template/dashboard.templ +++ /dev/null @@ -1,9 +0,0 @@ -package template - -templ Dashboard() { -
-

- Dashboard -

-
-} diff --git a/internal/template/dashboard/dashboard.templ b/internal/template/dashboard/dashboard.templ new file mode 100644 index 0000000..eb2dae0 --- /dev/null +++ b/internal/template/dashboard/dashboard.templ @@ -0,0 +1,23 @@ +package dashboard + +templ Dashboard() { +
+ + + @summaryCard() +
+} + +templ summaryCard() { +
+ Einnahmen + Gespart + Ausgaben + Gesamt + + 4,005.15 € + 4,005.15 € + 3,805.15 € + 200€ +
+} diff --git a/internal/template/dashboard/default.go b/internal/template/dashboard/default.go new file mode 100644 index 0000000..7d2270a --- /dev/null +++ b/internal/template/dashboard/default.go @@ -0,0 +1,2 @@ +package dashboard + diff --git a/internal/types/dashboard.go b/internal/types/dashboard.go new file mode 100644 index 0000000..abe440e --- /dev/null +++ b/internal/types/dashboard.go @@ -0,0 +1,8 @@ +package types + +type DashboardSummary struct { + Income int64 + Savings int64 + Expenses int64 + Total int64 +}