wip
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 8m31s

This commit is contained in:
2025-06-15 14:24:18 +02:00
parent d5d8a84a9a
commit 487ea82c34
7 changed files with 195 additions and 60 deletions

View File

@@ -1,10 +1,15 @@
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"
"time"
"github.com/a-h/templ"
)
@@ -14,12 +19,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,
}
}
@@ -42,17 +49,44 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
status = http.StatusNotFound
} else {
if user != nil {
comp = dashboard.Dashboard()
var err error
comp, err = handler.dashboard(user, 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)
handler.r.RenderLayoutWithStatus(r, w, comp, user, status)
}
}
func (handler IndexImpl) dashboard(user *types.User, 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", r.FormValue("timestamp"))
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
}
comp := dashboard.Dashboard(summary)
return comp, nil
}
func (handler IndexImpl) handleEmpty() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
updateSpan(r)