ch9.7
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user