Files
apocapoc-api/internal/application/queries/get_user_habits.go
T
david 935f742ac9 Add filtering support to GET /api/v1/habits endpoint
Implemented comprehensive filtering capabilities for the habits list endpoint:
- Filter by type (BOOLEAN, COUNTER, VALUE)
- Filter by frequency (DAILY, WEEKLY, MONTHLY)
- Filter by archived status
- Text search in habit name and description
- All filters can be combined
- Filters work with pagination

Technical changes:
- Added FilterParams to GetUserHabitsQuery
- Created HabitFilter struct in repository interface
- Implemented dynamic SQL query building in SQLite repository
- Updated HTTP handler to parse filter query parameters
- Added comprehensive tests for repository and handler filtering
- Updated all test mocks with new filter methods
2025-12-02 01:02:39 +01:00

117 lines
2.9 KiB
Go

package queries
import (
"context"
"apocapoc-api/internal/domain/entities"
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/domain/value_objects"
"apocapoc-api/internal/shared/pagination"
)
type HabitDTO struct {
ID string
Name string
Type value_objects.HabitType
Frequency value_objects.Frequency
TargetValue *float64
CarryOver bool
IsNegative bool
SpecificDays []int
}
type FilterParams struct {
Type *value_objects.HabitType
Frequency *value_objects.Frequency
IncludeArchived bool
Search string
}
type GetUserHabitsQuery struct {
UserID string
PaginationParams *pagination.Params
FilterParams *FilterParams
}
type GetUserHabitsResult struct {
Habits []HabitDTO
Pagination *pagination.Response
}
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) (*GetUserHabitsResult, error) {
var habits []*entities.Habit
var paginationResponse *pagination.Response
var err error
if query.FilterParams != nil {
filter := repositories.HabitFilter{
Type: query.FilterParams.Type,
Frequency: query.FilterParams.Frequency,
IncludeArchived: query.FilterParams.IncludeArchived,
Search: query.FilterParams.Search,
}
habits, err = h.habitRepo.FindByUserIDFiltered(ctx, query.UserID, filter, query.PaginationParams)
if err != nil {
return nil, err
}
if query.PaginationParams != nil {
totalItems, err := h.habitRepo.CountByUserIDFiltered(ctx, query.UserID, filter)
if err != nil {
return nil, err
}
response := pagination.NewResponse(*query.PaginationParams, totalItems)
paginationResponse = &response
}
} else if query.PaginationParams != nil {
habits, err = h.habitRepo.FindActiveByUserIDWithPagination(ctx, query.UserID, *query.PaginationParams)
if err != nil {
return nil, err
}
totalItems, err := h.habitRepo.CountActiveByUserID(ctx, query.UserID)
if err != nil {
return nil, err
}
response := pagination.NewResponse(*query.PaginationParams, totalItems)
paginationResponse = &response
} else {
habits, err = h.habitRepo.FindActiveByUserID(ctx, query.UserID)
if err != nil {
return nil, err
}
}
var habitDTOs []HabitDTO
for _, habit := range habits {
habitDTOs = append(habitDTOs, HabitDTO{
ID: habit.ID,
Name: habit.Name,
Type: habit.Type,
Frequency: habit.Frequency,
TargetValue: habit.TargetValue,
CarryOver: habit.CarryOver,
IsNegative: habit.IsNegative,
SpecificDays: habit.SpecificDays,
})
}
return &GetUserHabitsResult{
Habits: habitDTOs,
Pagination: paginationResponse,
}, nil
}