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
+38 -34
View File
@@ -1,16 +1,20 @@
package http
import "time"
import (
"time"
"apocapoc-api/internal/domain/value_objects"
)
type CreateHabitRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Frequency string `json:"frequency"`
SpecificDays []int `json:"specific_days,omitempty"`
SpecificDates []int `json:"specific_dates,omitempty"`
CarryOver bool `json:"carry_over"`
TargetValue *float64 `json:"target_value,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Type value_objects.HabitType `json:"type"`
Frequency value_objects.Frequency `json:"frequency"`
SpecificDays []int `json:"specific_days,omitempty"`
SpecificDates []int `json:"specific_dates,omitempty"`
CarryOver bool `json:"carry_over"`
TargetValue *float64 `json:"target_value,omitempty"`
}
type UpdateHabitRequest struct {
@@ -23,18 +27,18 @@ type UpdateHabitRequest struct {
}
type HabitResponse struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Frequency string `json:"frequency"`
SpecificDays []int `json:"specific_days,omitempty"`
SpecificDates []int `json:"specific_dates,omitempty"`
CarryOver bool `json:"carry_over"`
TargetValue *float64 `json:"target_value,omitempty"`
CreatedAt time.Time `json:"created_at"`
ArchivedAt *time.Time `json:"archived_at,omitempty"`
ID string `json:"id"`
UserID string `json:"user_id"`
Name string `json:"name"`
Description string `json:"description"`
Type value_objects.HabitType `json:"type"`
Frequency value_objects.Frequency `json:"frequency"`
SpecificDays []int `json:"specific_days,omitempty"`
SpecificDates []int `json:"specific_dates,omitempty"`
CarryOver bool `json:"carry_over"`
TargetValue *float64 `json:"target_value,omitempty"`
CreatedAt time.Time `json:"created_at"`
ArchivedAt *time.Time `json:"archived_at,omitempty"`
}
type MarkHabitRequest struct {
@@ -43,22 +47,22 @@ type MarkHabitRequest struct {
}
type TodaysHabitResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
TargetValue *float64 `json:"target_value,omitempty"`
ScheduledDate time.Time `json:"scheduled_date"`
IsCarriedOver bool `json:"is_carried_over"`
ID string `json:"id"`
Name string `json:"name"`
Type value_objects.HabitType `json:"type"`
TargetValue *float64 `json:"target_value,omitempty"`
ScheduledDate time.Time `json:"scheduled_date"`
IsCarriedOver bool `json:"is_carried_over"`
}
type UserHabitResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Frequency string `json:"frequency"`
SpecificDays []int `json:"specific_days,omitempty"`
TargetValue *float64 `json:"target_value,omitempty"`
CarryOver bool `json:"carry_over"`
ID string `json:"id"`
Name string `json:"name"`
Type value_objects.HabitType `json:"type"`
Frequency value_objects.Frequency `json:"frequency"`
SpecificDays []int `json:"specific_days,omitempty"`
TargetValue *float64 `json:"target_value,omitempty"`
CarryOver bool `json:"carry_over"`
}
type HabitEntryResponse struct {