feat: Add Menu entity and endpoint to fetch list of meals ✅ (auto-linted)
This commit is contained in:
parent
473a38448c
commit
8f0d143ae2
31
alembic/versions/20250430_124644_40904bb4_update_menu.py
Normal file
31
alembic/versions/20250430_124644_40904bb4_update_menu.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
"""create table for menus
|
||||||
|
Revision ID: 4d4e6b7c8b74
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-24 15:48:23.796289
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '4d4e6b7c8b74'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'menus',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
|
sa.Column('category', sa.String(), nullable=True),
|
||||||
|
sa.Column('price', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=func.now(), nullable=False),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now(), nullable=False),
|
||||||
|
sa.Index('ix_menus_category', 'category')
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('menus')
|
@ -0,0 +1,12 @@
|
|||||||
|
from fastapi import APIRouter, status
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from schemas.menu import MenuSchema
|
||||||
|
from helpers.menu_helpers import get_meals_by_menu
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/foods", status_code=status.HTTP_200_OK, response_model=List[MenuSchema])
|
||||||
|
def get_meals(menu_id: str):
|
||||||
|
meals = get_meals_by_menu(menu_id=menu_id)
|
||||||
|
return meals
|
48
helpers/menu_helpers.py
Normal file
48
helpers/menu_helpers.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from uuid import UUID
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.menu import Menu
|
||||||
|
from schemas.menu import MenuSchema
|
||||||
|
|
||||||
|
def get_menus(db: Session) -> List[MenuSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves a list of all menus from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[MenuSchema]: A list of MenuSchema objects representing all menus.
|
||||||
|
"""
|
||||||
|
menus = db.query(Menu).all()
|
||||||
|
return [MenuSchema.from_orm(menu) for menu in menus]
|
||||||
|
|
||||||
|
def get_menu_by_id(db: Session, menu_id: UUID) -> Optional[MenuSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves a menu by its ID from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
menu_id (UUID): The ID of the menu to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[MenuSchema]: The MenuSchema object representing the menu, or None if not found.
|
||||||
|
"""
|
||||||
|
menu = db.query(Menu).filter(Menu.id == menu_id).first()
|
||||||
|
return MenuSchema.from_orm(menu) if menu else None
|
||||||
|
|
||||||
|
def get_meals_by_menu(db: Session, menu_id: UUID) -> List[MenuSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves a list of meals associated with a specific menu from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
menu_id (UUID): The ID of the menu to retrieve meals for.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[MenuSchema]: A list of MenuSchema objects representing the meals associated with the menu.
|
||||||
|
"""
|
||||||
|
menu = db.query(Menu).filter(Menu.id == menu_id).first()
|
||||||
|
if menu:
|
||||||
|
return [MenuSchema.from_orm(meal.menu) for meal in menu.meals]
|
||||||
|
return []
|
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)
|
||||||
|
description = Column(String, nullable=True)
|
||||||
|
category = Column(String, nullable=True, index=True)
|
||||||
|
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")
|
@ -7,3 +7,6 @@ sqlalchemy>=1.4.0
|
|||||||
python-dotenv>=0.19.0
|
python-dotenv>=0.19.0
|
||||||
bcrypt>=3.2.0
|
bcrypt>=3.2.0
|
||||||
alembic>=1.13.1
|
alembic>=1.13.1
|
||||||
|
jose
|
||||||
|
passlib
|
||||||
|
pydantic
|
||||||
|
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
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class MenuBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Menu name")
|
||||||
|
description: Optional[str] = Field(None, description="Menu description")
|
||||||
|
category: Optional[str] = Field(None, description="Menu category")
|
||||||
|
price: int = Field(..., description="Menu price")
|
||||||
|
|
||||||
|
class MenuCreate(MenuBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MenuUpdate(MenuBase):
|
||||||
|
name: Optional[str] = Field(None, description="Menu name")
|
||||||
|
description: Optional[str] = Field(None, description="Menu description")
|
||||||
|
category: Optional[str] = Field(None, description="Menu category")
|
||||||
|
price: Optional[int] = Field(None, description="Menu price")
|
||||||
|
|
||||||
|
class MenuSchema(MenuBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user