Add in context to dashboard for user to topbar loads all items & refactor for clarity of variable extraction.

This commit is contained in:
2025-04-23 15:29:34 +01:00
parent b5f1b17684
commit 0634876b75
8 changed files with 47 additions and 30 deletions

View File

@@ -6,26 +6,37 @@ import (
"net/http"
httpHelpers "synlotto-website/helpers/http"
securityHelpers "synlotto-website/helpers/security"
templateHelpers "synlotto-website/helpers/template"
"synlotto-website/models"
"synlotto-website/storage"
)
var (
total, winners int
prizeSum float64
)
func AdminDashboardHandler(db *sql.DB) http.HandlerFunc {
return httpHelpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
// userID, ok := securityHelpers.GetCurrentUserID(r)
// if !ok {
// http.Redirect(w, r, "/account/login", http.StatusSeeOther)
// return
// }
userID, ok := securityHelpers.GetCurrentUserID(r)
if !ok {
http.Redirect(w, r, "/account/login", http.StatusSeeOther)
return
}
// TODO: check is_admin from users table here
user := storage.GetUserByID(db, userID)
if user == nil {
http.Error(w, "User not found", http.StatusUnauthorized)
return
}
context := templateHelpers.TemplateContext(w, r, models.TemplateData{})
// Total ticket stats
var total, winners int
var prizeSum float64
data := models.TemplateData{}
context := templateHelpers.TemplateContext(w, r, data)
context["User"] = user
context["IsAdmin"] = user.IsAdmin
// Missing messages, notifications, potentially syndicate notifictions if that becomes a new top bar icon.
db.QueryRow(`SELECT COUNT(*), SUM(CASE WHEN is_winner THEN 1 ELSE 0 END), SUM(prize_amount) FROM my_tickets`).Scan(&total, &winners, &prizeSum)
context["Stats"] = map[string]interface{}{
"TotalTickets": total,
@@ -33,7 +44,6 @@ func AdminDashboardHandler(db *sql.DB) http.HandlerFunc {
"TotalPrizeAmount": prizeSum,
}
// Match run log
rows, err := db.Query(`
SELECT run_at, triggered_by, tickets_matched, winners_found, COALESCE(notes, '')
FROM log_ticket_matching