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
This commit is contained in:
2025-11-26 14:50:11 +01:00
parent e87b7df979
commit 74cd2ec84d
16 changed files with 1474 additions and 7 deletions
@@ -0,0 +1,44 @@
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
}