Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API Endpoint for Reading Catalogue Items #49

Merged
merged 8 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def list(self, path: Optional[str], parent_path: Optional[str]) -> List[Catalogu
logger.info(message)
else:
logger.info("%s matching the provided filter(s)", message)
logger.debug("Provided filters: %s", query)
logger.debug("Provided filter(s): %s", query)

catalogue_categories = self._collection.find(query)
return [CatalogueCategoryOut(**catalogue_category) for catalogue_category in catalogue_categories]
Expand Down
24 changes: 23 additions & 1 deletion inventory_management_system_api/repositories/catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module for providing a repository for managing catalogue items in a MongoDB database.
"""
import logging
from typing import Optional
from typing import Optional, List

from fastapi import Depends
from pymongo.collection import Collection
Expand Down Expand Up @@ -62,6 +62,28 @@ def get(self, catalogue_item_id: str) -> Optional[CatalogueItemOut]:
return CatalogueItemOut(**catalogue_item)
return None

def list(self, catalogue_category_id: Optional[str]) -> List[CatalogueItemOut]:
"""
Retrieve all catalogue items from a MongoDB.

:param catalogue_category_id: The ID of the catalogue category to filter catalogue items by.
:return: A list of catalogue items, or an empty list if no catalogue items are returned by the database.
"""
query = {}
if catalogue_category_id:
catalogue_category_id = CustomObjectId(catalogue_category_id)
query["catalogue_category_id"] = catalogue_category_id

message = "Retrieving all catalogue items from the database"
if not query:
logger.info(message)
else:
logger.info("%s matching the provided catalogue category ID filter", message)
logger.debug("Provided catalogue category ID filter: %s", catalogue_category_id)

catalogue_items = self._collection.find(query)
return [CatalogueItemOut(**catalogue_item) for catalogue_item in catalogue_items]

def _is_duplicate_catalogue_item(self, catalogue_category_id: str, name: str) -> bool:
"""
Check if a catalogue item with the same name already exists within the catalogue category.
Expand Down
23 changes: 22 additions & 1 deletion inventory_management_system_api/routers/v1/catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
service.
"""
import logging
from typing import List, Annotated, Optional

from fastapi import APIRouter, status, Depends, HTTPException, Path
from fastapi import APIRouter, status, Depends, HTTPException, Path, Query

from inventory_management_system_api.core.exceptions import (
MissingRecordError,
Expand All @@ -22,6 +23,26 @@
router = APIRouter(prefix="/v1/catalogue-items", tags=["catalogue items"])


@router.get(path="/", summary="Get catalogue items", response_description="List of catalogue items")
def get_catalogue_items(
catalogue_category_id: Annotated[
Optional[str], Query(description="Filter catalogue items by catalogue category ID")
] = None,
catalogue_item_service: CatalogueItemService = Depends(),
) -> List[CatalogueItemSchema]:
# pylint: disable=missing-function-docstring
logger.info("Getting catalogue items")
if catalogue_category_id:
logger.debug("Catalogue category ID filter: '%s'", catalogue_category_id)

try:
catalogue_items = catalogue_item_service.list(catalogue_category_id)
return [CatalogueItemSchema(**catalogue_item.dict()) for catalogue_item in catalogue_items]
except InvalidObjectIdError:
logger.exception("The provided catalogue category ID filter value is not a valid ObjectId value")
return []


@router.get(
path="/{catalogue_item_id}", summary="Get a catalogue item by ID", response_description="Single catalogue item"
)
Expand Down
4 changes: 3 additions & 1 deletion inventory_management_system_api/schemas/catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class CatalogueItemPostRequestSchema(BaseModel):
Schema model for a catalogue item creation request.
"""

catalogue_category_id: str
catalogue_category_id: str = Field(
description="The ID of the catalogue category that the catalogue item belongs to"
)
name: str = Field(description="The name of the catalogue item")
description: str = Field(description="The catalogue item description")
properties: List[PropertyPostRequestSchema] = Field(description="The catalogue item properties")
Expand Down
11 changes: 10 additions & 1 deletion inventory_management_system_api/services/catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import logging
from numbers import Number
from typing import Optional, Dict
from typing import Optional, List, Dict

from fastapi import Depends

Expand Down Expand Up @@ -195,3 +195,12 @@ def get(self, catalogue_item_id: str) -> Optional[CatalogueItemOut]:
:return: The retrieved catalogue item, or `None` if not found.
"""
return self._catalogue_item_repository.get(catalogue_item_id)

def list(self, catalogue_category_id: Optional[str]) -> List[CatalogueItemOut]:
"""
Retrieve all catalogue items.

:param catalogue_category_id: The ID of the catalogue category to filter catalogue items by.
:return: A list of catalogue items, or an empty list if no catalogue items are retrieved.
"""
return self._catalogue_item_repository.list(catalogue_category_id)
10 changes: 5 additions & 5 deletions test/e2e/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_delete_catalogue_category_with_invalid_id(test_client):
assert response.json()["detail"] == "A catalogue category with such ID was not found"


def test_delete_catalogue_with_children_elements(test_client):
def test_delete_catalogue_category_with_children_elements(test_client):
"""
Test deleting a catalogue category with children elements.
"""
Expand Down Expand Up @@ -371,7 +371,7 @@ def test_get_catalogue_categories(test_client):
assert catalogue_categories[2]["parent_path"] == "/category-b"


def test_list_with_path_filter(test_client):
def test_get_catalogue_categories_with_path_filter(test_client):
"""
Test getting catalogue categories based on the provided parent path filter.
"""
Expand All @@ -391,7 +391,7 @@ def test_list_with_path_filter(test_client):
assert catalogue_categories[0]["parent_path"] == "/"


def test_list_with_parent_path_filter(test_client):
def test_get_catalogue_categories_with_parent_path_filter(test_client):
"""
Test getting catalogue categories based on the provided parent path filter.
"""
Expand Down Expand Up @@ -422,7 +422,7 @@ def test_list_with_parent_path_filter(test_client):
assert catalogue_categories[0]["parent_path"] == "/"


def test_list_with_path_and_parent_path_filters(test_client):
def test_get_catalogue_categories_with_path_and_parent_path_filters(test_client):
"""
Test getting catalogue categories based on the provided path and parent path filters.
"""
Expand All @@ -442,7 +442,7 @@ def test_list_with_path_and_parent_path_filters(test_client):
assert catalogue_categories[0]["parent_path"] == "/"


def test_list_with_path_and_parent_path_filters_no_matching_results(test_client):
def test_get_catalogue_categories_with_path_and_parent_path_filters_no_matching_results(test_client):
"""
Test getting catalogue categories based on the provided path and parent path filters when there is no matching
results in the database.
Expand Down
Loading