Add pagination support to GET /api/v1/habits endpoint

- Create pagination package with Params and Response structs
- Add FindActiveByUserIDWithPagination and CountActiveByUserID methods to HabitRepository interface
- Implement pagination in SQLite repository using LIMIT and OFFSET
- Update GetUserHabitsHandler to support optional pagination parameters
- Modify HTTP endpoint to parse 'page' and 'page_size' query params (default: page=1, page_size=50, max=100)
- Add GetUserHabitsResponse DTO with pagination metadata
- Maintain backward compatibility - endpoint works with and without pagination params
- Add comprehensive tests for pagination logic in repository, handler, and pagination package
- Update all mock repositories to implement new pagination methods
This commit is contained in:
2025-12-01 23:53:00 +01:00
parent f780c69806
commit 38e640c617
21 changed files with 774 additions and 41 deletions
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
@@ -229,3 +230,11 @@ func TestCreateHabitHandler_NegativeHabit(t *testing.T) {
t.Error("Expected habit ID to be returned")
}
}
func (m *mockHabitRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockHabitRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
@@ -132,3 +133,11 @@ func TestDeleteUserHandler_DeleteError(t *testing.T) {
t.Errorf("Handle() error = %v, want %v", err, customError)
}
}
func (m *mockDeleteUserRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockDeleteUserRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
"time"
@@ -537,3 +538,19 @@ func TestMarkHabitHandler_CounterFirstMarkWithNegative(t *testing.T) {
t.Fatalf("Expected no error, got %v", err)
}
}
func (m *mockEntryRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockEntryRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
func (m *mockHabitRepoForMark) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockHabitRepoForMark) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"errors"
"testing"
@@ -295,3 +296,11 @@ func TestRegisterUserHandler_ClosedRegistration(t *testing.T) {
t.Errorf("expected ErrRegistrationClosed, got %v", err)
}
}
func (m *mockUserRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockUserRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
"time"
@@ -334,3 +335,19 @@ func TestRequestPasswordResetHandler_ResetLinkFormat(t *testing.T) {
t.Fatal("Email body is empty")
}
}
func (m *mockRequestResetTokenRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockRequestResetTokenRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
func (m *mockRequestResetUserRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockRequestResetUserRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
"time"
@@ -379,3 +380,19 @@ func TestResetPasswordHandler_HashingError(t *testing.T) {
t.Fatal("Handle() expected error but got nil")
}
}
func (m *mockPasswordResetTokenRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockPasswordResetTokenRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
func (m *mockResetPasswordUserRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockResetPasswordUserRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
"time"
@@ -186,3 +187,11 @@ func TestRevokeAllTokensHandler_Handle(t *testing.T) {
})
}
}
func (m *mockRefreshTokenRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockRefreshTokenRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
"time"
@@ -137,3 +138,11 @@ func TestUnmarkHabitHandler_ReturnsErrorWhenEntryNotFound(t *testing.T) {
t.Errorf("Expected ErrNotFound for missing entry, got %v", err)
}
}
func (m *mockEntryRepoForUnmark) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockEntryRepoForUnmark) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
@@ -1,6 +1,7 @@
package commands
import (
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
"time"
@@ -336,3 +337,11 @@ func TestVerifyEmailHandler_UpdateError(t *testing.T) {
t.Fatal("Handle() expected error but got nil")
}
}
func (m *mockVerifyEmailUserRepo) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockVerifyEmailUserRepo) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}