Skip to content

Commit

Permalink
Clean up manufacturer.py router module #312
Browse files Browse the repository at this point in the history
  • Loading branch information
VKTB committed Jul 10, 2024
1 parent e75cb3f commit 8fc0ae3
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions inventory_management_system_api/routers/v1/manufacturer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Module for providing an API router which defines routes for managing manufacturer using the
`Manufacturer` service.
Module for providing an API router which defines routes for managing manufacturer using the `ManufacturerService`
service.
"""

import logging
Expand Down Expand Up @@ -30,8 +30,8 @@

@router.post(
path="",
summary="Create new manufacturer",
response_description="The new manufacturer",
summary="Create a new manufacturer",
response_description="The created manufacturer",
status_code=status.HTTP_201_CREATED,
)
def create_manufacturer(
Expand All @@ -40,26 +40,23 @@ def create_manufacturer(
# pylint: disable=missing-function-docstring
logger.info("Creating a new manufacturer")
logger.debug("Manufacturer data is %s", manufacturer)

try:
manufacturer = manufacturer_service.create(manufacturer)
return ManufacturerSchema(**manufacturer.model_dump())

except DuplicateRecordError as exc:
message = "A manufacturer with the same name has been found"
message = "A manufacturer with the same name already exists"
logger.exception(message)
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=message) from exc


@router.get(
path="",
summary="Get all manufacturers",
summary="Get manufacturers",
response_description="List of manufacturers",
)
def get_all_manufacturers(manufacturer_service: ManufacturerServiceDep) -> List[ManufacturerSchema]:
def get_manufacturers(manufacturer_service: ManufacturerServiceDep) -> List[ManufacturerSchema]:
# pylint: disable=missing-function-docstring
logger.info("Getting manufacturers")

manufacturers = manufacturer_service.list()
return [ManufacturerSchema(**manufacturer.model_dump()) for manufacturer in manufacturers]

Expand All @@ -69,52 +66,51 @@ def get_all_manufacturers(manufacturer_service: ManufacturerServiceDep) -> List[
summary="Get a manufacturer by ID",
response_description="Single manufacturer",
)
def get_one_manufacturer(
def get_manufacturer(
manufacturer_id: Annotated[str, Path(description="The ID of the manufacturer to be retrieved")],
manufacturer_service: ManufacturerServiceDep,
) -> ManufacturerSchema:
# pylint: disable=missing-function-docstring
logger.info("Getting manufacturer with ID %s", manufacturer_id)
logger.info("Getting manufacturer with ID: %s", manufacturer_id)
message = "Manufacturer not found"
try:
manufacturer = manufacturer_service.get(manufacturer_id)
if not manufacturer:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=message)
return ManufacturerSchema(**manufacturer.model_dump())
except InvalidObjectIdError as exc:
logger.exception("The ID is not a valid object value")
logger.exception("The ID is not a valid ObjectId value")
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=message) from exc

return ManufacturerSchema(**manufacturer.model_dump())


@router.patch(
path="/{manufacturer_id}",
summary="Update a manufacturer by its ID",
summary="Update a manufacturer partially by ID",
response_description="Manufacturer updated successfully",
)
def edit_manufacturer(
def partial_update_manufacturer(
manufacturer: ManufacturerPatchSchema,
manufacturer_id: Annotated[str, Path(description="The ID of the manufacturer that is to be updated")],
manufacturer_service: ManufacturerServiceDep,
) -> ManufacturerSchema:
# pylint: disable=missing-function-docstring
logger.info("Updating manufacturer with ID %s", manufacturer_id)
logger.info("Partially updating manufacturer with ID: %s", manufacturer_id)
try:
updated_manufacturer = manufacturer_service.update(manufacturer_id, manufacturer)
return ManufacturerSchema(**updated_manufacturer.model_dump())
except (MissingRecordError, InvalidObjectIdError) as exc:
message = "The specified manufacturer does not exist"
message = "Manufacturer not found"
logger.exception(message)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=message) from exc
except DuplicateRecordError as exc:
message = "A manufacturer with the same name has been found"
message = "A manufacturer with the same name already exists"
logger.exception(message)
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=message) from exc


@router.delete(
path="/{manufacturer_id}",
summary="Delete a manufacturer by its ID",
summary="Delete a manufacturer by ID",
response_description="Manufacturer deleted successfully",
status_code=status.HTTP_204_NO_CONTENT,
)
Expand All @@ -127,10 +123,10 @@ def delete_manufacturer(
try:
manufacturer_service.delete(manufacturer_id)
except (MissingRecordError, InvalidObjectIdError) as exc:
message = "The specified manufacturer does not exist"
message = "Manufacturer not found"
logger.exception(message)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=message) from exc
except PartOfCatalogueItemError as exc:
message = "The specified manufacturer is a part of a Catalogue Item"
message = "The specified manufacturer is a part of a catalogue item"
logger.exception(message)
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=message) from exc

0 comments on commit 8fc0ae3

Please sign in to comment.