Implement Application Layer with TDD

- Add date utilities for habit scheduling logic
- Implement CreateHabitHandler with full validation
- Implement GetTodaysHabitsHandler with carry-over support
- Implement MarkHabitHandler for completing habits
- All components developed following TDD methodology
- Complete test coverage for commands and queries
This commit is contained in:
2025-11-25 23:54:49 +01:00
parent 0977d3a58b
commit 34ea1f718e
8 changed files with 1002 additions and 0 deletions
@@ -0,0 +1,62 @@
package commands
import (
"context"
"habit-tracker-api/internal/domain/entities"
"habit-tracker-api/internal/domain/repositories"
"habit-tracker-api/internal/domain/value_objects"
"habit-tracker-api/internal/shared/errors"
)
type CreateHabitCommand struct {
UserID string
Name string
Description string
Type string
Frequency string
SpecificDays []int
SpecificDates []int
CarryOver bool
TargetValue *float64
}
type CreateHabitHandler struct {
habitRepo repositories.HabitRepository
}
func NewCreateHabitHandler(habitRepo repositories.HabitRepository) *CreateHabitHandler {
return &CreateHabitHandler{habitRepo: habitRepo}
}
func (h *CreateHabitHandler) Handle(ctx context.Context, cmd CreateHabitCommand) (string, error) {
habitType := value_objects.HabitType(cmd.Type)
if !habitType.IsValid() {
return "", errors.ErrInvalidInput
}
frequency := value_objects.Frequency(cmd.Frequency)
if !frequency.IsValid() {
return "", errors.ErrInvalidInput
}
if frequency == value_objects.FrequencyWeekly && len(cmd.SpecificDays) == 0 {
return "", errors.ErrInvalidInput
}
if frequency == value_objects.FrequencyMonthly && len(cmd.SpecificDates) == 0 {
return "", errors.ErrInvalidInput
}
habit := entities.NewHabit(cmd.UserID, cmd.Name, habitType, frequency, cmd.CarryOver)
habit.Description = cmd.Description
habit.SpecificDays = cmd.SpecificDays
habit.SpecificDates = cmd.SpecificDates
habit.TargetValue = cmd.TargetValue
if err := h.habitRepo.Create(ctx, habit); err != nil {
return "", err
}
return habit.ID, nil
}
@@ -0,0 +1,197 @@
package commands
import (
"context"
"testing"
"habit-tracker-api/internal/domain/entities"
"habit-tracker-api/internal/shared/errors"
)
type mockHabitRepo struct {
createFunc func(ctx context.Context, habit *entities.Habit) error
}
func (m *mockHabitRepo) Create(ctx context.Context, habit *entities.Habit) error {
return m.createFunc(ctx, habit)
}
func (m *mockHabitRepo) FindByID(ctx context.Context, id string) (*entities.Habit, error) {
return nil, nil
}
func (m *mockHabitRepo) FindByUserID(ctx context.Context, userID string) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockHabitRepo) FindActiveByUserID(ctx context.Context, userID string) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockHabitRepo) Update(ctx context.Context, habit *entities.Habit) error {
return nil
}
func (m *mockHabitRepo) Delete(ctx context.Context, id string) error {
return nil
}
func TestCreateHabitHandler_Success(t *testing.T) {
mock := &mockHabitRepo{
createFunc: func(ctx context.Context, habit *entities.Habit) error {
habit.ID = "habit-123"
return nil
},
}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Exercise",
Description: "Daily workout",
Type: "BOOLEAN",
Frequency: "DAILY",
CarryOver: 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")
}
}
func TestCreateHabitHandler_InvalidType(t *testing.T) {
mock := &mockHabitRepo{}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Exercise",
Type: "INVALID",
Frequency: "DAILY",
}
_, err := handler.Handle(context.Background(), cmd)
if err != errors.ErrInvalidInput {
t.Errorf("Expected ErrInvalidInput, got %v", err)
}
}
func TestCreateHabitHandler_InvalidFrequency(t *testing.T) {
mock := &mockHabitRepo{}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Exercise",
Type: "BOOLEAN",
Frequency: "INVALID",
}
_, err := handler.Handle(context.Background(), cmd)
if err != errors.ErrInvalidInput {
t.Errorf("Expected ErrInvalidInput, got %v", err)
}
}
func TestCreateHabitHandler_WeeklyWithoutSpecificDays(t *testing.T) {
mock := &mockHabitRepo{}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Exercise",
Type: "BOOLEAN",
Frequency: "WEEKLY",
SpecificDays: []int{},
}
_, err := handler.Handle(context.Background(), cmd)
if err != errors.ErrInvalidInput {
t.Errorf("Expected ErrInvalidInput, got %v", err)
}
}
func TestCreateHabitHandler_MonthlyWithoutSpecificDates(t *testing.T) {
mock := &mockHabitRepo{}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Exercise",
Type: "BOOLEAN",
Frequency: "MONTHLY",
SpecificDates: []int{},
}
_, err := handler.Handle(context.Background(), cmd)
if err != errors.ErrInvalidInput {
t.Errorf("Expected ErrInvalidInput, got %v", err)
}
}
func TestCreateHabitHandler_WeeklyWithSpecificDays(t *testing.T) {
mock := &mockHabitRepo{
createFunc: func(ctx context.Context, habit *entities.Habit) error {
habit.ID = "habit-123"
if len(habit.SpecificDays) != 3 {
t.Errorf("Expected 3 specific days, got %d", len(habit.SpecificDays))
}
return nil
},
}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Exercise",
Type: "BOOLEAN",
Frequency: "WEEKLY",
SpecificDays: []int{1, 3, 5},
}
_, err := handler.Handle(context.Background(), cmd)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
func TestCreateHabitHandler_MonthlyWithSpecificDates(t *testing.T) {
mock := &mockHabitRepo{
createFunc: func(ctx context.Context, habit *entities.Habit) error {
habit.ID = "habit-123"
if len(habit.SpecificDates) != 2 {
t.Errorf("Expected 2 specific dates, got %d", len(habit.SpecificDates))
}
return nil
},
}
handler := NewCreateHabitHandler(mock)
cmd := CreateHabitCommand{
UserID: "user-123",
Name: "Pay bills",
Type: "BOOLEAN",
Frequency: "MONTHLY",
SpecificDates: []int{1, 15},
}
_, err := handler.Handle(context.Background(), cmd)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
@@ -0,0 +1,50 @@
package commands
import (
"context"
"fmt"
"time"
"habit-tracker-api/internal/domain/entities"
"habit-tracker-api/internal/domain/repositories"
)
type MarkHabitCommand struct {
HabitID string
ScheduledDate time.Time
Value *float64
}
type MarkHabitHandler struct {
entryRepo repositories.HabitEntryRepository
habitRepo repositories.HabitRepository
}
func NewMarkHabitHandler(
entryRepo repositories.HabitEntryRepository,
habitRepo repositories.HabitRepository,
) *MarkHabitHandler {
return &MarkHabitHandler{
entryRepo: entryRepo,
habitRepo: habitRepo,
}
}
func (h *MarkHabitHandler) Handle(ctx context.Context, cmd MarkHabitCommand) error {
habit, err := h.habitRepo.FindByID(ctx, cmd.HabitID)
if err != nil {
return err
}
if habit == nil {
return fmt.Errorf("habit not found")
}
if !habit.IsActive() {
return fmt.Errorf("habit is archived")
}
entry := entities.NewHabitEntry(cmd.HabitID, cmd.ScheduledDate, cmd.Value)
return h.entryRepo.Create(ctx, entry)
}
@@ -0,0 +1,204 @@
package commands
import (
"context"
"testing"
"time"
"habit-tracker-api/internal/domain/entities"
"habit-tracker-api/internal/domain/value_objects"
"habit-tracker-api/internal/shared/errors"
)
type mockEntryRepo struct {
createFunc func(ctx context.Context, entry *entities.HabitEntry) error
}
func (m *mockEntryRepo) Create(ctx context.Context, entry *entities.HabitEntry) error {
if m.createFunc != nil {
return m.createFunc(ctx, entry)
}
return nil
}
func (m *mockEntryRepo) FindByID(ctx context.Context, id string) (*entities.HabitEntry, error) {
return nil, nil
}
func (m *mockEntryRepo) FindByHabitID(ctx context.Context, habitID string) ([]*entities.HabitEntry, error) {
return nil, nil
}
func (m *mockEntryRepo) FindByHabitIDAndDateRange(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
return nil, nil
}
func (m *mockEntryRepo) FindPendingByHabitID(ctx context.Context, habitID string, beforeDate time.Time) ([]*entities.HabitEntry, error) {
return nil, nil
}
func (m *mockEntryRepo) Update(ctx context.Context, entry *entities.HabitEntry) error {
return nil
}
func (m *mockEntryRepo) Delete(ctx context.Context, id string) error {
return nil
}
type mockHabitRepoForMark struct {
habit *entities.Habit
}
func (m *mockHabitRepoForMark) Create(ctx context.Context, habit *entities.Habit) error {
return nil
}
func (m *mockHabitRepoForMark) FindByID(ctx context.Context, id string) (*entities.Habit, error) {
return m.habit, nil
}
func (m *mockHabitRepoForMark) FindByUserID(ctx context.Context, userID string) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockHabitRepoForMark) FindActiveByUserID(ctx context.Context, userID string) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockHabitRepoForMark) Update(ctx context.Context, habit *entities.Habit) error {
return nil
}
func (m *mockHabitRepoForMark) Delete(ctx context.Context, id string) error {
return nil
}
func TestMarkHabitHandler_Success(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForMark{habit: habit}
entryRepo := &mockEntryRepo{
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
if entry.HabitID != "habit-1" {
t.Errorf("Expected HabitID habit-1, got %s", entry.HabitID)
}
entry.ID = "entry-123"
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_HabitNotFound(t *testing.T) {
habitRepo := &mockHabitRepoForMark{habit: nil}
entryRepo := &mockEntryRepo{}
handler := NewMarkHabitHandler(entryRepo, habitRepo)
cmd := MarkHabitCommand{
HabitID: "non-existent",
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
}
err := handler.Handle(context.Background(), cmd)
if err == nil {
t.Error("Expected error when habit not found")
}
}
func TestMarkHabitHandler_ArchivedHabit(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit.ID = "habit-1"
habit.Archive()
habitRepo := &mockHabitRepoForMark{habit: habit}
entryRepo := &mockEntryRepo{}
handler := NewMarkHabitHandler(entryRepo, habitRepo)
cmd := MarkHabitCommand{
HabitID: "habit-1",
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
}
err := handler.Handle(context.Background(), cmd)
if err == nil {
t.Error("Expected error when habit is archived")
}
}
func TestMarkHabitHandler_WithValue(t *testing.T) {
habit := entities.NewHabit("user-123", "Steps", value_objects.HabitTypeValue, value_objects.FrequencyDaily, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForMark{habit: habit}
value := 5000.0
entryRepo := &mockEntryRepo{
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
if entry.Value == nil {
t.Error("Expected value to be set")
}
if *entry.Value != value {
t.Errorf("Expected value %f, got %f", value, *entry.Value)
}
entry.ID = "entry-123"
return nil
},
}
handler := NewMarkHabitHandler(entryRepo, habitRepo)
cmd := MarkHabitCommand{
HabitID: "habit-1",
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
Value: &value,
}
err := handler.Handle(context.Background(), cmd)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
func TestMarkHabitHandler_DuplicateEntry(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForMark{habit: habit}
entryRepo := &mockEntryRepo{
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
return errors.ErrAlreadyExists
},
}
handler := NewMarkHabitHandler(entryRepo, habitRepo)
cmd := MarkHabitCommand{
HabitID: "habit-1",
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrAlreadyExists {
t.Errorf("Expected ErrAlreadyExists, got %v", err)
}
}