Initial commit
This commit is contained in:
commit
b6014c6ffc
47 files changed
+4329
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"vlog/internal/auth"
|
||||
"vlog/internal/models"
|
||||
)
|
||||
|
||||
// Dashboard computes the "what's coming up" view for the current user:
|
||||
// open reminders due soon/overdue (real + synthesized rego/WoF/service-due),
|
||||
// across every car. It's computed at request time — no scheduler, no
|
||||
// email, matching the in-app-only reminder decision. Shares its synthesis
|
||||
// logic with the car-scoped ListReminders (see reminder_view.go) so the two
|
||||
// views never disagree about what's due.
|
||||
func (h *Handlers) Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := auth.UserIDFromContext(r.Context())
|
||||
todayDate := today()
|
||||
|
||||
var cars []models.Car
|
||||
if err := h.dbSelect(r.Context(), &cars, `SELECT * FROM cars WHERE user_id = ?`, userID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to load dashboard")
|
||||
return
|
||||
}
|
||||
|
||||
carByID := make(map[string]models.Car, len(cars))
|
||||
carIDs := make([]string, len(cars))
|
||||
for i, c := range cars {
|
||||
carByID[c.ID] = c
|
||||
carIDs[i] = c.ID
|
||||
}
|
||||
|
||||
items := []models.ReminderView{} // not "var" — an empty result must encode as [], not null
|
||||
|
||||
if len(carIDs) > 0 {
|
||||
query, args, err := sqlx.In(`SELECT * FROM reminders WHERE car_id IN (?) AND completed_at IS NULL`, carIDs)
|
||||
if err == nil {
|
||||
var reminders []models.Reminder
|
||||
if err := h.dbSelect(r.Context(), &reminders, query, args...); err == nil {
|
||||
for _, rem := range reminders {
|
||||
car := carByID[rem.CarID]
|
||||
view := reminderToView(rem, car.Rego, car.Odometer, todayDate)
|
||||
if view.Status == "overdue" || view.Status == "upcoming" {
|
||||
items = append(items, view)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, c := range cars {
|
||||
car := c // local copy — synthesizedReminders takes a pointer
|
||||
for _, view := range synthesizedReminders(r.Context(), h, &car, todayDate) {
|
||||
if view.Status == "overdue" || view.Status == "upcoming" {
|
||||
items = append(items, view)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sortReminderViews(items)
|
||||
writeJSON(w, http.StatusOK, items)
|
||||
}
|
||||
Reference in new issue
Block a user