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:
@@ -13,8 +13,8 @@ type CreateHabitCommand struct {
|
||||
UserID string
|
||||
Name string
|
||||
Description string
|
||||
Type string
|
||||
Frequency string
|
||||
Type value_objects.HabitType
|
||||
Frequency value_objects.Frequency
|
||||
SpecificDays []int
|
||||
SpecificDates []int
|
||||
CarryOver bool
|
||||
@@ -30,25 +30,23 @@ func NewCreateHabitHandler(habitRepo repositories.HabitRepository) *CreateHabitH
|
||||
}
|
||||
|
||||
func (h *CreateHabitHandler) Handle(ctx context.Context, cmd CreateHabitCommand) (string, error) {
|
||||
habitType := value_objects.HabitType(cmd.Type)
|
||||
if !habitType.IsValid() {
|
||||
if !cmd.Type.IsValid() {
|
||||
return "", errors.ErrInvalidInput
|
||||
}
|
||||
|
||||
frequency := value_objects.Frequency(cmd.Frequency)
|
||||
if !frequency.IsValid() {
|
||||
if !cmd.Frequency.IsValid() {
|
||||
return "", errors.ErrInvalidInput
|
||||
}
|
||||
|
||||
if frequency == value_objects.FrequencyWeekly && len(cmd.SpecificDays) == 0 {
|
||||
if cmd.Frequency == value_objects.FrequencyWeekly && len(cmd.SpecificDays) == 0 {
|
||||
return "", errors.ErrInvalidInput
|
||||
}
|
||||
|
||||
if frequency == value_objects.FrequencyMonthly && len(cmd.SpecificDates) == 0 {
|
||||
if cmd.Frequency == value_objects.FrequencyMonthly && len(cmd.SpecificDates) == 0 {
|
||||
return "", errors.ErrInvalidInput
|
||||
}
|
||||
|
||||
habit := entities.NewHabit(cmd.UserID, cmd.Name, habitType, frequency, cmd.CarryOver)
|
||||
habit := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver)
|
||||
habit.Description = cmd.Description
|
||||
habit.SpecificDays = cmd.SpecificDays
|
||||
habit.SpecificDates = cmd.SpecificDates
|
||||
|
||||
Reference in New Issue
Block a user