74cd2ec84d
Implement all missing endpoints for full habit management:
- GET /api/v1/habits - List all user habits
- GET /api/v1/habits/{id} - Get specific habit
- PUT /api/v1/habits/{id} - Update habit
- DELETE /api/v1/habits/{id} - Archive habit (soft delete)
- GET /api/v1/habits/{id}/entries - Get habit entry history
- DELETE /api/v1/habits/{id}/entries/{date} - Unmark habit (soft delete entry)
All endpoints include:
- TDD approach with comprehensive test coverage
- JWT authentication and ownership validation
- Proper error handling (404, 403, 400, 500)
- Clean architecture with separated commands/queries
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package queries
|
|
|
|
import (
|
|
"context"
|
|
|
|
"apocapoc-api/internal/domain/repositories"
|
|
)
|
|
|
|
type HabitDTO struct {
|
|
ID string
|
|
Name string
|
|
Type string
|
|
Frequency string
|
|
TargetValue *float64
|
|
CarryOver bool
|
|
SpecificDays []int
|
|
}
|
|
|
|
type GetUserHabitsQuery struct {
|
|
UserID string
|
|
}
|
|
|
|
type GetUserHabitsHandler struct {
|
|
habitRepo repositories.HabitRepository
|
|
}
|
|
|
|
func NewGetUserHabitsHandler(habitRepo repositories.HabitRepository) *GetUserHabitsHandler {
|
|
return &GetUserHabitsHandler{
|
|
habitRepo: habitRepo,
|
|
}
|
|
}
|
|
|
|
func (h *GetUserHabitsHandler) Handle(ctx context.Context, query GetUserHabitsQuery) ([]HabitDTO, error) {
|
|
habits, err := h.habitRepo.FindActiveByUserID(ctx, query.UserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []HabitDTO
|
|
for _, habit := range habits {
|
|
result = append(result, HabitDTO{
|
|
ID: habit.ID,
|
|
Name: habit.Name,
|
|
Type: string(habit.Type),
|
|
Frequency: string(habit.Frequency),
|
|
TargetValue: habit.TargetValue,
|
|
CarryOver: habit.CarryOver,
|
|
SpecificDays: habit.SpecificDays,
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|