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
@@ -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,