feat: Updated endpoint endpoints/fruits.get.py via AI
This commit is contained in:
parent
4156b5ef02
commit
4c16acea7d
19
alembic/versions/20250412_115810_e1a977e1_update_fruit.py
Normal file
19
alembic/versions/20250412_115810_e1a977e1_update_fruit.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
"""add color field to fruits table
|
||||||
|
Revision ID: f9d4e2c7b3a1
|
||||||
|
Revises: d7f3e1b8c5a2
|
||||||
|
Create Date: 2024-01-19 10:23:45.123456
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'f9d4e2c7b3a1'
|
||||||
|
down_revision = 'd7f3e1b8c5a2'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.add_column('fruits', sa.Column('color', sa.String(50), nullable=True))
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_column('fruits', 'color')
|
@ -1,4 +1,4 @@
|
|||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from typing import List
|
from typing import List
|
||||||
from core.database import get_db
|
from core.database import get_db
|
||||||
@ -7,7 +7,9 @@ from helpers.fruit_helpers import get_all_fruits
|
|||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.get("/fruits", response_model=List[FruitSchema])
|
@router.get("/fruits", response_model=List[FruitSchema], status_code=200)
|
||||||
async def get_fruits(db: Session = Depends(get_db)):
|
async def get_fruits_with_color(db: Session = Depends(get_db)):
|
||||||
fruits = get_all_fruits(db)
|
fruits = get_all_fruits(db)
|
||||||
|
if not fruits:
|
||||||
|
raise HTTPException(status_code=404, detail="No fruits found")
|
||||||
return fruits
|
return fruits
|
@ -8,7 +8,7 @@ from schemas.fruit import FruitCreate, FruitUpdate
|
|||||||
def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bool = True) -> List[Fruit]:
|
def get_all_fruits(db: Session, skip: int = 0, limit: int = 100, active_only: bool = True) -> List[Fruit]:
|
||||||
"""
|
"""
|
||||||
Retrieves all fruits from the database with optional pagination and active filtering.
|
Retrieves all fruits from the database with optional pagination and active filtering.
|
||||||
Returns full fruit objects.
|
Returns full fruit objects including color.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session
|
db (Session): The database session
|
||||||
@ -56,7 +56,7 @@ def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit:
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session
|
db (Session): The database session
|
||||||
fruit_data (FruitCreate): The data for the fruit to create, including optional color
|
fruit_data (FruitCreate): The data for the fruit to create, including color
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Fruit: The newly created fruit object
|
Fruit: The newly created fruit object
|
||||||
@ -74,7 +74,7 @@ def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Option
|
|||||||
Args:
|
Args:
|
||||||
db (Session): The database session
|
db (Session): The database session
|
||||||
fruit_id (UUID): The ID of the fruit to update
|
fruit_id (UUID): The ID of the fruit to update
|
||||||
fruit_data (FruitUpdate): The update data for the fruit, including optional color
|
fruit_data (FruitUpdate): The update data for the fruit, including color
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Optional[Fruit]: The updated fruit object if found, otherwise None
|
Optional[Fruit]: The updated fruit object if found, otherwise None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user