ch9.6
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import "gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
||||||
|
)
|
||||||
|
|
||||||
type Filters struct {
|
type Filters struct {
|
||||||
Page int
|
Page int
|
||||||
@@ -17,3 +21,20 @@ func ValidateFilters(v *validator.Validator, f Filters) {
|
|||||||
|
|
||||||
v.Check(validator.PermittedValue(f.Sort, f.SortSafelist...), "sort", "invalid sort values")
|
v.Check(validator.PermittedValue(f.Sort, f.SortSafelist...), "sort", "invalid sort values")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f Filters) sortColumn() string {
|
||||||
|
for _, safeValue := range f.SortSafelist {
|
||||||
|
if f.Sort == safeValue {
|
||||||
|
return strings.TrimPrefix(f.Sort, "-")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("unsafe sort parameter: " + f.Sort)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Filters) sortDirection() string {
|
||||||
|
if strings.HasPrefix(f.Sort, "-") {
|
||||||
|
return "DESC"
|
||||||
|
}
|
||||||
|
return "ASC"
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
||||||
@@ -135,12 +136,12 @@ func (m MovieModel) Delete(id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m MovieModel) GetAll(title string, genres []string, filter Filters) ([]*Movie, error) {
|
func (m MovieModel) GetAll(title string, genres []string, filter Filters) ([]*Movie, error) {
|
||||||
query := `
|
query := fmt.Sprintf(`
|
||||||
SELECT id, created_at, title, year, runtime, genres, version
|
SELECT id, created_at, title, year, runtime, genres, version
|
||||||
FROM movies
|
FROM movies
|
||||||
WHERE (to_tsvector('simple', title) @@ plainto_tsquery('simple', $1) OR $1 = '')
|
WHERE (to_tsvector('simple', title) @@ plainto_tsquery('simple', $1) OR $1 = '')
|
||||||
AND (genres @> $2 OR $2 = '{}')
|
AND (genres @> $2 OR $2 = '{}')
|
||||||
ORDER BY id`
|
ORDER BY %s %s, id ASC`, filter.sortColumn(), filter.sortDirection())
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|||||||
Reference in New Issue
Block a user