Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 3m40s
108 lines
2.2 KiB
Go
108 lines
2.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"spend-sparrow/internal/handler/middleware"
|
|
"spend-sparrow/internal/service"
|
|
"spend-sparrow/internal/template/dashboard"
|
|
"spend-sparrow/internal/utils"
|
|
"time"
|
|
)
|
|
|
|
type Dashboard interface {
|
|
Handle(router *http.ServeMux)
|
|
}
|
|
|
|
type DashboardImpl struct {
|
|
r *Render
|
|
d *service.Dashboard
|
|
}
|
|
|
|
func NewDashboard(r *Render, d *service.Dashboard) Dashboard {
|
|
return DashboardImpl{
|
|
r: r,
|
|
d: d,
|
|
}
|
|
}
|
|
|
|
func (handler DashboardImpl) Handle(router *http.ServeMux) {
|
|
router.Handle("GET /dashboard", handler.handleDashboard())
|
|
router.Handle("GET /dashboard/dataset", handler.handleDashboardDataset())
|
|
}
|
|
|
|
func (handler DashboardImpl) handleDashboard() 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
|
|
}
|
|
|
|
comp := dashboard.Dashboard()
|
|
handler.r.RenderLayoutWithStatus(r, w, comp, user, http.StatusOK)
|
|
}
|
|
}
|
|
|
|
func (handler DashboardImpl) handleDashboardDataset() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
updateSpan(r)
|
|
|
|
user := middleware.GetUser(r)
|
|
|
|
series, err := handler.d.MainChart(r.Context(), user)
|
|
if err != nil {
|
|
handleError(w, r, err)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
account := ""
|
|
savings := ""
|
|
|
|
for _, entry := range series {
|
|
account += fmt.Sprintf(`["%s",%.2f],`, entry.Day.Format(time.RFC3339), float64(entry.Value)/100)
|
|
savings += fmt.Sprintf(`["%s",%.2f],`, entry.Day.Format(time.RFC3339), float64(entry.Savings)/100)
|
|
}
|
|
account = account[:len(account)-1]
|
|
savings = savings[:len(savings)-1]
|
|
|
|
_, err = fmt.Fprintf(w, `
|
|
{
|
|
"tooltip": {
|
|
"trigger": "axis",
|
|
"formatter": "<updated by client>"
|
|
},
|
|
"xAxis": {
|
|
"type": "time"
|
|
},
|
|
"yAxis": {
|
|
"axisLabel": {
|
|
"formatter": "{value} €"
|
|
}
|
|
},
|
|
"series": [
|
|
{
|
|
"data": [%s],
|
|
"type": "line",
|
|
"name": "Account Value"
|
|
},
|
|
{
|
|
"data": [%s],
|
|
"type": "line",
|
|
"name": "Savings"
|
|
}
|
|
]
|
|
}
|
|
`, account, savings)
|
|
if err != nil {
|
|
slog.InfoContext(r.Context(), "could not write response", "err", err)
|
|
}
|
|
}
|
|
}
|