Product rendering, cart functionality

This commit is contained in:
2026-02-08 12:40:04 +07:00
parent d8cad25f33
commit 9e96da8f53
9 changed files with 399 additions and 14 deletions

View File

@@ -0,0 +1,52 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, update
from typing import Optional
from db.models.orders import OrderItem
class OrderItemRepository:
async def get_item_by_id(
self, session: AsyncSession, item_id: int
) -> Optional[OrderItem]:
stmt = select(OrderItem).where(OrderItem.id == item_id)
return await session.scalar(stmt)
async def get_item_by_order_and_product(
self, session: AsyncSession, *, order_id: int, product_id: int
) -> Optional[OrderItem]:
stmt = (
select(OrderItem)
.where(OrderItem.order_id == order_id)
.where(OrderItem.product_id == product_id)
)
return await session.scalar(stmt)
async def update_order_item_quantity(
self, session: AsyncSession, order_item_id: int, *, quantity: int = 0
):
stmt = (
update(OrderItem.__table__)
.where(OrderItem.id == order_item_id)
.values(quantity=quantity)
)
await session.execute(stmt)
await session.commit()
async def create_order_item(
self,
session: AsyncSession,
*,
order_id: int,
product_id: int,
quantity: int = 1,
) -> OrderItem:
order_item = OrderItem(
order_id=order_id, product_id=product_id, quantity=quantity
)
session.add(order_item)
await session.commit()
return order_item

View File

@@ -2,7 +2,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from typing import Optional
from db.models import Order
from db.models import Order, OrderStatus
class OrderRepository:
@@ -13,9 +13,33 @@ class OrderRepository:
return await session.scalar(stmt)
async def get_orders_by_user(
self, session: AsyncSession, user_id: int
self, session: AsyncSession, customer: int
) -> list[Order]:
stmt = select(Order).where(Order.customer == user_id)
stmt = select(Order).where(Order.customer == customer)
result = await session.scalars(stmt)
return list(result)
async def get_draft_order_by_user(
self, session: AsyncSession, customer: int
) -> Optional[Order]:
stmt = (
select(Order)
.where(Order.customer == customer)
.where(Order.status == OrderStatus.DRAFT)
.limit(1)
)
return await session.scalar(stmt)
async def create_order(
self,
session: AsyncSession,
customer: int,
status: OrderStatus = OrderStatus.DRAFT,
):
order = Order(customer=customer, status=status)
session.add(order)
await session.commit()
return order

View File

@@ -1,5 +1,5 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from sqlalchemy import select, update
from typing import Optional
from db.models import Product
@@ -26,3 +26,15 @@ class ProductRepository:
result = await session.scalars(stmt)
return list(result)
async def add_product_file_id(
self, session: AsyncSession, product_id: int, *, file_id: str
) -> None:
stmt = (
update(Product.__table__)
.where(Product.id == product_id)
.where(Product.file_id is None)
.values(file_id=file_id)
)
await session.execute(stmt)
await session.commit()