| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "embed" |
| 6 | "html/template" |
| 7 | "log/slog" |
| 8 | |
| 9 | "github.com/stevedylandev/andromeda/pkg/auth" |
| 10 | ) |
| 11 | |
| 12 | //go:embed templates/*.html static/* |
| 13 | var appFS embed.FS |
| 14 | |
| 15 | type App struct { |
| 16 | DB *sql.DB |
| 17 | Log *slog.Logger |
| 18 | Templates *template.Template |
| 19 | Sessions *auth.Store |
| 20 | AdminPassword string |
| 21 | APIKey string |
| 22 | CookieSecure bool |
| 23 | BaseURL string |
| 24 | } |
| 25 | |
| 26 | // valueTypes is the fixed set of value types a habit may declare. Kept in sync |
| 27 | // with the CHECK constraint in habbitsSchema and the normalizeValue switch. |
| 28 | var valueTypes = []string{"int", "float", "bool", "string"} |
| 29 | |
| 30 | // habitRow is the view model for a habit in list/detail templates. |
| 31 | type habitRow struct { |
| 32 | ShortID string |
| 33 | Name string |
| 34 | ValueType string |
| 35 | Unit string |
| 36 | Description string |
| 37 | RecordCount int |
| 38 | } |
| 39 | |
| 40 | // recordRow is the view model for a record. Date is the grouping key |
| 41 | // (YYYY-MM-DD); TimeDisplay is the time-of-day shown within a day group; |
| 42 | // RecordedAtInput is the datetime-local value for edit forms. |
| 43 | type recordRow struct { |
| 44 | ShortID string |
| 45 | HabitShortID string |
| 46 | HabitName string |
| 47 | ValueType string |
| 48 | Value string |
| 49 | Unit string |
| 50 | Date string |
| 51 | TimeDisplay string |
| 52 | RecordedAtInput string |
| 53 | } |
| 54 | |
| 55 | // habitDay groups records by day. Used for both the dashboard (all habits, |
| 56 | // records interleaved chronologically) and the habit detail page. |
| 57 | type habitDay struct { |
| 58 | Date string |
| 59 | Records []recordRow |
| 60 | } |
| 61 | |
| 62 | type loginPageData struct { |
| 63 | Error string |
| 64 | } |
| 65 | |
| 66 | type dashboardData struct { |
| 67 | Success string |
| 68 | Error string |
| 69 | Habits []habitRow |
| 70 | Days []habitDay |
| 71 | } |
| 72 | |
| 73 | type newHabitPageData struct { |
| 74 | Error string |
| 75 | ValueTypes []string |
| 76 | } |
| 77 | |
| 78 | type settingsPageData struct { |
| 79 | Success string |
| 80 | Error string |
| 81 | ValueTypes []string |
| 82 | Habits []habitRow |
| 83 | } |
| 84 | |
| 85 | type habitPageData struct { |
| 86 | Success string |
| 87 | Error string |
| 88 | Habit habitRow |
| 89 | Days []habitDay |
| 90 | } |