Files
apocapoc-api/internal/application/queries/get_habit_by_id.go
T
david 74cd2ec84d Complete CRUD operations for habits
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
2025-11-26 14:50:11 +01:00

45 lines
959 B
Go

package queries
import (
"context"
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/shared/errors"
)
type GetHabitByIDQuery struct {
HabitID string
UserID string
}
type GetHabitByIDHandler struct {
habitRepo repositories.HabitRepository
}
func NewGetHabitByIDHandler(habitRepo repositories.HabitRepository) *GetHabitByIDHandler {
return &GetHabitByIDHandler{
habitRepo: habitRepo,
}
}
func (h *GetHabitByIDHandler) Handle(ctx context.Context, query GetHabitByIDQuery) (*HabitDTO, error) {
habit, err := h.habitRepo.FindByID(ctx, query.HabitID)
if err != nil {
return nil, err
}
if habit.UserID != query.UserID {
return nil, errors.ErrUnauthorized
}
return &HabitDTO{
ID: habit.ID,
Name: habit.Name,
Type: string(habit.Type),
Frequency: string(habit.Frequency),
TargetValue: habit.TargetValue,
CarryOver: habit.CarryOver,
SpecificDays: habit.SpecificDays,
}, nil
}