fix: store entries for the day I am currently seeing
Some checks are pending
Build and Push / build (push) Waiting to run

This commit is contained in:
Bastian Gruber 2026-02-24 12:19:54 +00:00
parent 8bc15951e4
commit 6b6778508f
Signed by: gruberb
GPG key ID: 426AF1CBA0530691
5 changed files with 35 additions and 4 deletions

View file

@ -10,7 +10,6 @@ import (
"log"
"net/http"
"strconv"
"time"
)
type Handler struct {
@ -54,7 +53,7 @@ func writeError(w http.ResponseWriter, status int, msg string) {
}
func (h *Handler) handleGetToday(w http.ResponseWriter, r *http.Request) {
h.handleGetDayData(w, time.Now().Format("2006-01-02"))
h.handleGetDayData(w, h.cfg.Now().Format("2006-01-02"))
}
func (h *Handler) handleGetDay(w http.ResponseWriter, r *http.Request) {
@ -124,6 +123,16 @@ func (h *Handler) handleAddEntry(w http.ResponseWriter, r *http.Request) {
return
}
now := h.cfg.Now()
entryDate := now.Format("2006-01-02")
entryTime := now.Format("15:04")
if req.Date != "" {
entryDate = req.Date
if req.Date != now.Format("2006-01-02") {
entryTime = "12:00"
}
}
var entries []types.Entry
for _, item := range estimation.Items {
desc := item.Name
@ -132,6 +141,8 @@ func (h *Handler) handleAddEntry(w http.ResponseWriter, r *http.Request) {
}
entry := types.Entry{
Date: entryDate,
Time: entryTime,
Type: req.Type,
Description: desc,
Kcal: item.Kcal,
@ -193,7 +204,7 @@ func (h *Handler) handleWeighIn(w http.ResponseWriter, r *http.Request) {
return
}
weighIn, err := h.db.AddWeighIn("", req.WeightKg)
weighIn, err := h.db.AddWeighIn(h.cfg.Now().Format("2006-01-02"), req.WeightKg)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return

View file

@ -18,6 +18,7 @@ func DefaultConfig() types.Config {
ActivityLevel: "moderate",
WeeklyLossKg: 0.5,
ListenAddr: ":8080",
Timezone: "America/Halifax",
}
}

View file

@ -56,9 +56,20 @@ type Config struct {
ActivityLevel string `toml:"activity_level" json:"activity_level"`
WeeklyLossKg float64 `toml:"weekly_loss_kg" json:"weekly_loss_kg"`
ListenAddr string `toml:"listen_addr" json:"listen_addr"`
Timezone string `toml:"timezone" json:"timezone"`
HasAPIKey bool `toml:"-" json:"has_api_key"`
}
// Now returns the current time in the configured timezone.
func (c *Config) Now() time.Time {
if c.Timezone != "" {
if loc, err := time.LoadLocation(c.Timezone); err == nil {
return time.Now().In(loc)
}
}
return time.Now()
}
type TodayResponse struct {
Date string `json:"date"`
Entries []Entry `json:"entries"`
@ -72,6 +83,7 @@ type TodayResponse struct {
type AddEntryRequest struct {
Type EntryType `json:"type"`
Description string `json:"description"`
Date string `json:"date,omitempty"`
}
type AddEntryResponse struct {

5
web/favicon.svg Normal file
View file

@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<rect width="32" height="32" rx="6" fill="#0d1117"/>
<rect x="1" y="1" width="30" height="30" rx="5" fill="none" stroke="#30363d" stroke-width="1"/>
<text x="16" y="22" text-anchor="middle" font-family="monospace" font-weight="bold" font-size="16" fill="#3fb950">CT</text>
</svg>

After

Width:  |  Height:  |  Size: 347 B

View file

@ -4,6 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CalTrack</title>
<link rel="icon" type="image/svg+xml" href="favicon.svg">
<style>
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;700&display=swap');
@ -679,7 +680,8 @@ async function addEntry(desc) {
try {
const data = await api('POST', '/api/entry', {
type: currentMode,
description: desc.trim()
description: desc.trim(),
date: currentDate
});
if (data.error) {