Files
apocapoc-api/internal/application/queries/get_user_habits.go
T
david ca2533d4df Improve type safety with proper value objects for HabitType and Frequency
Replace generic string types with strongly-typed value objects throughout
the application layer. This change ensures compile-time type checking and
automatic validation during JSON deserialization.

Changes:
- Add JSON marshaling/unmarshaling to HabitType and Frequency value objects
- Update all DTOs to use typed fields instead of strings
- Update commands and queries to use proper types
- Remove unnecessary string conversions
- Add comprehensive JSON serialization tests
- Fix existing tests to work with typed fields

Benefits:
- Type safety: compiler catches invalid usage
- Automatic validation: invalid values rejected during JSON parsing
- Better code documentation and self-explanatory APIs
- Reduced runtime errors
2025-11-27 00:39:45 +01:00

55 lines
1.2 KiB
Go

package queries
import (
"context"
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/domain/value_objects"
)
type HabitDTO struct {
ID string
Name string
Type value_objects.HabitType
Frequency value_objects.Frequency
TargetValue *float64
CarryOver bool
SpecificDays []int
}
type GetUserHabitsQuery struct {
UserID string
}
type GetUserHabitsHandler struct {
habitRepo repositories.HabitRepository
}
func NewGetUserHabitsHandler(habitRepo repositories.HabitRepository) *GetUserHabitsHandler {
return &GetUserHabitsHandler{
habitRepo: habitRepo,
}
}
func (h *GetUserHabitsHandler) Handle(ctx context.Context, query GetUserHabitsQuery) ([]HabitDTO, error) {
habits, err := h.habitRepo.FindActiveByUserID(ctx, query.UserID)
if err != nil {
return nil, err
}
var result []HabitDTO
for _, habit := range habits {
result = append(result, HabitDTO{
ID: habit.ID,
Name: habit.Name,
Type: habit.Type,
Frequency: habit.Frequency,
TargetValue: habit.TargetValue,
CarryOver: habit.CarryOver,
SpecificDays: habit.SpecificDays,
})
}
return result, nil
}