feat: Generated endpoint endpoints/food-list.get.py via AI for Menu with auto lint fixes
This commit is contained in:
parent
73cc8891a4
commit
a767d7c3a4
31
alembic/versions/20250419_234528_84566e88_update_menu.py
Normal file
31
alembic/versions/20250419_234528_84566e88_update_menu.py
Normal file
@ -0,0 +1,31 @@
|
||||
"""create table for Menu
|
||||
Revision ID: 456d7f2e9d5c
|
||||
Revises: 0001
|
||||
Create Date: 2023-05-25 12:00:00
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '456d7f2e9d5c'
|
||||
down_revision = '0001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
table_name = "menus"
|
||||
|
||||
op.create_table(
|
||||
table_name,
|
||||
sa.Column('id', sa.String(36), primary_key=True),
|
||||
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||
sa.Column('description', sa.String(), nullable=True),
|
||||
sa.Column('category', sa.String(), nullable=False),
|
||||
sa.Column('price', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now(), onupdate=sa.func.now())
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
table_name = "menus"
|
||||
op.drop_table(table_name)
|
@ -0,0 +1,13 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from typing import List
|
||||
from sqlalchemy.orm import Session
|
||||
from core.database import get_db
|
||||
from schemas.menu import MenuSchema
|
||||
from helpers.menu_helpers import get_all_menus
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/food-list", response_model=List[MenuSchema], status_code=200)
|
||||
async def get_food_list(db: Session = Depends(get_db)):
|
||||
menus = get_all_menus(db)
|
||||
return menus
|
89
helpers/menu_helpers.py
Normal file
89
helpers/menu_helpers.py
Normal file
@ -0,0 +1,89 @@
|
||||
import uuid
|
||||
from typing import List, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func
|
||||
from models.menu import Menu
|
||||
from schemas.menu import MenuSchema, MenuCreate, MenuUpdate
|
||||
|
||||
def get_all_menus(db: Session) -> List[MenuSchema]:
|
||||
"""
|
||||
Retrieves all menus from the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
|
||||
Returns:
|
||||
List[MenuSchema]: A list of all menu objects.
|
||||
"""
|
||||
menus = db.query(Menu).all()
|
||||
return [MenuSchema.from_orm(menu) for menu in menus]
|
||||
|
||||
def get_menu_by_id(db: Session, menu_id: uuid.UUID) -> Optional[MenuSchema]:
|
||||
"""
|
||||
Retrieves a single menu by its ID.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
menu_id (UUID): The ID of the menu to retrieve.
|
||||
|
||||
Returns:
|
||||
Optional[MenuSchema]: The menu object if found, otherwise None.
|
||||
"""
|
||||
menu = db.query(Menu).filter(Menu.id == menu_id).first()
|
||||
return MenuSchema.from_orm(menu) if menu else None
|
||||
|
||||
def get_menus_by_category(db: Session, category: str) -> List[MenuSchema]:
|
||||
"""
|
||||
Retrieves menus from the database based on the category.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
category (str): The category of menus to retrieve.
|
||||
|
||||
Returns:
|
||||
List[MenuSchema]: A list of menu objects matching the category.
|
||||
"""
|
||||
menus = db.query(Menu).filter(func.lower(Menu.category) == func.lower(category)).all()
|
||||
return [MenuSchema.from_orm(menu) for menu in menus]
|
||||
|
||||
def create_menu(db: Session, menu_data: MenuCreate) -> MenuSchema:
|
||||
"""
|
||||
Creates a new menu in the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
menu_data (MenuCreate): The data for the menu to create.
|
||||
|
||||
Returns:
|
||||
MenuSchema: The newly created menu object.
|
||||
"""
|
||||
db_menu = Menu(**menu_data.dict())
|
||||
db.add(db_menu)
|
||||
db.commit()
|
||||
db.refresh(db_menu)
|
||||
return MenuSchema.from_orm(db_menu)
|
||||
|
||||
def update_menu(db: Session, menu_id: uuid.UUID, menu_data: MenuUpdate) -> Optional[MenuSchema]:
|
||||
"""
|
||||
Updates an existing menu in the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
menu_id (UUID): The ID of the menu to update.
|
||||
menu_data (MenuUpdate): The updated data for the menu.
|
||||
|
||||
Returns:
|
||||
Optional[MenuSchema]: The updated menu object if found, otherwise None.
|
||||
"""
|
||||
menu = db.query(Menu).filter(Menu.id == menu_id).first()
|
||||
if not menu:
|
||||
return None
|
||||
|
||||
menu_data = menu_data.dict(exclude_unset=True)
|
||||
for key, value in menu_data.items():
|
||||
setattr(menu, key, value)
|
||||
|
||||
db.add(menu)
|
||||
db.commit()
|
||||
db.refresh(menu)
|
||||
return MenuSchema.from_orm(menu)
|
20
models/menu.py
Normal file
20
models/menu.py
Normal file
@ -0,0 +1,20 @@
|
||||
from sqlalchemy import Column, String, Integer, DateTime
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.sql import func
|
||||
from core.database import Base
|
||||
import uuid
|
||||
|
||||
class Menu(Base):
|
||||
__tablename__ = "menus"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name = Column(String, nullable=False, unique=True)
|
||||
description = Column(String, nullable=True)
|
||||
category = Column(String, nullable=False)
|
||||
price = Column(Integer, nullable=False)
|
||||
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
meals = relationship("Meal", back_populates="menu")
|
27
schemas/menu.py
Normal file
27
schemas/menu.py
Normal file
@ -0,0 +1,27 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
class MenuBase(BaseModel):
|
||||
name: str = Field(..., description="Name of the menu")
|
||||
description: Optional[str] = Field(None, description="Description of the menu")
|
||||
category: str = Field(..., description="Category of the menu")
|
||||
price: int = Field(..., description="Price of the menu")
|
||||
|
||||
class MenuCreate(MenuBase):
|
||||
pass
|
||||
|
||||
class MenuUpdate(MenuBase):
|
||||
name: Optional[str] = Field(None, description="Name of the menu")
|
||||
description: Optional[str] = Field(None, description="Description of the menu")
|
||||
category: Optional[str] = Field(None, description="Category of the menu")
|
||||
price: Optional[int] = Field(None, description="Price of the menu")
|
||||
|
||||
class MenuSchema(MenuBase):
|
||||
id: UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user