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
This commit is contained in:
2025-11-27 00:39:45 +01:00
parent 7768037724
commit ca2533d4df
11 changed files with 284 additions and 54 deletions
@@ -35,8 +35,8 @@ func (h *GetHabitByIDHandler) Handle(ctx context.Context, query GetHabitByIDQuer
return &HabitDTO{
ID: habit.ID,
Name: habit.Name,
Type: string(habit.Type),
Frequency: string(habit.Frequency),
Type: habit.Type,
Frequency: habit.Frequency,
TargetValue: habit.TargetValue,
CarryOver: habit.CarryOver,
SpecificDays: habit.SpecificDays,
@@ -53,7 +53,7 @@ func TestGetHabitByIDHandler_ReturnsHabitSuccessfully(t *testing.T) {
t.Errorf("Expected name 'Drink Water', got %s", result.Name)
}
if result.Type != string(value_objects.HabitTypeValue) {
if result.Type != value_objects.HabitTypeValue {
t.Errorf("Expected type %s, got %s", value_objects.HabitTypeValue, result.Type)
}
@@ -5,13 +5,14 @@ import (
"time"
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/domain/value_objects"
"apocapoc-api/internal/shared/utils"
)
type TodaysHabitDTO struct {
ID string
Name string
Type string
Type value_objects.HabitType
TargetValue *float64
ScheduledDate time.Time
IsCarriedOver bool
@@ -80,7 +81,7 @@ func (h *GetTodaysHabitsHandler) Handle(
result = append(result, TodaysHabitDTO{
ID: habit.ID,
Name: habit.Name,
Type: string(habit.Type),
Type: habit.Type,
TargetValue: habit.TargetValue,
ScheduledDate: query.Date,
IsCarriedOver: !shouldAppear && habit.CarryOver,
@@ -4,13 +4,14 @@ import (
"context"
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/domain/value_objects"
)
type HabitDTO struct {
ID string
Name string
Type string
Frequency string
Type value_objects.HabitType
Frequency value_objects.Frequency
TargetValue *float64
CarryOver bool
SpecificDays []int
@@ -41,8 +42,8 @@ func (h *GetUserHabitsHandler) Handle(ctx context.Context, query GetUserHabitsQu
result = append(result, HabitDTO{
ID: habit.ID,
Name: habit.Name,
Type: string(habit.Type),
Frequency: string(habit.Frequency),
Type: habit.Type,
Frequency: habit.Frequency,
TargetValue: habit.TargetValue,
CarryOver: habit.CarryOver,
SpecificDays: habit.SpecificDays,
@@ -92,11 +92,11 @@ func TestGetUserHabitsHandler_IncludesAllHabitFields(t *testing.T) {
t.Errorf("Expected name 'Drink Water', got %s", result.Name)
}
if result.Type != string(value_objects.HabitTypeValue) {
if result.Type != value_objects.HabitTypeValue {
t.Errorf("Expected type %s, got %s", value_objects.HabitTypeValue, result.Type)
}
if result.Frequency != string(value_objects.FrequencyDaily) {
if result.Frequency != value_objects.FrequencyDaily {
t.Errorf("Expected frequency %s, got %s", value_objects.FrequencyDaily, result.Frequency)
}