feat: category and tag filters to recipe pagination route (#1508)

* fixed incorrect response model

* added category and tag filters

* moved categories and tags params to route and
changed to query array param

* type fixes

* added category and tag tests
This commit is contained in:
Michael Genson
2022-08-09 17:01:59 -05:00
committed by GitHub
parent e82e7d0fb3
commit f45e2587a0
3 changed files with 219 additions and 19 deletions

View File

@@ -129,7 +129,14 @@ class RepositoryRecipes(RepositoryGeneric[Recipe, RecipeModel]):
.all()
)
def page_all(self, pagination: PaginationQuery, override=None, load_food=False) -> RecipePagination:
def page_all(
self,
pagination: PaginationQuery,
override=None,
load_food=False,
categories: Optional[list[UUID4 | str]] = None,
tags: Optional[list[UUID4 | str]] = None,
) -> RecipePagination:
q = self.session.query(self.model)
args = [
@@ -145,6 +152,23 @@ class RepositoryRecipes(RepositoryGeneric[Recipe, RecipeModel]):
fltr = self._filter_builder()
q = q.filter_by(**fltr)
if categories:
for category in categories:
if isinstance(category, UUID):
q = q.filter(RecipeModel.recipe_category.any(Category.id == category))
else:
q = q.filter(RecipeModel.recipe_category.any(Category.slug == category))
if tags:
for tag in tags:
if isinstance(tag, UUID):
q = q.filter(RecipeModel.tags.any(Tag.id == tag))
else:
q = q.filter(RecipeModel.tags.any(Tag.slug == tag))
q, count, total_pages = self.add_pagination_to_query(q, pagination)
try: