diff options
| author | alex <[email protected]> | 2026-07-15 20:24:31 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-15 20:24:31 +0200 |
| commit | 8e12cb9bb66adc7adaf6d276133d6f235d294d23 (patch) | |
| tree | 31aa3f6a06407b39bb23d7cc724c70899795a788 /order-service/app/api/v1/endpoints | |
| download | order-inventory-system-8e12cb9bb66adc7adaf6d276133d6f235d294d23.tar.xz order-inventory-system-8e12cb9bb66adc7adaf6d276133d6f235d294d23.zip | |
init
Diffstat (limited to 'order-service/app/api/v1/endpoints')
| -rw-r--r-- | order-service/app/api/v1/endpoints/orders.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/order-service/app/api/v1/endpoints/orders.py b/order-service/app/api/v1/endpoints/orders.py new file mode 100644 index 0000000..4530958 --- /dev/null +++ b/order-service/app/api/v1/endpoints/orders.py @@ -0,0 +1,48 @@ +from decimal import Decimal +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session + +from app.api import deps +from app.models.order import Order, OrderItem +from app.schemas.order import OrderCreate, OrderOut + +router = APIRouter() + + [email protected]("/", response_model=OrderOut, status_code=status.HTTP_202_ACCEPTED) +def create_order(order_in: OrderCreate, db: Session = Depends(deps.get_db)): + """ + Submit a new customer order. + Returns status code 202 (Accepted) as the order is pending verification. + """ + try: + total = Decimal("0.00") + for item in order_in.items: + total += item.price * item.quantity + + db_order = Order( + customer_id=order_in.customer_id, status="PENDING", total_price=total + ) + db.add(db_order) + db.flush() + + for item in order_in.items: + db_item = OrderItem( + order_id=db_order.id, + product_id=item.product_id, + quantity=item.quantity, + price=item.price, + ) + db.add(db_item) + + db.commit() + db.refresh(db_order) + + return db_order + + except Exception: + db.rollback() + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="Could not initialize checkout process.", + ) |
