From a767d7c3a49ad00e2c4e08573fc923f3dfa84915 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sat, 19 Apr 2025 23:45:58 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/food-list.get.py via AI for Menu with auto lint fixes --- .../20250419_234528_84566e88_update_menu.py | 31 +++++++ endpoints/food-list.get.py | 13 +++ helpers/menu_helpers.py | 89 +++++++++++++++++++ models/menu.py | 20 +++++ schemas/menu.py | 27 ++++++ 5 files changed, 180 insertions(+) create mode 100644 alembic/versions/20250419_234528_84566e88_update_menu.py create mode 100644 helpers/menu_helpers.py create mode 100644 models/menu.py create mode 100644 schemas/menu.py diff --git a/alembic/versions/20250419_234528_84566e88_update_menu.py b/alembic/versions/20250419_234528_84566e88_update_menu.py new file mode 100644 index 0000000..163bf07 --- /dev/null +++ b/alembic/versions/20250419_234528_84566e88_update_menu.py @@ -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) \ No newline at end of file diff --git a/endpoints/food-list.get.py b/endpoints/food-list.get.py index e69de29..09a339c 100644 --- a/endpoints/food-list.get.py +++ b/endpoints/food-list.get.py @@ -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 \ No newline at end of file diff --git a/helpers/menu_helpers.py b/helpers/menu_helpers.py new file mode 100644 index 0000000..bc44b71 --- /dev/null +++ b/helpers/menu_helpers.py @@ -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) \ No newline at end of file diff --git a/models/menu.py b/models/menu.py new file mode 100644 index 0000000..1991a8b --- /dev/null +++ b/models/menu.py @@ -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") \ No newline at end of file diff --git a/schemas/menu.py b/schemas/menu.py new file mode 100644 index 0000000..2e596e0 --- /dev/null +++ b/schemas/menu.py @@ -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 \ No newline at end of file