10d45fc34e
COUNTER type: - Only accepts integer values (rejects decimals) - Auto-increment behavior: each mark adds to existing value - Supports decrement via negative values (e.g., -2) - Enforces minimum value of 0 (never negative) - Default increment is 1 if no value provided VALUE type: - Accepts decimal values - Replaces value (no auto-increment) IsNegative field: - New boolean field on Habit entity - Persisted in database (is_negative column) - Available in all DTOs and HTTP responses - Marks habits as "negative" (e.g., candy consumption) Examples: - COUNTER: 5 + (-2) = 3, 2 + (-3) = 0, 0 + (-1) = 0 - VALUE: 5000 → 12000 = 12000 (replacement)
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package commands
|
|
|
|
import (
|
|
"context"
|
|
|
|
"apocapoc-api/internal/domain/entities"
|
|
"apocapoc-api/internal/domain/repositories"
|
|
"apocapoc-api/internal/domain/value_objects"
|
|
"apocapoc-api/internal/shared/errors"
|
|
)
|
|
|
|
type CreateHabitCommand struct {
|
|
UserID string
|
|
Name string
|
|
Description string
|
|
Type value_objects.HabitType
|
|
Frequency value_objects.Frequency
|
|
SpecificDays []int
|
|
SpecificDates []int
|
|
CarryOver bool
|
|
IsNegative bool
|
|
TargetValue *float64
|
|
}
|
|
|
|
type CreateHabitHandler struct {
|
|
habitRepo repositories.HabitRepository
|
|
}
|
|
|
|
func NewCreateHabitHandler(habitRepo repositories.HabitRepository) *CreateHabitHandler {
|
|
return &CreateHabitHandler{habitRepo: habitRepo}
|
|
}
|
|
|
|
func (h *CreateHabitHandler) Handle(ctx context.Context, cmd CreateHabitCommand) (string, error) {
|
|
if !cmd.Type.IsValid() {
|
|
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 := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver, cmd.IsNegative)
|
|
habit.Description = cmd.Description
|
|
habit.SpecificDays = cmd.SpecificDays
|
|
habit.SpecificDates = cmd.SpecificDates
|
|
habit.TargetValue = cmd.TargetValue
|
|
|
|
if err := h.habitRepo.Create(ctx, habit); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return habit.ID, nil
|
|
}
|