Spaces:
Sleeping
Sleeping
Download src/routers/orders.py from hashir-qt/keyvault-api: direct link, hf CLI and curl.
- Browser
- Download file 2 kB
-
https://huggingface.co/spaces/hashir-qt/keyvault-api/resolve/main/src/routers/orders.py
- Command line
-
hf download hf://spaces/hashir-qt/keyvault-api/src/routers/orders.py
-
curl -L -o orders.py https://huggingface.co/spaces/hashir-qt/keyvault-api/resolve/main/src/routers/orders.py
2 kB
| """Order creation and retrieval HTTP endpoints.""" | |
| import logging | |
| from fastapi import APIRouter, Depends, status | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from src.database import get_db | |
| from src.dependencies import get_current_user | |
| from src.schemas.order import CreateOrderRequest | |
| from src.services import order_service | |
| from src.utils.exceptions import NotFoundError, ValidationError, to_http_exception | |
| logger = logging.getLogger("marketplace.orders_router") | |
| router = APIRouter() | |
| async def create_order( | |
| payload: CreateOrderRequest, | |
| current_user=Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| """Create an order from cart items. Payment is mocked for demo — always succeeds.""" | |
| try: | |
| order = await order_service.create_order(db, current_user, payload) | |
| except (NotFoundError, ValidationError) as exc: | |
| raise to_http_exception(exc) | |
| return { | |
| "success": True, | |
| "data": order, | |
| "message": "Order placed successfully. Your keys are ready!", | |
| } | |
| async def list_orders( | |
| current_user=Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| """Return all orders for the authenticated buyer, newest first.""" | |
| orders = await order_service.get_buyer_orders(db, current_user.id) | |
| return {"success": True, "data": orders, "message": "OK"} | |
| async def get_order( | |
| order_id: str, | |
| current_user=Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| """Return a single order with decrypted keys. Buyer must own the order.""" | |
| try: | |
| order = await order_service.get_order_by_id(db, order_id, current_user.id) | |
| except NotFoundError as exc: | |
| raise to_http_exception(exc) | |
| return {"success": True, "data": order, "message": "OK"} | |