This commit is contained in:
2025-06-08 15:35:21 +02:00
parent 935019c1c4
commit 2a6f96d787
6 changed files with 94 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"spend-sparrow/internal/handler/middleware" "spend-sparrow/internal/handler/middleware"
"spend-sparrow/internal/template" "spend-sparrow/internal/template"
"spend-sparrow/internal/template/dashboard"
"github.com/a-h/templ" "github.com/a-h/templ"
) )
@@ -41,7 +42,7 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
status = http.StatusNotFound status = http.StatusNotFound
} else { } else {
if user != nil { if user != nil {
comp = template.Dashboard() comp = dashboard.Dashboard()
} else { } else {
comp = template.Index() comp = template.Index()
} }

View File

@@ -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
}

View File

@@ -1,9 +0,0 @@
package template
templ Dashboard() {
<div>
<h1 class="text-8xl">
Dashboard
</h1>
</div>
}

View File

@@ -0,0 +1,23 @@
package dashboard
templ Dashboard() {
<div class="mt-10">
<label for="month">Select Month:</label>
<input name="month" type="date" class="input"/>
@summaryCard()
</div>
}
templ summaryCard() {
<section class="grid grid-cols-[auto_auto_auto_auto_1fr] gap-4">
<span>Einnahmen</span>
<span>Gespart</span>
<span>Ausgaben</span>
<span>Gesamt</span>
<span></span>
<span>4,005.15 €</span>
<span>4,005.15 €</span>
<span>3,805.15 €</span>
<span>200€</span>
</section>
}

View File

@@ -0,0 +1,2 @@
package dashboard

View File

@@ -0,0 +1,8 @@
package types
type DashboardSummary struct {
Income int64
Savings int64
Expenses int64
Total int64
}