25 lines
844 B
Python
25 lines
844 B
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_db
|
|
from schemas.fruit import FruitSchema
|
|
from helpers.fruit_helpers import get_all_fruits, get_fruit_by_id
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/fruits", status_code=status.HTTP_200_OK, response_model=List[FruitSchema])
|
|
def get_fruits(
|
|
order_by: Optional[str] = None,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
return get_all_fruits(db, order_by)
|
|
|
|
@router.get("/fruits/{fruit_id}", status_code=status.HTTP_200_OK, response_model=FruitSchema)
|
|
def get_fruit(
|
|
fruit_id: str,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
fruit = get_fruit_by_id(db, fruit_id)
|
|
if not fruit:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Fruit not found")
|
|
return fruit |