TestFastApi/api_v1/products/dependencies.py

20 lines
539 B
Python

from fastapi import Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from core.models import db_helper
from . import crud
async def get_product_by_id(
product_id: int,
session: AsyncSession = Depends(db_helper.session_dependency),
):
product = await crud.get_products_by_id(session=session, product_id=product_id)
if product is not None:
return product
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Product {product_id} not found",
)