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:
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestArchiveHabitHandler_ArchivesSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -59,7 +59,7 @@ func TestArchiveHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestArchiveHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -81,7 +81,7 @@ func TestArchiveHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestArchiveHabitHandler_CanArchiveAlreadyArchivedHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.Archive()
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ type CreateHabitCommand struct {
|
||||
SpecificDays []int
|
||||
SpecificDates []int
|
||||
CarryOver bool
|
||||
IsNegative bool
|
||||
TargetValue *float64
|
||||
}
|
||||
|
||||
@@ -46,7 +47,7 @@ func (h *CreateHabitHandler) Handle(ctx context.Context, cmd CreateHabitCommand)
|
||||
return "", errors.ErrInvalidInput
|
||||
}
|
||||
|
||||
habit := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver)
|
||||
habit := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver, cmd.IsNegative)
|
||||
habit.Description = cmd.Description
|
||||
habit.SpecificDays = cmd.SpecificDays
|
||||
habit.SpecificDates = cmd.SpecificDates
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,13 @@ package commands
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"apocapoc-api/internal/domain/entities"
|
||||
"apocapoc-api/internal/domain/repositories"
|
||||
"apocapoc-api/internal/domain/value_objects"
|
||||
"apocapoc-api/internal/shared/errors"
|
||||
)
|
||||
|
||||
type MarkHabitCommand struct {
|
||||
@@ -44,7 +47,61 @@ func (h *MarkHabitHandler) Handle(ctx context.Context, cmd MarkHabitCommand) err
|
||||
return fmt.Errorf("habit is archived")
|
||||
}
|
||||
|
||||
entry := entities.NewHabitEntry(cmd.HabitID, cmd.ScheduledDate, cmd.Value)
|
||||
if habit.Type == value_objects.HabitTypeCounter && cmd.Value != nil {
|
||||
if *cmd.Value != math.Floor(*cmd.Value) {
|
||||
return errors.ErrInvalidInput
|
||||
}
|
||||
}
|
||||
|
||||
var finalValue *float64
|
||||
|
||||
if habit.Type == value_objects.HabitTypeCounter {
|
||||
startOfDay := time.Date(cmd.ScheduledDate.Year(), cmd.ScheduledDate.Month(), cmd.ScheduledDate.Day(), 0, 0, 0, 0, cmd.ScheduledDate.Location())
|
||||
endOfDay := startOfDay.Add(24 * time.Hour).Add(-time.Nanosecond)
|
||||
|
||||
existingEntries, _ := h.entryRepo.FindByHabitIDAndDateRange(ctx, cmd.HabitID, startOfDay, endOfDay)
|
||||
|
||||
if len(existingEntries) > 0 {
|
||||
existingEntry := existingEntries[0]
|
||||
|
||||
var increment float64 = 1.0
|
||||
if cmd.Value != nil {
|
||||
increment = *cmd.Value
|
||||
}
|
||||
|
||||
var newValue float64
|
||||
if existingEntry.Value != nil {
|
||||
newValue = *existingEntry.Value + increment
|
||||
} else {
|
||||
newValue = increment
|
||||
}
|
||||
|
||||
if newValue < 0 {
|
||||
newValue = 0
|
||||
}
|
||||
|
||||
finalValue = &newValue
|
||||
existingEntry.Value = finalValue
|
||||
existingEntry.CompletedAt = time.Now()
|
||||
|
||||
return h.entryRepo.Update(ctx, existingEntry)
|
||||
} else {
|
||||
if cmd.Value != nil {
|
||||
value := *cmd.Value
|
||||
if value < 0 {
|
||||
value = 0
|
||||
}
|
||||
finalValue = &value
|
||||
} else {
|
||||
defaultValue := 1.0
|
||||
finalValue = &defaultValue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
finalValue = cmd.Value
|
||||
}
|
||||
|
||||
entry := entities.NewHabitEntry(cmd.HabitID, cmd.ScheduledDate, finalValue)
|
||||
|
||||
return h.entryRepo.Create(ctx, entry)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ import (
|
||||
)
|
||||
|
||||
type mockEntryRepo struct {
|
||||
createFunc func(ctx context.Context, entry *entities.HabitEntry) error
|
||||
createFunc func(ctx context.Context, entry *entities.HabitEntry) error
|
||||
findByDateRangeFunc func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error)
|
||||
updateFunc func(ctx context.Context, entry *entities.HabitEntry) error
|
||||
}
|
||||
|
||||
func (m *mockEntryRepo) Create(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
@@ -30,6 +32,9 @@ func (m *mockEntryRepo) FindByHabitID(ctx context.Context, habitID string) ([]*e
|
||||
}
|
||||
|
||||
func (m *mockEntryRepo) FindByHabitIDAndDateRange(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
if m.findByDateRangeFunc != nil {
|
||||
return m.findByDateRangeFunc(ctx, habitID, from, to)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -38,6 +43,9 @@ func (m *mockEntryRepo) FindPendingByHabitID(ctx context.Context, habitID string
|
||||
}
|
||||
|
||||
func (m *mockEntryRepo) Update(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if m.updateFunc != nil {
|
||||
return m.updateFunc(ctx, entry)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -74,7 +82,7 @@ func (m *mockHabitRepoForMark) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_Success(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
@@ -122,7 +130,7 @@ func TestMarkHabitHandler_HabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_ArchivedHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.Archive()
|
||||
|
||||
@@ -144,7 +152,7 @@ func TestMarkHabitHandler_ArchivedHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_WithValue(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Steps", value_objects.HabitTypeValue, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Steps", value_objects.HabitTypeValue, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
@@ -179,7 +187,7 @@ func TestMarkHabitHandler_WithValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_DuplicateEntry(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
@@ -202,3 +210,332 @@ func TestMarkHabitHandler_DuplicateEntry(t *testing.T) {
|
||||
t.Errorf("Expected ErrAlreadyExists, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterOnlyAcceptsIntegers(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decimalValue := 2.5
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decimalValue,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != errors.ErrInvalidInput {
|
||||
t.Errorf("Expected ErrInvalidInput for decimal value on COUNTER, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAcceptsIntegers(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{
|
||||
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 3.0 {
|
||||
t.Errorf("Expected value 3.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
intValue := 3.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &intValue,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAutoIncrementFirstMark(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{}, nil
|
||||
},
|
||||
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 1.0 {
|
||||
t.Errorf("Expected default value 1.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: nil,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAutoIncrementSubsequentMarks(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 3.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 4.0 {
|
||||
t.Errorf("Expected value 4.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: nil,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAutoIncrementWithCustomValue(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 3.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 5.0 {
|
||||
t.Errorf("Expected value 5.0 (3+2), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
increment := 2.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &increment,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterCanDecrement(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Cigarettes", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 5.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 3.0 {
|
||||
t.Errorf("Expected value 3.0 (5-2), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decrement := -2.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decrement,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterMinimumZero(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 2.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 0.0 {
|
||||
t.Errorf("Expected value 0.0 (2-3 clamped to 0), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decrement := -3.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decrement,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterStaysAtZero(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 0.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 0.0 {
|
||||
t.Errorf("Expected value to stay at 0.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decrement := -1.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decrement,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterFirstMarkWithNegative(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{}, nil
|
||||
},
|
||||
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 0.0 {
|
||||
t.Errorf("Expected value 0.0 (negative clamped), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
negativeValue := -5.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &negativeValue,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ func (m *mockEntryRepoForUnmark) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func TestUnmarkHabitHandler_UnmarksSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
scheduledDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
|
||||
@@ -87,7 +87,7 @@ func TestUnmarkHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnmarkHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -112,7 +112,7 @@ func TestUnmarkHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnmarkHabitHandler_ReturnsErrorWhenEntryNotFound(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
|
||||
@@ -33,7 +33,7 @@ func (m *mockHabitRepoForUpdate) Update(ctx context.Context, habit *entities.Hab
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_UpdatesSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -101,7 +101,7 @@ func TestUpdateHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -124,7 +124,7 @@ func TestUpdateHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_CannotUpdateArchivedHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.Archive()
|
||||
|
||||
@@ -148,7 +148,7 @@ func TestUpdateHabitHandler_CannotUpdateArchivedHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_ValidatesInput(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
|
||||
@@ -39,6 +39,7 @@ func (h *GetHabitByIDHandler) Handle(ctx context.Context, query GetHabitByIDQuer
|
||||
Frequency: habit.Frequency,
|
||||
TargetValue: habit.TargetValue,
|
||||
CarryOver: habit.CarryOver,
|
||||
IsNegative: habit.IsNegative,
|
||||
SpecificDays: habit.SpecificDays,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (m *mockHabitRepoWithFindByID) FindByID(ctx context.Context, id string) (*e
|
||||
|
||||
func TestGetHabitByIDHandler_ReturnsHabitSuccessfully(t *testing.T) {
|
||||
targetValue := 5.0
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true)
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.TargetValue = &targetValue
|
||||
|
||||
@@ -82,7 +82,7 @@ func TestGetHabitByIDHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHabitByIDHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoWithFindByID{
|
||||
|
||||
@@ -25,7 +25,7 @@ func (m *mockEntryRepoWithFindByHabitID) FindByHabitIDAndDateRange(ctx context.C
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_ReturnsEntriesSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
date1 := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
|
||||
@@ -101,7 +101,7 @@ func TestGetHabitEntriesHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoWithFindByID{
|
||||
@@ -127,7 +127,7 @@ func TestGetHabitEntriesHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_WithPagination(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
entries := make([]*entities.HabitEntry, 10)
|
||||
@@ -179,7 +179,7 @@ func TestGetHabitEntriesHandler_WithPagination(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_RequiresPaginationWithoutDateRange(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoWithFindByID{
|
||||
@@ -207,7 +207,7 @@ func TestGetHabitEntriesHandler_RequiresPaginationWithoutDateRange(t *testing.T)
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_AllowsNoPaginationWithShortDateRange(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
from := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
@@ -247,7 +247,7 @@ func TestGetHabitEntriesHandler_AllowsNoPaginationWithShortDateRange(t *testing.
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_RequiresPaginationWithLongDateRange(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
from := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
@@ -14,6 +14,7 @@ type TodaysHabitDTO struct {
|
||||
Name string
|
||||
Type value_objects.HabitType
|
||||
TargetValue *float64
|
||||
IsNegative bool
|
||||
ScheduledDate time.Time
|
||||
IsCarriedOver bool
|
||||
}
|
||||
@@ -83,6 +84,7 @@ func (h *GetTodaysHabitsHandler) Handle(
|
||||
Name: habit.Name,
|
||||
Type: habit.Type,
|
||||
TargetValue: habit.TargetValue,
|
||||
IsNegative: habit.IsNegative,
|
||||
ScheduledDate: query.Date,
|
||||
IsCarriedOver: !shouldAppear && habit.CarryOver,
|
||||
})
|
||||
|
||||
@@ -76,7 +76,7 @@ func (m *mockEntryRepo) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_DailyHabitNoEntries(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepo{habits: []*entities.Habit{habit}}
|
||||
@@ -110,7 +110,7 @@ func TestGetTodaysHabitsHandler_DailyHabitNoEntries(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_DailyHabitAlreadyCompleted(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
targetDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
|
||||
@@ -139,7 +139,7 @@ func TestGetTodaysHabitsHandler_DailyHabitAlreadyCompleted(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_WeeklyHabitOnCorrectDay(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1, 3, 5}
|
||||
|
||||
@@ -168,7 +168,7 @@ func TestGetTodaysHabitsHandler_WeeklyHabitOnCorrectDay(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_WeeklyHabitOnWrongDay(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1, 3, 5}
|
||||
|
||||
@@ -197,7 +197,7 @@ func TestGetTodaysHabitsHandler_WeeklyHabitOnWrongDay(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_CarryOverEnabled(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, true)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, true, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1}
|
||||
|
||||
@@ -230,7 +230,7 @@ func TestGetTodaysHabitsHandler_CarryOverEnabled(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_CarryOverDisabled(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ type HabitDTO struct {
|
||||
Frequency value_objects.Frequency
|
||||
TargetValue *float64
|
||||
CarryOver bool
|
||||
IsNegative bool
|
||||
SpecificDays []int
|
||||
}
|
||||
|
||||
@@ -46,6 +47,7 @@ func (h *GetUserHabitsHandler) Handle(ctx context.Context, query GetUserHabitsQu
|
||||
Frequency: habit.Frequency,
|
||||
TargetValue: habit.TargetValue,
|
||||
CarryOver: habit.CarryOver,
|
||||
IsNegative: habit.IsNegative,
|
||||
SpecificDays: habit.SpecificDays,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ import (
|
||||
)
|
||||
|
||||
func TestGetUserHabitsHandler_ReturnsAllActiveHabits(t *testing.T) {
|
||||
habit1 := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit1 := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit1.ID = "habit-1"
|
||||
|
||||
habit2 := entities.NewHabit("user-123", "Read", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit2 := entities.NewHabit("user-123", "Read", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit2.ID = "habit-2"
|
||||
|
||||
habitRepo := &mockHabitRepo{habits: []*entities.Habit{habit1, habit2}}
|
||||
@@ -64,7 +64,7 @@ func TestGetUserHabitsHandler_ReturnsEmptyListForUserWithNoHabits(t *testing.T)
|
||||
|
||||
func TestGetUserHabitsHandler_IncludesAllHabitFields(t *testing.T) {
|
||||
targetValue := 5.0
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true)
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.TargetValue = &targetValue
|
||||
|
||||
|
||||
@@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user