Skip to content

Commit

Permalink
ECOM-12827 feat: move pagination to the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
cvejk committed Dec 10, 2024
1 parent 8a2e9cf commit eab0892
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
18 changes: 17 additions & 1 deletion domain/redirectdefinition/query/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type (
Source redirectstore.RedirectSource `json:"source"`
Dimension redirectstore.Dimension `json:"dimension"`
OnlyActive bool `json:"onlyActive"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
// SearchHandlerFn handler
SearchHandlerFn func(ctx context.Context, l *zap.Logger, qry Search) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error)
Expand All @@ -28,7 +30,21 @@ type (
// SearchHandler ...
func SearchHandler(repo redirectrepository.RedirectsDefinitionRepository) SearchHandlerFn {
return func(ctx context.Context, _ *zap.Logger, qry Search) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
return repo.FindMany(ctx, string(qry.Source), string(qry.Dimension), qry.OnlyActive)
// Default pagination values if not provided
page := qry.Page
if page < 1 {
page = 1
}
pageSize := qry.PageSize
if pageSize < 1 {
pageSize = 20 // Default page size
}
// Create pagination struct
pagination := redirectrepository.Pagination{Page: page, PageSize: pageSize}

// Define sort logic (example: sort by "source" ascending)
sort := redirectrepository.Sort{Field: "source", Direction: 1}
return repo.FindMany(ctx, string(qry.Source), string(qry.Dimension), qry.OnlyActive, pagination, sort)
}
}

Expand Down
26 changes: 23 additions & 3 deletions domain/redirectdefinition/repository/redirectdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ import (
"go.uber.org/zap"
)

type Pagination struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
}

type Sort struct {
Field string `json:"field"`
Direction int `json:"direction"` // 1 for ascending, -1 for descending
}

type (
RedirectsDefinitionRepository interface {
FindOne(ctx context.Context, id, source string) (*redirectstore.RedirectDefinition, error)
FindMany(ctx context.Context, source, dimension string, onlyActive bool) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error)
FindMany(ctx context.Context, source, dimension string, onlyActive bool, pagination Pagination, sort Sort) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error)
FindAll(ctx context.Context, onlyActive bool) (defs map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, err error)
Insert(ctx context.Context, def *redirectstore.RedirectDefinition) error
Update(ctx context.Context, def *redirectstore.RedirectDefinition) error
Expand Down Expand Up @@ -67,7 +77,7 @@ func (rs BaseRedirectsDefinitionRepository) FindOne(ctx context.Context, id, sou
return &result, nil
}

func (rs BaseRedirectsDefinitionRepository) FindMany(ctx context.Context, source, dimension string, onlyActive bool) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
func (rs BaseRedirectsDefinitionRepository) FindMany(ctx context.Context, source, dimension string, onlyActive bool, pagination Pagination, sort Sort) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
var result []*redirectstore.RedirectDefinition
filter := bson.M{}

Expand All @@ -85,7 +95,17 @@ func (rs BaseRedirectsDefinitionRepository) FindMany(ctx context.Context, source
filter["stale"] = false
}

findErr := rs.collection.Find(ctx, filter, &result)
// Calculate skip and limit for pagination
skip := (pagination.Page - 1) * pagination.PageSize
limit := pagination.PageSize

// Add pagination and sorting options
opts := options.Find().SetSkip(int64(skip)).SetLimit(int64(limit))
if sort.Field != "" {
opts.SetSort(bson.D{{Key: sort.Field, Value: sort.Direction}})
}

findErr := rs.collection.Find(ctx, filter, &result, opts)
var retResult = make(map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition)
for _, red := range result {
if _, ok := retResult[red.Source]; !ok {
Expand Down
34 changes: 1 addition & 33 deletions domain/redirectdefinition/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,7 @@ func (rs *Service) Search(
return nil, redirectstore.NewRedirectDefinitionError(err.Error())
}

paginatedResult := paginateResult(result, page, pageSize)

return paginatedResult, nil
}

func paginateResult(
results map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition,
page, pageSize int,
) map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition {
keys := make([]redirectstore.RedirectSource, 0, len(results))
for key := range results {
keys = append(keys, key)
}

// Determine the start and end indices for the requested page
start := (page - 1) * pageSize
end := start + pageSize

// Ensure indices are within bounds
if start > len(keys) {
return nil // No results for this page
}

if end > len(keys) {
end = len(keys)
}

paginated := make(map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, end-start)
for _, key := range keys[start:end] {
paginated[key] = results[key]
}

return paginated
return result, nil
}

// Create a redirect
Expand Down

0 comments on commit eab0892

Please sign in to comment.