# schemas.py
class SubmenuBase(BaseModel):
title: str
description: str
dishes_count: int = 0
class Submenu(SubmenuBase):
id: uuid.UUID
class Config:
orm_mode = True
class SubmenuCreate(SubmenuBase):
pass
class DishBase(BaseModel):
title: str
description: str
price: str
class Dish(DishBase):
id: uuid.UUID
submenu_id: uuid.UUID
class Config:
orm_mode = True
class DishCreate(DishBase):
pass
# models.py
class Submenu(Base):
__tablename__ = "submenus"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
title = Column(String, unique=True)
description = Column(String)
dishes_count = Column(Integer, default=0)
dishes = relationship("Dish",
back_populates="submenu",
cascade="all, delete")
class Dish(Base):
__tablename__ = "dishes"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
title = Column(String, unique=True)
description = Column(String)
price = Column(Float, index=True)
submenu_id = Column(UUID(as_uuid=True), ForeignKey("submenus.id"))
submenu = relationship("Submenu", back_populates="dishes")
# router.py
@dish_router.post("/", response_model=schemas.Dish,
status_code=status.HTTP_201_CREATED)
def create_dish(
dish: schemas.DishCreate,
db: Session = Depends(get_db)):
new_dish = service.create_dish(dish, db)
if new_dish is None:
raise HTTPException(status_code=201, detail="Dish already exists.")
return new_dish
# service.py
def create_dish(dish: schemas.DishCreate, db: Session):
dish_in_db = db_requests.get_dish_by_title(dish.title, db)
if dish_in_db:
return None
return db_requests.create_dish(dish, db)
# db_requests.py
def create_dish(dish: schemas.DishCreate, db: Session):
new_dish = models.Dish(title=dish.title,
description=dish.description,
price=dish.price)
db.add(new_dish)
db.flush()
submenus.db_requests.add_one_dish_to_the_quantity(new_dish.submenu_id, db)
db.commit()
return new_dish
# submenus.db_requests.py
def add_one_dish_to_the_quantity(submenu_id: uuid.UUID, db: Session):
new_count = db.query(models.Submenu.dishes_count) \
.filter(models.Submenu.id == submenu_id) \
.first() + 1
print(new_count)
db.query(models.Submenu) \
.filter(models.Submenu.id == submenu_id) \
.update({"dishes_count": new_count})
# service.py
def create_dish(dish: schemas.DishCreate, submenu_title: str, db: Session):
submenu = db_requests.get_submenu_by_title(submenu_title, db)
if not submenu:
raise HTTPException(status_code=404, detail="Submenu not found.")
dish_in_db = db_requests.get_dish_by_title(dish.title, db)
if dish_in_db:
raise HTTPException(status_code=409, detail="Dish already exists.")
return db_requests.create_dish(dish, submenu.id, db)
# db_requests.py
def create_dish(dish: schemas.DishCreate, submenu_id: uuid.UUID, db: Session):
new_dish = models.Dish(title=dish.title,
description=dish.description,
price=dish.price,
submenu_id=submenu_id)
db.add(new_dish)
db.flush()
add_one_dish_to_the_quantity(submenu_id, db)
db.commit()
return new_dish
def get_submenu_by_title(submenu_title: str, db: Session):
return db.query(models.Submenu).filter(models.Submenu.title == submenu_title).first()
def get_dish_by_title(dish_title: str, db: Session):
return db.query(models.Dish).filter(models.Dish.title == dish_title).first()
def add_one_dish_to_the_quantity(submenu_id: uuid.UUID, db: Session):
db.query(models.Submenu) \
.filter(models.Submenu.id == submenu_id) \
.update({"dishes_count": models.Submenu.dishes_count + 1})
# router.py
@dish_router.post("/", response_model=schemas.Dish,
status_code=status.HTTP_201_CREATED)
def create_dish(
dish: schemas.DishCreate,
submenu_title: str,
db: Session = Depends(get_db)):
new_dish = service.create_dish(dish, submenu_title, db)
return new_dish
# forms.py
self.helper.form_action = reverse('games_detail', kwargs={'currency_pk': submenu_id})