From 35124ea8755d20f382eeb670a278d4df27b03f6f Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Sun, 19 Feb 2023 18:48:19 -0600 Subject: [PATCH] feat: Shopping List Item Pagination Route (#2145) * added pagination route for list items * pytest --- mealie/routes/groups/controller_shopping_lists.py | 7 +++++++ mealie/schema/group/group_shopping_list.py | 4 ++++ .../test_group_shopping_list_items.py | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/mealie/routes/groups/controller_shopping_lists.py b/mealie/routes/groups/controller_shopping_lists.py index 3c8739ea437..090b8188197 100644 --- a/mealie/routes/groups/controller_shopping_lists.py +++ b/mealie/routes/groups/controller_shopping_lists.py @@ -12,6 +12,7 @@ ShoppingListCreate, ShoppingListItemCreate, ShoppingListItemOut, + ShoppingListItemPagination, ShoppingListItemsCollectionOut, ShoppingListItemUpdate, ShoppingListItemUpdateBulk, @@ -101,6 +102,12 @@ def mixins(self): self.logger, ) + @item_router.get("", response_model=ShoppingListItemPagination) + def get_all(self, q: PaginationQuery = Depends()): + response = self.repo.page_all(pagination=q, override=ShoppingListItemOut) + response.set_pagination_guides(router.url_path_for("get_all"), q.dict()) + return response + @item_router.post("/create-bulk", response_model=ShoppingListItemsCollectionOut, status_code=201) def create_many(self, data: list[ShoppingListItemCreate]): items = self.service.bulk_create_items(data) diff --git a/mealie/schema/group/group_shopping_list.py b/mealie/schema/group/group_shopping_list.py index e9ea6baf1f4..a62ddf47aeb 100644 --- a/mealie/schema/group/group_shopping_list.py +++ b/mealie/schema/group/group_shopping_list.py @@ -177,6 +177,10 @@ class ShoppingListItemsCollectionOut(MealieModel): deleted_items: list[ShoppingListItemOut] = [] +class ShoppingListItemPagination(PaginationBase): + items: list[ShoppingListItemOut] + + class ShoppingListCreate(MealieModel): name: str | None = None extras: dict | None = {} diff --git a/tests/integration_tests/user_group_tests/test_group_shopping_list_items.py b/tests/integration_tests/user_group_tests/test_group_shopping_list_items.py index 21b5a996619..bf8aa541b03 100644 --- a/tests/integration_tests/user_group_tests/test_group_shopping_list_items.py +++ b/tests/integration_tests/user_group_tests/test_group_shopping_list_items.py @@ -101,6 +101,17 @@ def test_shopping_list_items_get_one( assert response.status_code == 200 +def test_shopping_list_items_get_all( + api_client: TestClient, + unique_user: TestUser, + list_with_items: ShoppingListOut, +) -> None: + params = {"page": 1, "perPage": -1, "queryFilter": f"shopping_list_id={list_with_items.id}"} + response = api_client.get(api_routes.groups_shopping_items, params=params, headers=unique_user.token) + pagination_json = utils.assert_derserialize(response, 200) + assert len(pagination_json["items"]) == len(list_with_items.list_items) + + def test_shopping_list_items_get_one_404(api_client: TestClient, unique_user: TestUser) -> None: response = api_client.get(api_routes.groups_shopping_items_item_id(uuid4()), headers=unique_user.token) assert response.status_code == 404