Add COUNTER type with auto-increment and IsNegative field

COUNTER type:
- Only accepts integer values (rejects decimals)
- Auto-increment behavior: each mark adds to existing value
- Supports decrement via negative values (e.g., -2)
- Enforces minimum value of 0 (never negative)
- Default increment is 1 if no value provided

VALUE type:
- Accepts decimal values
- Replaces value (no auto-increment)

IsNegative field:
- New boolean field on Habit entity
- Persisted in database (is_negative column)
- Available in all DTOs and HTTP responses
- Marks habits as "negative" (e.g., candy consumption)

Examples:
- COUNTER: 5 + (-2) = 3, 2 + (-3) = 0, 0 + (-1) = 0
- VALUE: 5000 → 12000 = 12000 (replacement)
This commit is contained in:
2025-11-27 08:58:39 +01:00
parent 358f844073
commit 10d45fc34e
23 changed files with 593 additions and 55 deletions
+4
View File
@@ -14,6 +14,7 @@ type CreateHabitRequest struct {
SpecificDays []int `json:"specific_days,omitempty"`
SpecificDates []int `json:"specific_dates,omitempty"`
CarryOver bool `json:"carry_over"`
IsNegative bool `json:"is_negative"`
TargetValue *float64 `json:"target_value,omitempty"`
}
@@ -36,6 +37,7 @@ type HabitResponse struct {
SpecificDays []int `json:"specific_days,omitempty"`
SpecificDates []int `json:"specific_dates,omitempty"`
CarryOver bool `json:"carry_over"`
IsNegative bool `json:"is_negative"`
TargetValue *float64 `json:"target_value,omitempty"`
CreatedAt time.Time `json:"created_at"`
ArchivedAt *time.Time `json:"archived_at,omitempty"`
@@ -51,6 +53,7 @@ type TodaysHabitResponse struct {
Name string `json:"name"`
Type value_objects.HabitType `json:"type"`
TargetValue *float64 `json:"target_value,omitempty"`
IsNegative bool `json:"is_negative"`
ScheduledDate time.Time `json:"scheduled_date"`
IsCarriedOver bool `json:"is_carried_over"`
}
@@ -63,6 +66,7 @@ type UserHabitResponse struct {
SpecificDays []int `json:"specific_days,omitempty"`
TargetValue *float64 `json:"target_value,omitempty"`
CarryOver bool `json:"carry_over"`
IsNegative bool `json:"is_negative"`
}
type HabitEntryResponse struct {
@@ -84,6 +84,7 @@ func (h *HabitHandlers) CreateHabit(w http.ResponseWriter, r *http.Request) {
SpecificDays: req.SpecificDays,
SpecificDates: req.SpecificDates,
CarryOver: req.CarryOver,
IsNegative: req.IsNegative,
TargetValue: req.TargetValue,
}
@@ -137,6 +138,7 @@ func (h *HabitHandlers) GetUserHabits(w http.ResponseWriter, r *http.Request) {
SpecificDays: habit.SpecificDays,
TargetValue: habit.TargetValue,
CarryOver: habit.CarryOver,
IsNegative: habit.IsNegative,
}
}
@@ -192,6 +194,7 @@ func (h *HabitHandlers) GetHabitByID(w http.ResponseWriter, r *http.Request) {
SpecificDays: habit.SpecificDays,
TargetValue: habit.TargetValue,
CarryOver: habit.CarryOver,
IsNegative: habit.IsNegative,
}
respondJSON(w, http.StatusOK, response)
@@ -464,6 +467,7 @@ func (h *HabitHandlers) GetTodaysHabits(w http.ResponseWriter, r *http.Request)
Name: habit.Name,
Type: habit.Type,
TargetValue: habit.TargetValue,
IsNegative: habit.IsNegative,
ScheduledDate: habit.ScheduledDate,
IsCarriedOver: habit.IsCarriedOver,
}
@@ -29,8 +29,8 @@ func (r *HabitRepository) Create(ctx context.Context, habit *entities.Habit) err
query := `
INSERT INTO habits (
id, user_id, name, description, type, frequency,
specific_days, specific_dates, carry_over, target_value, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
specific_days, specific_dates, carry_over, is_negative, target_value, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
_, err := r.db.ExecContext(ctx, query,
@@ -43,6 +43,7 @@ func (r *HabitRepository) Create(ctx context.Context, habit *entities.Habit) err
specificDays,
specificDates,
habit.CarryOver,
habit.IsNegative,
habit.TargetValue,
habit.CreatedAt,
)
@@ -57,7 +58,7 @@ func (r *HabitRepository) Create(ctx context.Context, habit *entities.Habit) err
func (r *HabitRepository) FindByID(ctx context.Context, id string) (*entities.Habit, error) {
query := `
SELECT id, user_id, name, description, type, frequency,
specific_days, specific_dates, carry_over, target_value,
specific_days, specific_dates, carry_over, is_negative, target_value,
created_at, archived_at
FROM habits
WHERE id = ?
@@ -80,6 +81,7 @@ func (r *HabitRepository) FindByID(ctx context.Context, id string) (*entities.Ha
&specificDays,
&specificDates,
&habit.CarryOver,
&habit.IsNegative,
&habit.TargetValue,
&habit.CreatedAt,
&archivedAt,
@@ -108,7 +110,7 @@ func (r *HabitRepository) FindByID(ctx context.Context, id string) (*entities.Ha
func (r *HabitRepository) FindActiveByUserID(ctx context.Context, userID string) ([]*entities.Habit, error) {
query := `
SELECT id, user_id, name, description, type, frequency,
specific_days, specific_dates, carry_over, target_value,
specific_days, specific_dates, carry_over, is_negative, target_value,
created_at, archived_at
FROM habits
WHERE user_id = ? AND archived_at IS NULL
@@ -131,7 +133,7 @@ func (r *HabitRepository) Update(ctx context.Context, habit *entities.Habit) err
query := `
UPDATE habits
SET name = ?, description = ?, type = ?, frequency = ?,
specific_days = ?, specific_dates = ?, carry_over = ?,
specific_days = ?, specific_dates = ?, carry_over = ?, is_negative = ?,
target_value = ?, archived_at = ?
WHERE id = ?
`
@@ -144,6 +146,7 @@ func (r *HabitRepository) Update(ctx context.Context, habit *entities.Habit) err
specificDays,
specificDates,
habit.CarryOver,
habit.IsNegative,
habit.TargetValue,
habit.ArchivedAt,
habit.ID,
@@ -182,6 +185,7 @@ func (r *HabitRepository) scanHabits(rows *sql.Rows) ([]*entities.Habit, error)
&specificDays,
&specificDates,
&habit.CarryOver,
&habit.IsNegative,
&habit.TargetValue,
&habit.CreatedAt,
&archivedAt,
@@ -210,7 +214,7 @@ func (r *HabitRepository) scanHabits(rows *sql.Rows) ([]*entities.Habit, error)
func (r *HabitRepository) FindByUserID(ctx context.Context, userID string) ([]*entities.Habit, error) {
query := `
SELECT id, user_id, name, description, type, frequency,
specific_days, specific_dates, carry_over, target_value,
specific_days, specific_dates, carry_over, is_negative, target_value,
created_at, archived_at
FROM habits
WHERE user_id = ?
@@ -130,9 +130,9 @@ func TestHabitRepositoryFindActiveByUserID(t *testing.T) {
userID := "user-789"
habit1 := entities.NewHabit(userID, "Habit 1", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit2 := entities.NewHabit(userID, "Habit 2", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit3 := entities.NewHabit(userID, "Habit 3", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit1 := entities.NewHabit(userID, "Habit 1", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit2 := entities.NewHabit(userID, "Habit 2", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit3 := entities.NewHabit(userID, "Habit 3", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
repo.Create(ctx, habit1)
repo.Create(ctx, habit2)
@@ -42,6 +42,7 @@ CREATE TABLE IF NOT EXISTS habits (
specific_days TEXT,
specific_dates TEXT,
carry_over BOOLEAN DEFAULT 0,
is_negative BOOLEAN DEFAULT 0,
target_value REAL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
archived_at DATETIME,