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
+9 -6
View File
@@ -16,6 +16,7 @@ type Habit struct {
SpecificDays []int
SpecificDates []int
CarryOver bool
IsNegative bool
TargetValue *float64
CreatedAt time.Time
ArchivedAt *time.Time
@@ -27,14 +28,16 @@ func NewHabit(
habitType value_objects.HabitType,
frequency value_objects.Frequency,
carryOver bool,
isNegative bool,
) *Habit {
return &Habit{
UserID: userID,
Name: name,
Type: habitType,
Frequency: frequency,
CarryOver: carryOver,
CreatedAt: time.Now(),
UserID: userID,
Name: name,
Type: habitType,
Frequency: frequency,
CarryOver: carryOver,
IsNegative: isNegative,
CreatedAt: time.Now(),
}
}
+6 -5
View File
@@ -13,8 +13,9 @@ func TestNewHabit(t *testing.T) {
habitType := value_objects.HabitTypeBoolean
frequency := value_objects.FrequencyDaily
carryOver := false
isNegative := false
habit := NewHabit(userID, name, habitType, frequency, carryOver)
habit := NewHabit(userID, name, habitType, frequency, carryOver, isNegative)
if habit.UserID != userID {
t.Errorf("Expected UserID %s, got %s", userID, habit.UserID)
@@ -46,7 +47,7 @@ func TestNewHabit(t *testing.T) {
}
func TestHabit_Archive(t *testing.T) {
habit := NewHabit("user-123", "Test Habit", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit := NewHabit("user-123", "Test Habit", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
if habit.ArchivedAt != nil {
t.Error("New habit should not be archived")
@@ -64,7 +65,7 @@ func TestHabit_Archive(t *testing.T) {
}
func TestHabit_IsActive(t *testing.T) {
habit := NewHabit("user-123", "Test Habit", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit := NewHabit("user-123", "Test Habit", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
if !habit.IsActive() {
t.Error("New habit should be active")
@@ -78,7 +79,7 @@ func TestHabit_IsActive(t *testing.T) {
}
func TestHabit_WithSpecificDays(t *testing.T) {
habit := NewHabit("user-123", "Workout", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
habit := NewHabit("user-123", "Workout", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
habit.SpecificDays = []int{1, 3, 5} // Monday, Wednesday, Friday
if len(habit.SpecificDays) != 3 {
@@ -91,7 +92,7 @@ func TestHabit_WithSpecificDays(t *testing.T) {
}
func TestHabit_WithTargetValue(t *testing.T) {
habit := NewHabit("user-123", "Drink Water", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false)
habit := NewHabit("user-123", "Drink Water", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
targetValue := 8.0
habit.TargetValue = &targetValue