feat(dashboard): #163 first summary
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 5m11s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m6s

This commit was merged in pull request #181.
This commit is contained in:
2025-06-08 15:35:21 +02:00
parent 935019c1c4
commit 6b8059889d
15 changed files with 280 additions and 67 deletions

View File

@@ -1,9 +1,16 @@
package handler
import (
"fmt"
"log/slog"
"net/http"
"spend-sparrow/internal/handler/middleware"
"spend-sparrow/internal/service"
"spend-sparrow/internal/template"
"spend-sparrow/internal/template/dashboard"
"spend-sparrow/internal/types"
"spend-sparrow/internal/utils"
"time"
"github.com/a-h/templ"
)
@@ -13,12 +20,14 @@ type Index interface {
}
type IndexImpl struct {
render *Render
r *Render
d *service.Dashboard
}
func NewIndex(render *Render) Index {
func NewIndex(r *Render, d *service.Dashboard) Index {
return IndexImpl{
render: render,
r: r,
d: d,
}
}
@@ -33,6 +42,8 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
user := middleware.GetUser(r)
htmx := utils.IsHtmx(r)
var comp templ.Component
var status int
@@ -41,14 +52,47 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
status = http.StatusNotFound
} else {
if user != nil {
comp = template.Dashboard()
var err error
comp, err = handler.dashboard(user, htmx, r)
if err != nil {
slog.Error("Failed to get dashboard summary", "err", err)
}
} else {
comp = template.Index()
}
status = http.StatusOK
}
handler.render.RenderLayoutWithStatus(r, w, comp, user, status)
if htmx {
handler.r.RenderWithStatus(r, w, comp, status)
} else {
handler.r.RenderLayoutWithStatus(r, w, comp, user, status)
}
}
}
func (handler IndexImpl) dashboard(user *types.User, htmx bool, r *http.Request) (templ.Component, error) {
var month time.Time
var err error
monthStr := r.URL.Query().Get("month")
if monthStr != "" {
month, err = time.Parse("2006-01-02", monthStr)
if err != nil {
return nil, fmt.Errorf("could not parse timestamp: %w", service.ErrBadRequest)
}
} else {
month = time.Now()
}
summary, err := handler.d.Summary(r.Context(), user, month)
if err != nil {
return nil, err
}
if htmx {
return dashboard.DashboardData(summary), nil
} else {
return dashboard.Dashboard(summary), nil
}
}