This commit is contained in:
65
internal/handler/dashboard.go
Normal file
65
internal/handler/dashboard.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"spend-sparrow/internal/handler/middleware"
|
||||
"spend-sparrow/internal/service"
|
||||
t "spend-sparrow/internal/template/dashboard"
|
||||
"spend-sparrow/internal/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Dashboard interface {
|
||||
Handle(router *http.ServeMux)
|
||||
}
|
||||
|
||||
type DashboardImpl struct {
|
||||
s service.Dashboard
|
||||
r *Render
|
||||
}
|
||||
|
||||
func NewDashboard(s service.Dashboard, r *Render) Dashboard {
|
||||
return DashboardImpl{
|
||||
s: s,
|
||||
r: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (h DashboardImpl) Handle(r *http.ServeMux) {
|
||||
r.Handle("GET /transaction", h.handleDashboardPage())
|
||||
}
|
||||
|
||||
func (h DashboardImpl) handleDashboardPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
updateSpan(r)
|
||||
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
handleError(w, r, fmt.Errorf("could not parse timestamp: %w", service.ErrBadRequest))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
month = time.Now()
|
||||
}
|
||||
|
||||
summary, err := h.s.Summary(r.Context(), user, month)
|
||||
|
||||
comp := t.Dashboard(summary)
|
||||
if utils.IsHtmx(r) {
|
||||
h.r.Render(r, w, comp)
|
||||
} else {
|
||||
h.r.RenderLayout(r, w, comp, user)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user