diff --git a/internal/data/filters.go b/internal/data/filters.go index 6b7b5cd..4fbda6e 100644 --- a/internal/data/filters.go +++ b/internal/data/filters.go @@ -38,3 +38,11 @@ func (f Filters) sortDirection() string { } return "ASC" } + +func (f Filters) limit() int { + return f.PageSize +} + +func (f Filters) offset() int { + return (f.Page - 1) * f.PageSize +} diff --git a/internal/data/movies.go b/internal/data/movies.go index 1c78110..c33ff5c 100644 --- a/internal/data/movies.go +++ b/internal/data/movies.go @@ -141,12 +141,13 @@ func (m MovieModel) GetAll(title string, genres []string, filter Filters) ([]*Mo FROM movies WHERE (to_tsvector('simple', title) @@ plainto_tsquery('simple', $1) OR $1 = '') AND (genres @> $2 OR $2 = '{}') - ORDER BY %s %s, id ASC`, filter.sortColumn(), filter.sortDirection()) + ORDER BY %s %s, id ASC + LIMIT $3 OFFSET $4`, filter.sortColumn(), filter.sortDirection()) ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - rows, err := m.DB.QueryContext(ctx, query, title, pq.Array(genres)) + rows, err := m.DB.QueryContext(ctx, query, title, pq.Array(genres), filter.limit(), filter.offset()) if err != nil { return nil, err }