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
}