summaryrefslogtreecommitdiff
path: root/internal/handlers/timeline.go
blob: b923d3e7510b21d5c32d4e7014ee48290ef0721a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package handlers

import (
	"net/http"
	"strconv"
	"time"

	"task-dashboard/internal/models"
)

// HandleTimeline renders the timeline view
func (h *Handler) HandleTimeline(w http.ResponseWriter, r *http.Request) {
	// Parse query params
	startStr := r.URL.Query().Get("start")
	daysStr := r.URL.Query().Get("days")

	var start time.Time
	if startStr != "" {
		parsed, err := time.ParseInLocation("2006-01-02", startStr, time.Local)
		if err == nil {
			start = parsed
		} else {
			start = time.Now()
		}
	} else {
		start = time.Now()
	}

	// Normalize start to beginning of day
	start = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())

	days := 3 // Default
	if daysStr != "" {
		if d, err := strconv.Atoi(daysStr); err == nil && d > 0 {
			days = d
		}
	}

	end := start.AddDate(0, 0, days)

	// Call BuildTimeline
	items, err := BuildTimeline(r.Context(), h.store, h.googleCalendarClient, start, end)
	if err != nil {
		JSONError(w, http.StatusInternalServerError, "Failed to build timeline", err)
		return
	}

	data := struct {
		Items []models.TimelineItem
		Start time.Time
		Days  int
	}{
		Items: items,
		Start: start,
		Days:  days,
	}

	HTMLResponse(w, h.templates, "timeline-tab", data)
}