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
@@ -195,3 +195,37 @@ func TestCreateHabitHandler_MonthlyWithSpecificDates(t *testing.T) {
t.Fatalf("Expected no error, got %v", err)
}
}
func TestCreateHabitHandler_NegativeHabit(t *testing.T) {
mock := &mockHabitRepo{
createFunc: func(ctx context.Context, habit *entities.Habit) error {
habit.ID = "habit-123"
if !habit.IsNegative {
t.Error("Expected IsNegative to be true")
}
return nil
},
}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Eat Candy",
Description: "Track bad habit",
Type: "COUNTER",
Frequency: "DAILY",
CarryOver: false,
IsNegative: true,
}
habitID, err := handler.Handle(context.Background(), cmd)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if habitID == "" {
t.Error("Expected habit ID to be returned")
}
}