feat: allow editing frequency and target_value on habits

- Add frequency to UpdateHabitRequest (was immutable, now editable)
- Validate frequency + specific_days/dates coherence on update
- Keep type and is_negative immutable (they change entry semantics)
- Remove completion_rate references from swagger and README
This commit is contained in:
2026-03-08 00:02:37 +01:00
parent 2b059ec334
commit 94c5c30d09
11 changed files with 125 additions and 36 deletions
+19 -4
View File
@@ -5,6 +5,7 @@ import (
"strings"
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/domain/value_objects"
"apocapoc-api/internal/shared/errors"
)
@@ -13,10 +14,11 @@ type UpdateHabitCommand struct {
UserID string
Name string
Description string
CarryOver bool
TargetValue *float64
Frequency value_objects.Frequency
SpecificDays []int
SpecificDates []int
CarryOver bool
TargetValue *float64
}
type UpdateHabitHandler struct {
@@ -34,6 +36,18 @@ func (h *UpdateHabitHandler) Handle(ctx context.Context, cmd UpdateHabitCommand)
return errors.ErrInvalidInput
}
if !cmd.Frequency.IsValid() {
return errors.ErrInvalidInput
}
if cmd.Frequency == value_objects.FrequencyWeekly && len(cmd.SpecificDays) == 0 {
return errors.ErrInvalidInput
}
if cmd.Frequency == value_objects.FrequencyMonthly && len(cmd.SpecificDates) == 0 {
return errors.ErrInvalidInput
}
habit, err := h.habitRepo.FindByID(ctx, cmd.HabitID)
if err != nil {
return err
@@ -49,10 +63,11 @@ func (h *UpdateHabitHandler) Handle(ctx context.Context, cmd UpdateHabitCommand)
habit.Name = cmd.Name
habit.Description = cmd.Description
habit.CarryOver = cmd.CarryOver
habit.TargetValue = cmd.TargetValue
habit.Frequency = cmd.Frequency
habit.SpecificDays = cmd.SpecificDays
habit.SpecificDates = cmd.SpecificDates
habit.CarryOver = cmd.CarryOver
habit.TargetValue = cmd.TargetValue
return h.habitRepo.Update(ctx, habit)
}