fix: reminder_time format for habits and tasks (HH:MM instead of timestamp)

This commit is contained in:
Cosmo
2026-02-06 14:25:52 +00:00
parent afeb3adddf
commit 9e90aa6d95
2 changed files with 41 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package model
import ( import (
"database/sql" "database/sql"
"strings"
"time" "time"
) )
@@ -12,8 +13,8 @@ type Habit struct {
Description string `db:"description" json:"description"` Description string `db:"description" json:"description"`
Color string `db:"color" json:"color"` Color string `db:"color" json:"color"`
Icon string `db:"icon" json:"icon"` Icon string `db:"icon" json:"icon"`
Frequency string `db:"frequency" json:"frequency"` // daily, weekly, custom Frequency string `db:"frequency" json:"frequency"`
TargetDays []int `db:"-" json:"target_days"` // 0=Sun, 1=Mon, etc. TargetDays []int `db:"-" json:"target_days"`
TargetCount int `db:"target_count" json:"target_count"` TargetCount int `db:"target_count" json:"target_count"`
ReminderTime sql.NullString `db:"reminder_time" json:"-"` ReminderTime sql.NullString `db:"reminder_time" json:"-"`
ReminderTimeStr *string `db:"-" json:"reminder_time"` ReminderTimeStr *string `db:"-" json:"reminder_time"`
@@ -23,8 +24,23 @@ type Habit struct {
} }
func (h *Habit) ProcessForJSON() { func (h *Habit) ProcessForJSON() {
if h.ReminderTime.Valid { if h.ReminderTime.Valid && h.ReminderTime.String != "" {
h.ReminderTimeStr = &h.ReminderTime.String timeStr := h.ReminderTime.String
// Handle formats like "0000-01-01T19:00:00Z" or "19:00:00"
if strings.Contains(timeStr, "T") {
// Parse as timestamp and extract time
t, err := time.Parse(time.RFC3339, timeStr)
if err == nil {
formatted := t.Format("15:04")
h.ReminderTimeStr = &formatted
return
}
}
// Handle "19:00:00" format
if len(timeStr) >= 5 {
formatted := timeStr[:5]
h.ReminderTimeStr = &formatted
}
} }
} }
@@ -62,7 +78,7 @@ type UpdateHabitRequest struct {
} }
type LogHabitRequest struct { type LogHabitRequest struct {
Date string `json:"date,omitempty"` // YYYY-MM-DD, defaults to today Date string `json:"date,omitempty"`
Count int `json:"count,omitempty"` Count int `json:"count,omitempty"`
Note string `json:"note,omitempty"` Note string `json:"note,omitempty"`
} }

View File

@@ -2,6 +2,7 @@ package model
import ( import (
"database/sql" "database/sql"
"strings"
"time" "time"
) )
@@ -14,7 +15,7 @@ type Task struct {
Color string `db:"color" json:"color"` Color string `db:"color" json:"color"`
DueDate sql.NullTime `db:"due_date" json:"-"` DueDate sql.NullTime `db:"due_date" json:"-"`
DueDateStr *string `db:"-" json:"due_date"` DueDateStr *string `db:"-" json:"due_date"`
Priority int `db:"priority" json:"priority"` // 0=none, 1=low, 2=medium, 3=high Priority int `db:"priority" json:"priority"`
ReminderTime sql.NullString `db:"reminder_time" json:"-"` ReminderTime sql.NullString `db:"reminder_time" json:"-"`
ReminderTimeStr *string `db:"-" json:"reminder_time"` ReminderTimeStr *string `db:"-" json:"reminder_time"`
CompletedAt sql.NullTime `db:"completed_at" json:"-"` CompletedAt sql.NullTime `db:"completed_at" json:"-"`
@@ -28,8 +29,22 @@ func (t *Task) ProcessForJSON() {
s := t.DueDate.Time.Format("2006-01-02") s := t.DueDate.Time.Format("2006-01-02")
t.DueDateStr = &s t.DueDateStr = &s
} }
if t.ReminderTime.Valid { if t.ReminderTime.Valid && t.ReminderTime.String != "" {
t.ReminderTimeStr = &t.ReminderTime.String timeStr := t.ReminderTime.String
// Handle formats like "0000-01-01T19:00:00Z" or "19:00:00"
if strings.Contains(timeStr, "T") {
parsed, err := time.Parse(time.RFC3339, timeStr)
if err == nil {
formatted := parsed.Format("15:04")
t.ReminderTimeStr = &formatted
return
}
}
// Handle "19:00:00" format
if len(timeStr) >= 5 {
formatted := timeStr[:5]
t.ReminderTimeStr = &formatted
}
} }
t.Completed = t.CompletedAt.Valid t.Completed = t.CompletedAt.Valid
} }
@@ -39,9 +54,9 @@ type CreateTaskRequest struct {
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"` Icon string `json:"icon,omitempty"`
Color string `json:"color,omitempty"` Color string `json:"color,omitempty"`
DueDate *string `json:"due_date,omitempty"` // YYYY-MM-DD DueDate *string `json:"due_date,omitempty"`
Priority int `json:"priority,omitempty"` Priority int `json:"priority,omitempty"`
ReminderTime *string `json:"reminder_time,omitempty"` // HH:MM ReminderTime *string `json:"reminder_time,omitempty"`
} }
type UpdateTaskRequest struct { type UpdateTaskRequest struct {