Код: Выделить всё
class Products(Base):
__tablename__ = "products"
product_id = Column(Integer, primary_key = True)
name = Column(String, nullable= False)
price = Column(Integer, nullable= False)
description = Column(String)
stars = Column(Integer)
ingredients = Column(String, nullable= False)
nutrition = Column(String, nullable= False)
image_id = Column(Integer)
Код: Выделить всё
class BaseService:
model = None
@classmethod
async def find_all(cls):
async with async_session_maker() as session:
query = select(cls.model)
result = await session.execute(query)
return result.mappings().all()
Код: Выделить всё
from app.database import async_session_maker
from sqlalchemy import select
from app.products.models import Products
from app.services.base import BaseService
class ProductService(BaseService):
model = Products
Код: Выделить всё
class Products(Base):
__tablename__ = "products"
product_id = Column(Integer, primary_key = True)
name = Column(String, nullable= False)
price = Column(Integer, nullable= False)
description = Column(String)
stars = Column(Integer)
ingredients = Column(String, nullable= False)
nutrition = Column(String, nullable= False)
image_id = Column(Integer)
Тело ответа
Код: Выделить всё
{
"Products": {
"name": "Apple Pie",
"price": 500,
"stars": 4,
"nutrition": "Calories: 250 per slice",
"description": "Delicious homemade apple pie",
"product_id": 1,
"ingredients": "Apples, sugar, flour, cinnamon",
"image_id": null
}
}
< table class="s-table">
product_id(pk)
имя
цена
описание
звезды
ингредиенты
питание
image_id
1
Apple_pie
500
Вкусное домашнее яблоко пирог
4
Яблоки, сахар, мука, корица
Калорий: 250 на кусочек
Нет
Подробнее здесь: https://stackoverflow.com/questions/792 ... s-of-table
Мобильная версия