diff --git a/internal/postgres/migrations/004_add_movies_search_indexes.down.sql b/internal/postgres/migrations/004_add_movies_search_indexes.down.sql new file mode 100644 index 0000000..0cf0291 --- /dev/null +++ b/internal/postgres/migrations/004_add_movies_search_indexes.down.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS movies_genres_idx; \ No newline at end of file diff --git a/internal/postgres/migrations/004_add_movies_search_indexes.up.sql b/internal/postgres/migrations/004_add_movies_search_indexes.up.sql new file mode 100644 index 0000000..18fe276 --- /dev/null +++ b/internal/postgres/migrations/004_add_movies_search_indexes.up.sql @@ -0,0 +1 @@ +CREATE INDEX IF NOT EXISTS movies_genres_idx ON movies USING GIN (genres); \ No newline at end of file diff --git a/internal/postgres/movie.go b/internal/postgres/movie.go index ba4dd5e..29065e9 100644 --- a/internal/postgres/movie.go +++ b/internal/postgres/movie.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "log/slog" + "strings" "github.com/denpeshkov/greenlight/internal/greenlight" "github.com/lib/pq" @@ -52,11 +53,17 @@ func (s *MovieService) GetMovies(ctx context.Context, filter greenlight.MovieFil ctx, cancel := context.WithTimeout(ctx, s.db.opts.queryTimeout) defer cancel() - query := ` + sortCol, sortDir := filter.Sort, "ASC" + if v, ok := strings.CutPrefix(sortCol, "-"); ok { + sortCol = v + sortDir = "DESC" + } + + query := fmt.Sprintf(` SELECT id, title, release_date, runtime, genres, version FROM movies WHERE (LOWER(title) = LOWER($1) OR $1 = '') AND (genres @> $2 OR $2 = '{}') - ORDER BY id ASC` + ORDER BY %s %s, id ASC`, sortCol, sortDir) rs, err := s.db.db.QueryContext(ctx, query, filter.Title, pq.Array(filter.Genres)) if err != nil { return nil, fmt.Errorf("%s: %w", op, err)