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
+17 -1
View File
@@ -13,6 +13,22 @@ require (
github.com/joho/godotenv v1.5.1
github.com/mattn/go-sqlite3 v1.14.32
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.4
golang.org/x/crypto v0.45.0
)
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
github.com/swaggo/swag v1.16.4 // indirect
golang.org/x/net v0.47.0 // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/tools v0.7.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
+71
View File
@@ -1,14 +1,85 @@
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE=
github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/go-chi/httprate v0.7.4 h1:a2GIjv8he9LRf3712zxxnRdckQCm7I8y8yQhkJ84V6M=
github.com/go-chi/httprate v0.7.4/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.20.0 h1:MYlu0sBgChmCfJxxUKZ8g1cPWFOB37YSZqewK7OKeyA=
github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXymS4zJbYVCZmcgkasdo=
github.com/go-openapi/spec v0.20.6 h1:ich1RQ3WDbfoeTqTAb+5EIxNmpKVJZWBNah9RAT0jIQ=
github.com/go-openapi/spec v0.20.6/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR6VaaBKcWA=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM=
github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe h1:K8pHPVoTgxFJt1lXuIzzOX7zZhZFldJQK/CgKx9BFIc=
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
github.com/swaggo/http-swagger v1.3.4 h1:q7t/XLx0n15H1Q9/tk3Y9L4n210XzJF5WtnDX64a5ww=
github.com/swaggo/http-swagger v1.3.4/go.mod h1:9dAh0unqMBAlbp1uE2Uc2mQTxNMU/ha4UbucIg1MFkQ=
github.com/swaggo/swag v1.16.4 h1:clWJtd9LStiG3VeijiCfOVODP6VpHtKdQy9ELFG3s1A=
github.com/swaggo/swag v1.16.4/go.mod h1:VBsHJRsDvfYvqoiMKnsdwhNV9LEMHgEDZcyVYX0sxPg=
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
@@ -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")
}
}
+58 -1
View File
@@ -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)
}
@@ -12,6 +12,8 @@ import (
type mockEntryRepo struct {
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
+3
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,6 +28,7 @@ func NewHabit(
habitType value_objects.HabitType,
frequency value_objects.Frequency,
carryOver bool,
isNegative bool,
) *Habit {
return &Habit{
UserID: userID,
@@ -34,6 +36,7 @@ func NewHabit(
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
+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,